feat:google oauth
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
8
src/lib/server/oauth.ts
Normal 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'
|
||||
);
|
||||
Reference in New Issue
Block a user