-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Migrate to lucia v4 (removed the lucia library, implemented aut…
…h manually)
- Loading branch information
Showing
20 changed files
with
219 additions
and
314 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
template_builder/templates/extras/src/lib/auth/{Auth}index.ts
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,106 @@ | ||
import { Twitch } from 'arctic'; | ||
import { Base32Encoding, encodeHex } from 'oslo/encoding'; | ||
import { generateRandomString, sha256 } from 'oslo/crypto'; | ||
import type { Selectable } from 'kysely'; | ||
import { db } from '$lib/db'; | ||
import { CLIENT_ID, CLIENT_SECRET } from '$env/static/private'; | ||
import type { DB } from '$lib/db/schema'; | ||
|
||
const ONE_DAY = 1000 * 60 * 60 * 24; | ||
const base32 = new Base32Encoding('abcdefghijklmnopqrstuvwxyz234567'); | ||
|
||
export function generateId(length = 15): string { | ||
return generateRandomString(length, 'abcdefghijklmnopqrstuvwxyz0123456789'); | ||
} | ||
|
||
export function generateSessionToken(): string { | ||
const bytes = new Uint8Array(20); | ||
crypto.getRandomValues(bytes); | ||
const token = base32.encode(bytes, { | ||
includePadding: false, | ||
}); | ||
return token; | ||
} | ||
|
||
export async function createSession( | ||
token: string, | ||
userId: string, | ||
): Promise<Session> { | ||
const sessionId = encodeHex(await sha256(new TextEncoder().encode(token))); | ||
const session: Session = { | ||
id: sessionId, | ||
userId, | ||
expiresAt: new Date(Date.now() + ONE_DAY * 7), | ||
}; | ||
await db | ||
.insertInto('Session') | ||
.values({ | ||
id: session.id, | ||
user_id: session.userId, | ||
expires_at: Math.floor(session.expiresAt.getTime() / 1000), | ||
}) | ||
.execute(); | ||
return session; | ||
} | ||
|
||
export async function validateSessionToken( | ||
token: string, | ||
): Promise<SessionValidationResult> { | ||
const sessionId = encodeHex(await sha256(new TextEncoder().encode(token))); | ||
const row = await db | ||
.selectFrom('Session as s') | ||
.innerJoin('User as u', 'u.id', 's.user_id') | ||
.select(['s.id', 's.user_id', 's.expires_at', 'u.twitch_id', 'u.username']) | ||
.where('s.id', '=', sessionId) | ||
.executeTakeFirst(); | ||
if (!row) { | ||
return { session: null, user: null }; | ||
} | ||
const session: Session = { | ||
id: row.id, | ||
userId: row.user_id, | ||
expiresAt: new Date(row.expires_at * 1000), | ||
}; | ||
if (Date.now() >= session.expiresAt.getTime()) { | ||
await db.deleteFrom('Session').where('id', '=', session.id).execute(); | ||
return { session: null, user: null }; | ||
} | ||
const user: User = { | ||
id: row.user_id, | ||
twitch_id: row.twitch_id, | ||
username: row.username, | ||
}; | ||
if (Date.now() >= session.expiresAt.getTime() - ONE_DAY * 15) { | ||
session.expiresAt = new Date(Date.now() + ONE_DAY * 30); | ||
await db | ||
.updateTable('Session') | ||
.set({ | ||
expires_at: Math.floor(session.expiresAt.getTime() / 1000), | ||
}) | ||
.where('id', '=', session.id) | ||
.execute(); | ||
} | ||
return { session, user }; | ||
} | ||
|
||
export async function invalidateSession(sessionId: string): Promise<void> { | ||
await db.deleteFrom('Session').where('id', '=', sessionId).execute(); | ||
} | ||
|
||
export type SessionValidationResult = | ||
| { session: Session; user: User } | ||
| { session: null; user: null }; | ||
|
||
export interface Session { | ||
id: string; | ||
userId: string; | ||
expiresAt: Date; | ||
} | ||
|
||
export type User = Selectable<DB['User']>; | ||
|
||
export let twitch: Twitch; | ||
export function initAuth(origin: string) { | ||
if (twitch) return; | ||
twitch = new Twitch(CLIENT_ID, CLIENT_SECRET, origin + '/api/auth/callback'); | ||
} |
41 changes: 0 additions & 41 deletions
41
template_builder/templates/extras/src/lib/server/{Auth,D1}auth.ts
This file was deleted.
Oops, something went wrong.
57 changes: 0 additions & 57 deletions
57
template_builder/templates/extras/src/lib/server/{Auth,Planetscale}auth.ts
This file was deleted.
Oops, something went wrong.
40 changes: 0 additions & 40 deletions
40
template_builder/templates/extras/src/lib/server/{Auth,Sqlite}auth.ts
This file was deleted.
Oops, something went wrong.
40 changes: 0 additions & 40 deletions
40
template_builder/templates/extras/src/lib/server/{Auth,Turso}auth.ts
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
template_builder/templates/extras/src/lib/server/{Trpc,Auth}trpc.ts
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
13 changes: 0 additions & 13 deletions
13
template_builder/templates/extras/src/lib/trpc/{Trpc}index.ts
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.