This repository has been archived on 2025-08-19. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
coinstorge/website/src/lib/auth.ts

82 lines
3 KiB
TypeScript
Raw Normal View History

// 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";
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",
database: drizzleAdapter(db, {
provider: "pg",
schema: schema,
2025-05-22 13:17:11 +03:00
}),
socialProviders: {
google: {
clientId: env.GOOGLE_CLIENT_ID,
clientSecret: env.GOOGLE_CLIENT_SECRET,
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,
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,
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,
maxAge: 60 * 5,
2025-05-22 13:17:11 +03:00
}
},
2025-05-22 14:37:19 +03:00
advanced: {
database: {
generateId: false,
}
2025-05-22 14:37:19 +03:00
}
2025-05-22 13:17:11 +03:00
});