Skip to content

Commit

Permalink
Auth userId used for book queries
Browse files Browse the repository at this point in the history
  • Loading branch information
arackaf committed Jan 4, 2023
1 parent a0e219a commit eb63546
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
8 changes: 4 additions & 4 deletions svelte-kit/src/data/books.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 },
Expand Down Expand Up @@ -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",
Expand All @@ -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 } }
]
Expand Down
5 changes: 4 additions & 1 deletion svelte-kit/src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand Down
9 changes: 7 additions & 2 deletions svelte-kit/src/routes/(logged-in)/home/view/+page.server.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
9 changes: 7 additions & 2 deletions svelte-kit/src/routes/api/books/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}

0 comments on commit eb63546

Please sign in to comment.