-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ implement google oauth (#27)
* feat: ✨ [SEMI-STABLE] implement google oauth * chore: 🔧 cleanup code * feat: ✨ use sub prop for id * style: 🎨 move google button to layout * fix: 🐛 handle empty name fields * chore: 🔧 [STABLE] remove unused deps * chore: 🔧 remove unused fields * feat: ✨ use email as userId * fix: 🐛 add console log to try block
- Loading branch information
1 parent
2b7f669
commit 9cdb339
Showing
9 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { dev } from "$app/environment"; | ||
import { googleAuth } from "$lib/server/lucia"; | ||
|
||
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(); | ||
|
||
// 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(), | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { OAuthRequestError } from "@lucia-auth/oauth"; | ||
import type { GoogleUser } from "@lucia-auth/oauth/providers"; | ||
|
||
import { auth, googleAuth } from "$lib/server/lucia"; | ||
|
||
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); | ||
} | ||
|
||
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; | ||
}; | ||
|
||
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, | ||
}); | ||
} | ||
|
||
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, | ||
}); | ||
} | ||
|
||
const session = await auth.createSession({ | ||
userId: user.userId, | ||
attributes: {}, | ||
}); | ||
|
||
locals.auth.setSession(session); | ||
|
||
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, | ||
}); | ||
} | ||
|
||
// All other errors. | ||
return new Response(null, { | ||
status: 500, | ||
}); | ||
} | ||
}; |