diff --git a/README.md b/README.md index d101fef0..c440baa0 100644 --- a/README.md +++ b/README.md @@ -42,4 +42,4 @@ TBA If you need credentials for the `.env` file, contact the project lead ([Minh](https://github.com/minhxNguyen7/)). -After changes to the .env file, run `pnpm run check` to update SvelteKit's auto-generated environment variable types. +After changes to the .env file, run `pnpm run check` to update SvelteKit's auto-generated environment variable types. diff --git a/package.json b/package.json index 44ec1ad0..5ea0a85b 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "dependencies": { "@iconify/json": "^2.2.160", "@lucia-auth/adapter-prisma": "3.0.2", + "@lucia-auth/oauth": "^3.5.3", "@prisma/client": "5.6.0", "lucia": "2.7.4", "svelty-picker": "^5.2.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c9757405..143c0afd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,6 +11,9 @@ dependencies: '@lucia-auth/adapter-prisma': specifier: 3.0.2 version: 3.0.2(@prisma/client@5.6.0)(lucia@2.7.4) + '@lucia-auth/oauth': + specifier: ^3.5.3 + version: 3.5.3(lucia@2.7.4) '@prisma/client': specifier: 5.6.0 version: 5.6.0(prisma@5.6.0) @@ -2530,6 +2533,14 @@ packages: lucia: 2.7.4 dev: false + /@lucia-auth/oauth@3.5.3(lucia@2.7.4): + resolution: {integrity: sha512-3blBldenf2zXsSJTZUwzB+NxI+0KJljfMHT0WR7hyLNwi4Ijss7F2cQqk2xW1lGH3lzdmsV93ZsY9Yv8ekBRUw==} + peerDependencies: + lucia: ^2.0.0 + dependencies: + lucia: 2.7.4 + dev: false + /@lukeed/ms@2.0.1: resolution: {integrity: sha512-Xs/4RZltsAL7pkvaNStUQt7netTkyxrS0K+RILcVr3TRMS/ToOg4I6uNfhB9SlGsnWBym4U+EaXq0f0cEMNkHA==} engines: {node: '>=8'} diff --git a/prisma/dev.db b/prisma/dev.db index d0cd72ce..b13b1a9f 100644 Binary files a/prisma/dev.db and b/prisma/dev.db differ diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 9eff4e71..af305c7b 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -26,9 +26,11 @@ export const handle: Handle = async ({ event, resolve }) => { if (event.locals?.auth) { const session = await event.locals.auth.validate(); const user = session?.user; + if (user) { event.locals.user = user; } + if (event.route.id?.startsWith("/(protected)")) { if (!user) throw redirect(302, "/auth"); // if (!user.verified) throw redirect(302, "/auth/verify/email"); diff --git a/src/lib/server/lucia.ts b/src/lib/server/lucia.ts index 710ea0c2..1962e0a5 100644 --- a/src/lib/server/lucia.ts +++ b/src/lib/server/lucia.ts @@ -1,8 +1,14 @@ import { prisma } from "@lucia-auth/adapter-prisma"; +import { google } from "@lucia-auth/oauth/providers"; import { lucia } from "lucia"; import { sveltekit } from "lucia/middleware"; import { dev } from "$app/environment"; +import { + GOOGLE_OAUTH_CLIENT_ID, + GOOGLE_OAUTH_CLIENT_SECRET, + GOOGLE_OAUTH_REDIRECT_URI, +} from "$env/static/private"; import { prisma as client } from "$lib/server/prisma"; export const auth = lucia({ @@ -23,8 +29,21 @@ export const auth = lucia({ verified: data.verified, receiveEmail: data.receiveEmail, token: data.token, + + googleId: data.google_id, + username: data.username, }; }, }); +export const googleAuth = google(auth, { + clientId: GOOGLE_OAUTH_CLIENT_ID!, + clientSecret: GOOGLE_OAUTH_CLIENT_SECRET!, + redirectUri: GOOGLE_OAUTH_REDIRECT_URI!, + scope: [ + "https://www.googleapis.com/auth/userinfo.profile", + "https://www.googleapis.com/auth/userinfo.email", + ], +}); + export type Auth = typeof auth; diff --git a/src/routes/auth/+page.svelte b/src/routes/auth/+page.svelte index 4eda3326..e14d0a21 100644 --- a/src/routes/auth/+page.svelte +++ b/src/routes/auth/+page.svelte @@ -24,6 +24,15 @@ {/if} +
+