Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ disable google auth #44

Merged
merged 1 commit into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/lib/server/lucia.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { prisma } from "@lucia-auth/adapter-prisma";
import { google } from "@lucia-auth/oauth/providers";
// 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 {
// 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({
Expand Down Expand Up @@ -36,14 +36,14 @@ export const auth = lucia({
},
});

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 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;
4 changes: 2 additions & 2 deletions src/routes/auth/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
</div>
{/if}

<div class="mb-4">
<!-- <div class="mb-4">
<a
href="/auth/login/google"
class="mx-auto flex w-fit rounded-md bg-blue-500 p-2 font-semibold text-white"
>
Continue with Google
</a>
</div>
</div> -->

<TabGroup justify="justify-center">
<Tab bind:group={tabSet} name="signInTab" value={"signIn"}>Sign In</Tab>
Expand Down
54 changes: 27 additions & 27 deletions src/routes/auth/login/google/+server.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import { dev } from "$app/environment";
import { googleAuth } from "$lib/server/lucia";
// import { dev } from "$app/environment";
// import { googleAuth } from "$lib/server/lucia";

export const GET = async ({ cookies, locals }) => {
const session = await locals.auth.validate();
// export const GET = async ({ cookies, locals }) => {
// const session = await locals.auth.validate();

if (session) {
return new Response(null, {
status: 302,
headers: {
Location: "/",
},
});
}
const [url, state] = await googleAuth.getAuthorizationUrl();
// if (session) {
// return new Response(null, {
// status: 302,
// headers: {
// Location: "/",
// },
// });
// }
// const [url, state] = await googleAuth.getAuthorizationUrl();

// Store state.
cookies.set("google_oauth_state", state, {
httpOnly: true,
secure: !dev,
path: "/",
maxAge: 30 * 24 * 60 * 60,
});
// // Store state.
// cookies.set("google_oauth_state", state, {
// httpOnly: true,
// secure: !dev,
// path: "/",
// maxAge: 30 * 24 * 60 * 60,
// });

return new Response(null, {
status: 302,
headers: {
Location: url.toString(),
},
});
};
// return new Response(null, {
// status: 302,
// headers: {
// Location: url.toString(),
// },
// });
// };
200 changes: 100 additions & 100 deletions src/routes/auth/login/google/callback/+server.ts
Original file line number Diff line number Diff line change
@@ -1,114 +1,114 @@
import { OAuthRequestError } from "@lucia-auth/oauth";
import type { GoogleUser } from "@lucia-auth/oauth/providers";
// import { OAuthRequestError } from "@lucia-auth/oauth";
// import type { GoogleUser } from "@lucia-auth/oauth/providers";

import { auth, googleAuth } from "$lib/server/lucia";
// import { auth, googleAuth } from "$lib/server/lucia";

const getUser = async (googleUser: GoogleUser) => {
if (!googleUser.email) {
return null;
}
// const getUser = async (googleUser: GoogleUser) => {
// if (!googleUser.email) {
// return null;
// }

try {
const dbUser = await auth.getUser(googleUser.email);
if (dbUser) {
return dbUser;
}
} catch (error) {
/* If a user cannot be found, an error is raised and caught here. */
console.log("User not found in database", error);
}
// try {
// const dbUser = await auth.getUser(googleUser.email);
// if (dbUser) {
// return dbUser;
// }
// } catch (error) {
// /* If a user cannot be found, an error is raised and caught here. */
// console.log("User not found in database", error);
// }

const token = crypto.randomUUID();
const user = await auth.createUser({
userId: googleUser.email.toLowerCase(),
key: {
providerId: "google",
providerUserId: googleUser.email.toLowerCase(),
password: null,
},
attributes: {
email: googleUser.email.toLowerCase(),
firstName: googleUser.given_name ?? "",
lastName: googleUser.family_name ?? "",
// role: "USER",
verified: false,
receiveEmail: true,
token: token,
},
});
// const token = crypto.randomUUID();
// const user = await auth.createUser({
// userId: googleUser.email.toLowerCase(),
// key: {
// providerId: "google",
// providerUserId: googleUser.email.toLowerCase(),
// password: null,
// },
// attributes: {
// email: googleUser.email.toLowerCase(),
// firstName: googleUser.given_name ?? "",
// lastName: googleUser.family_name ?? "",
// // role: "USER",
// verified: false,
// receiveEmail: true,
// token: token,
// },
// });

return user;
};
// return user;
// };

export const GET = async ({ url, cookies, locals }) => {
/**
* Check for a session. if it exists,
* redirect to a page of your liking.
*/
const session = await locals.auth.validate();
if (session) {
return new Response(null, {
status: 302,
headers: {
Location: "/auth",
},
});
}
// export const GET = async ({ url, cookies, locals }) => {
// /**
// * Check for a session. if it exists,
// * redirect to a page of your liking.
// */
// const session = await locals.auth.validate();
// if (session) {
// return new Response(null, {
// status: 302,
// headers: {
// Location: "/auth",
// },
// });
// }

/**
* Validate state of the request.
*/
const storedState = cookies.get("google_oauth_state") ?? null;
const state = url.searchParams.get("state");
const code = url.searchParams.get("code");
if (!storedState || !state || storedState !== state || !code) {
return new Response(null, {
status: 400,
});
}
// /**
// * Validate state of the request.
// */
// const storedState = cookies.get("google_oauth_state") ?? null;
// const state = url.searchParams.get("state");
// const code = url.searchParams.get("code");
// if (!storedState || !state || storedState !== state || !code) {
// return new Response(null, {
// status: 400,
// });
// }

try {
const { googleUser } = await googleAuth.validateCallback(code);
const user = await getUser(googleUser);
// try {
// const { googleUser } = await googleAuth.validateCallback(code);
// const user = await getUser(googleUser);

if (!user) {
/**
* You should probably redirect the user to a page and show a
* message that the account could not be created.
*
* This is a very rare case, but it can happen.
*/
return new Response(null, {
status: 500,
});
}
// if (!user) {
// /**
// * You should probably redirect the user to a page and show a
// * message that the account could not be created.
// *
// * This is a very rare case, but it can happen.
// */
// return new Response(null, {
// status: 500,
// });
// }

const session = await auth.createSession({
userId: user.userId,
attributes: {},
});
// const session = await auth.createSession({
// userId: user.userId,
// attributes: {},
// });

locals.auth.setSession(session);
// locals.auth.setSession(session);

return new Response(null, {
status: 302,
headers: {
Location: "/auth",
},
});
} catch (e) {
console.log(e);
// return new Response(null, {
// status: 302,
// headers: {
// Location: "/auth",
// },
// });
// } catch (e) {
// console.log(e);

// Invalid code.
if (e instanceof OAuthRequestError) {
return new Response(null, {
status: 400,
});
}
// // Invalid code.
// if (e instanceof OAuthRequestError) {
// return new Response(null, {
// status: 400,
// });
// }

// All other errors.
return new Response(null, {
status: 500,
});
}
};
// // All other errors.
// return new Response(null, {
// status: 500,
// });
// }
// };
Loading