feat:google oauth

This commit is contained in:
2025-10-03 17:00:21 +02:00
parent 6fddf426b6
commit 0caa5dc9d6
12 changed files with 292 additions and 1 deletions

View File

@@ -82,3 +82,26 @@ export function deleteSessionTokenCookie(event: RequestEvent) {
path: '/'
});
}
export async function getUserFromGoogleId(googleId: string) {
const [user] = await db.select().from(table.user).where(eq(table.user.googleId, googleId));
return user || null;
}
export async function createUser(googleId: string, name: string) {
const userId = generateUserId();
const user: table.User = {
id: userId,
username: name,
googleId,
passwordHash: null,
age: null
};
await db.insert(table.user).values(user);
return user;
}
function generateUserId(): string {
const bytes = crypto.getRandomValues(new Uint8Array(15));
return encodeBase64url(bytes);
}

View File

@@ -4,7 +4,8 @@ export const user = pgTable('user', {
id: text('id').primaryKey(),
age: integer('age'),
username: text('username').notNull().unique(),
passwordHash: text('password_hash').notNull()
passwordHash: text('password_hash'),
googleId: text('google_id').unique()
});
export const session = pgTable('session', {

8
src/lib/server/oauth.ts Normal file
View File

@@ -0,0 +1,8 @@
import { Google } from 'arctic';
import { GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET } from '$env/static/private';
export const google = new Google(
GOOGLE_CLIENT_ID,
GOOGLE_CLIENT_SECRET,
'https://sergeno.ziasvannes.tech/login/google/callback'
);