diff --git a/svelte-kit/src/data/books.ts b/svelte-kit/src/data/books.ts index e36eaf4de..3a59600c2 100644 --- a/svelte-kit/src/data/books.ts +++ b/svelte-kit/src/data/books.ts @@ -21,7 +21,7 @@ const bookProjections = bookFields.reduce<{ [k: string]: 1 }>((result, field) => return result; }, {}); -export const searchBooks = async (search: string) => { +export const searchBooks = async (userId: string, search: string) => { const httpStart = +new Date(); return fetch(env.MONGO_URL + "/action/aggregate", { method: "POST", @@ -35,7 +35,7 @@ export const searchBooks = async (search: string) => { database: "my-library", dataSource: "Cluster0", pipeline: [ - { $match: { title: { $regex: search || "", $options: "i" }, userId: "60a93babcc3928454b5d1cc6" } }, + { $match: { title: { $regex: search || "", $options: "i" }, userId } }, { $project: bookProjections }, { $addFields: { dateAdded: { $toDate: "$_id" } } }, { $limit: 50 }, @@ -65,7 +65,7 @@ export const searchBooks = async (search: string) => { }); }; -export const booksSubjectsDump = async () => { +export const booksSubjectsDump = async (userId: string) => { const httpStart = +new Date(); return fetch(env.MONGO_URL + "/action/aggregate", { method: "POST", @@ -79,7 +79,7 @@ export const booksSubjectsDump = async () => { database: "my-library", dataSource: "Cluster0", pipeline: [ - { $match: { userId: "60a93babcc3928454b5d1cc6", "subjects.0": { $exists: true } } }, + { $match: { userId, "subjects.0": { $exists: true } } }, { $group: { _id: "$subjects", count: { $count: {} } } }, { $project: { _id: 0, subjects: "$_id", count: 1 } } ] diff --git a/svelte-kit/src/hooks.server.ts b/svelte-kit/src/hooks.server.ts index ce9e177b0..adacb58a1 100644 --- a/svelte-kit/src/hooks.server.ts +++ b/svelte-kit/src/hooks.server.ts @@ -49,11 +49,14 @@ const auth = SvelteKitAuth({ } const userSync = await getUserSync(account.providerAccountId); + if (userSync) { + account.syncdId = userSync.sk; + } return true; }, async jwt({ token, account }) { - token.userId ??= account?.providerAccountId; + token.userId ??= account?.syncdId || account?.providerAccountId; return token; }, async session({ session, user, token }) { diff --git a/svelte-kit/src/routes/(logged-in)/home/view/+page.server.ts b/svelte-kit/src/routes/(logged-in)/home/view/+page.server.ts index 49689122b..ea3cb7f9c 100644 --- a/svelte-kit/src/routes/(logged-in)/home/view/+page.server.ts +++ b/svelte-kit/src/routes/(logged-in)/home/view/+page.server.ts @@ -1,9 +1,14 @@ import { booksSubjectsDump } from "$data/books"; import { allSubjects } from "$data/subjects"; -export async function load() { +export async function load({ locals }: any) { + const session = await locals.getSession(); + if (!session) { + return {}; + } + const subjects = allSubjects(); - const books = booksSubjectsDump(); + const books = booksSubjectsDump(session.userId); return { subjects, diff --git a/svelte-kit/src/routes/api/books/+server.ts b/svelte-kit/src/routes/api/books/+server.ts index 4260becfc..c4b8242fb 100644 --- a/svelte-kit/src/routes/api/books/+server.ts +++ b/svelte-kit/src/routes/api/books/+server.ts @@ -2,7 +2,12 @@ import { searchBooks } from "$data/books"; import { BOOKS_CACHE } from "$lib/state/cacheHelpers"; import { json } from "@sveltejs/kit"; -export async function GET({ url, request, setHeaders }: { url: URL; cookies: any; request: any; setHeaders: any }) { +export async function GET({ url, request, setHeaders, locals }: { url: URL; cookies: any; request: any; setHeaders: any; locals: any }) { + const session = await locals.getSession(); + if (!session) { + return json({}); + } + const currentCacheBust = request.headers.get(BOOKS_CACHE); if (currentCacheBust) { @@ -14,7 +19,7 @@ export async function GET({ url, request, setHeaders }: { url: URL; cookies: any const search = url.searchParams.get("search") || ""; - const books = await searchBooks(search); + const books = await searchBooks(session.userId, search); return json(books); }