This repository has been archived by the owner on Nov 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
247 additions
and
534 deletions.
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
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 |
---|---|---|
@@ -1,13 +1,116 @@ | ||
import { Auth } from '@auth/core'; | ||
import { authConfig } from '~/lib/auth'; | ||
import { generateState, OAuth2RequestError } from 'arctic'; | ||
import { generateId } from 'lucia'; | ||
import { parseCookies, serializeCookie } from 'oslo/cookie'; | ||
import { z } from 'zod'; | ||
import { users } from '~/db/schemas/auth'; | ||
import { env } from '~/env'; | ||
import { github, lucia } from '~/lib/auth'; | ||
import { db } from '~/lib/db'; | ||
import { createElysia } from '~/util/elysia'; | ||
|
||
// const githubUserSchema = z.object({ | ||
// id: z.string(), | ||
// login: z.string(), | ||
// }); | ||
|
||
export const routes = createElysia({ prefix: '/auth' }) | ||
.get('/*', async (ctx) => { | ||
const res = await Auth(ctx.request, authConfig); | ||
return res; | ||
.get('/login/github', async (ctx) => { | ||
const state = generateState(); | ||
const url = await github.createAuthorizationURL(state); | ||
return new Response(null, { | ||
status: 302, | ||
headers: { | ||
Location: url.toString(), | ||
'Set-Cookie': serializeCookie('github_oauth_state', state, { | ||
httpOnly: true, | ||
secure: env.NODE_ENV === 'production', // set `Secure` flag in HTTPS | ||
maxAge: 60 * 10, // 10 minutes | ||
path: '/', | ||
}), | ||
}, | ||
}); | ||
}) | ||
.get('/login/github/callback', async (ctx) => { | ||
const cookies = parseCookies(ctx.request.headers.get('Cookie') ?? ''); | ||
const stateCookie = cookies.get('github_oauth_state') ?? null; | ||
|
||
const url = new URL(ctx.request.url); | ||
const state = url.searchParams.get('state'); | ||
const code = url.searchParams.get('code'); | ||
|
||
// verify state | ||
if (!state || !stateCookie || !code || stateCookie !== state) { | ||
return new Response(null, { | ||
status: 400, | ||
}); | ||
} | ||
|
||
try { | ||
const tokens = await github.validateAuthorizationCode(code); | ||
const githubUserResponse = await fetch('https://api.github.com/user', { | ||
headers: { | ||
Authorization: `Bearer ${tokens.accessToken}`, | ||
}, | ||
}); | ||
const githubUser = await githubUserResponse.json(); | ||
|
||
const existingUser = await db.query.users.findFirst({ | ||
where: (users, { eq }) => eq(users.githubId, githubUser.id), | ||
}); | ||
|
||
if (existingUser) { | ||
const session = await lucia.createSession(existingUser.id, {}); | ||
const sessionCookie = lucia.createSessionCookie(session.id); | ||
return new Response(null, { | ||
status: 302, | ||
headers: { | ||
Location: '/', | ||
'Set-Cookie': sessionCookie.serialize(), | ||
}, | ||
}); | ||
} | ||
|
||
const userId = generateId(15); | ||
await db.insert(users).values({ | ||
id: userId, | ||
githubId: githubUser.id, | ||
}); | ||
|
||
const session = await lucia.createSession(userId, {}); | ||
const sessionCookie = lucia.createSessionCookie(session.id); | ||
return new Response(null, { | ||
status: 302, | ||
headers: { | ||
Location: '/', | ||
'Set-Cookie': sessionCookie.serialize(), | ||
}, | ||
}); | ||
} catch (e) { | ||
console.log(e); | ||
if (e instanceof OAuth2RequestError) { | ||
// bad verification code, invalid credentials, etc | ||
return new Response(null, { | ||
status: 400, | ||
}); | ||
} | ||
return new Response(null, { | ||
status: 500, | ||
}); | ||
} | ||
}) | ||
.post('/*', async (ctx) => { | ||
const res = await Auth(ctx.request, authConfig); | ||
return res; | ||
.post('/logout', async (ctx) => { | ||
if (!ctx.session) { | ||
return new Response(null, { | ||
status: 401, | ||
}); | ||
} | ||
|
||
await lucia.invalidateSession(ctx.session.id); | ||
return new Response(null, { | ||
status: 302, | ||
headers: { | ||
Location: '/', | ||
'Set-Cookie': lucia.createBlankSessionCookie().serialize(), | ||
}, | ||
}); | ||
}); |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.