Skip to content

Commit

Permalink
Modified Prisma queries in login callback to only select necessary fi…
Browse files Browse the repository at this point in the history
…elds

Moved layout's `Map` type to `types.d.ts` as `DefinitionDashMap`
  • Loading branch information
hopperelec committed Jan 25, 2024
1 parent c080bcf commit 0d9fdea
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/lib/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
export type Question = { id: number; question: string };
export type LocalDoor = { room1_id: number; room2_id: number };
export type DefinitionDashMap = {
id: number;
data: string;
doors: LocalDoor[];
};
8 changes: 2 additions & 6 deletions src/routes/(app)/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ import type { LayoutServerLoad } from "./$types";
import type { User } from "@prisma/client";
import { getMapFor } from "$lib/get-map-for";
import prisma from "$lib/prisma";
import type { DefinitionDashMap } from "$lib/types";

type Map = {
id: number;
data: string;
doors: { room1_id: number; room2_id: number }[];
};
type Props = {
user: User;
map?: Map;
map?: DefinitionDashMap;
};
export const load: LayoutServerLoad = async ({ locals }) => {
const data: Props = {
Expand Down
11 changes: 7 additions & 4 deletions src/routes/login/callback/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { OAuth2Client } from "google-auth-library";
import { PUBLIC_GOOGLE_CLIENT_ID } from "$env/static/public";
import { ALLOWED_DOMAIN } from "$env/static/private";
import prisma from "$lib/prisma";
import type { School, User } from "@prisma/client";
import { toBuffer } from "uuid-buffer";
import { dev } from "$app/environment";
import { SESSION_COOKIE_KEY, SESSION_DURATION_DAYS } from "$lib/constants";
Expand All @@ -30,17 +29,20 @@ export const POST: RequestHandler = async ({ request, cookies }) => {
throw error(400, "Failed to verify ID token");
}

let user: User | null = await prisma.user.findFirst({
let user: { id: number } | null = await prisma.user.findFirst({
where: { google_sub: payload.sub },
select: { id: true },
});
if (!user) {
const domain = payload.hd || ALLOWED_DOMAIN;
let school: School | null = await prisma.school.findUnique({
let school: { id: number } | null = await prisma.school.findUnique({
where: { domain },
select: { id: true },
});
if (!school) {
school = await prisma.school.create({
data: { domain },
select: { id: true },
});
}
user = await prisma.user.create({
Expand All @@ -49,9 +51,10 @@ export const POST: RequestHandler = async ({ request, cookies }) => {
google_sub: payload.sub,
allowed: payload.hd === ALLOWED_DOMAIN,
},
select: { id: true },
});
}
user = await prisma.user.update({
await prisma.user.update({
where: {
id: user.id,
},
Expand Down

0 comments on commit 0d9fdea

Please sign in to comment.