2025-05-23 16:26:02 +03:00
// src/lib/auth.ts (or your auth config file)
2025-05-22 13:17:11 +03:00
import { betterAuth } from "better-auth" ;
import { drizzleAdapter } from "better-auth/adapters/drizzle" ;
import { env } from '$env/dynamic/private' ;
import { db } from "./server/db" ;
2025-05-23 16:26:02 +03:00
import * as schema from "./server/db/schema" ;
import { generateUsername } from "./utils/random" ;
import { uploadProfilePicture } from "./server/s3" ;
2025-05-22 13:17:11 +03:00
if ( ! env . GOOGLE_CLIENT_ID ) throw new Error ( 'GOOGLE_CLIENT_ID is not set' ) ;
if ( ! env . GOOGLE_CLIENT_SECRET ) throw new Error ( 'GOOGLE_CLIENT_SECRET is not set' ) ;
export const auth = betterAuth ( {
baseURL : env.PUBLIC_BETTER_AUTH_URL ,
secret : env.PRIVATE_BETTER_AUTH_SECRET ,
appName : "Rugplay" ,
2025-05-30 16:44:38 +03:00
trustedOrigins : [
env . BETTER_AUTH_URL , "http://rugplay.com" , "http://localhost:5173" ,
] ,
2025-05-22 13:17:11 +03:00
database : drizzleAdapter ( db , {
provider : "pg" ,
2025-05-23 16:26:02 +03:00
schema : schema ,
2025-05-22 13:17:11 +03:00
} ) ,
socialProviders : {
google : {
clientId : env.GOOGLE_CLIENT_ID ,
clientSecret : env.GOOGLE_CLIENT_SECRET ,
2025-05-23 16:26:02 +03:00
mapProfileToUser : async ( profile ) = > {
const newUsername = generateUsername ( ) ;
let s3ImageKey : string | null = null ;
if ( profile . picture ) {
try {
const response = await fetch ( profile . picture ) ;
if ( ! response . ok ) {
console . error ( ` Failed to fetch profile picture: ${ response . statusText } ` ) ;
} else {
const blob = await response . blob ( ) ;
const arrayBuffer = await blob . arrayBuffer ( ) ;
s3ImageKey = await uploadProfilePicture (
2025-05-28 16:49:13 +03:00
profile . sub ,
2025-05-23 16:26:02 +03:00
new Uint8Array ( arrayBuffer ) ,
blob . type ,
blob . size
) ;
}
} catch ( error ) {
console . error ( 'Failed to upload profile picture during social login:' , error ) ;
}
}
return {
name : profile.name ,
email : profile.email ,
2025-05-28 16:49:13 +03:00
image : s3ImageKey ,
2025-05-23 16:26:02 +03:00
username : newUsername ,
} ;
} ,
}
} ,
user : {
additionalFields : {
username : { type : "string" , required : true , input : false } ,
isAdmin : { type : "boolean" , required : false , input : false } ,
isBanned : { type : "boolean" , required : false , input : false } ,
banReason : { type : "string" , required : false , input : false } ,
baseCurrencyBalance : { type : "string" , required : false , input : false } ,
bio : { type : "string" , required : false } ,
2025-05-29 17:41:09 +03:00
volumeMaster : { type : "string" , required : false , input : false } ,
volumeMuted : { type : "boolean" , required : false , input : false } ,
2025-05-22 13:17:11 +03:00
}
} ,
session : {
cookieCache : {
enabled : true ,
2025-05-23 16:26:02 +03:00
maxAge : 60 * 5 ,
2025-05-22 13:17:11 +03:00
}
} ,
2025-05-22 14:37:19 +03:00
advanced : {
2025-05-23 16:26:02 +03:00
database : {
generateId : false ,
}
2025-05-22 14:37:19 +03:00
}
2025-05-22 13:17:11 +03:00
} ) ;