Added better auth. Still fixing issues
This commit is contained in:
56
apps/backend/src/lib/auth.ts
Normal file
56
apps/backend/src/lib/auth.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { sendMail } from '@/lib/mailer';
|
||||
import { db } from '@/lib/prisma';
|
||||
import bcrypt from 'bcrypt';
|
||||
import { betterAuth } from 'better-auth';
|
||||
import { prismaAdapter } from 'better-auth/adapters/prisma';
|
||||
import { emailOTP } from 'better-auth/plugins';
|
||||
|
||||
export const auth = betterAuth({
|
||||
database: prismaAdapter(db, {
|
||||
provider: 'postgresql',
|
||||
}),
|
||||
emailAndPassword: {
|
||||
enabled: true,
|
||||
password: {
|
||||
hash: async (password) => {
|
||||
return bcrypt.hash(password, 10);
|
||||
},
|
||||
verify: async (password, hash) => {
|
||||
return bcrypt.compare(password, hash);
|
||||
},
|
||||
},
|
||||
},
|
||||
plugins: [
|
||||
emailOTP({
|
||||
async sendVerificationOTP({ email, otp, type }) {
|
||||
let subject = 'Tu código de verificación';
|
||||
let text = `Tu código OTP es ${otp}.`;
|
||||
let html = `<p>Tu código OTP es <strong>${otp}</strong>.</p>`;
|
||||
|
||||
if (type === 'sign-in') {
|
||||
subject = 'Código de inicio de sesión';
|
||||
text = `Tu código para iniciar sesión es ${otp}.`;
|
||||
html = `<p>Tu código para iniciar sesión es <strong>${otp}</strong>.</p>`;
|
||||
} else if (type === 'forget-password') {
|
||||
subject = 'Código para restablecer contraseña';
|
||||
text = `Tu código para restablecer tu contraseña es ${otp}.`;
|
||||
html = `<p>Tu código para restablecer tu contraseña es <strong>${otp}</strong>.</p>`;
|
||||
} else if (type === 'email-verification') {
|
||||
subject = 'Verifica tu correo electrónico';
|
||||
text = `Tu código de verificación es ${otp}.`;
|
||||
html = `<p>Tu código de verificación es <strong>${otp}</strong>.</p>`;
|
||||
}
|
||||
|
||||
await sendMail({
|
||||
to: email,
|
||||
subject,
|
||||
text,
|
||||
html,
|
||||
});
|
||||
},
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
export type Session = typeof auth.$Infer.Session.session;
|
||||
export type User = typeof auth.$Infer.Session.user;
|
||||
@@ -1,26 +0,0 @@
|
||||
import { createClient } from '@supabase/supabase-js';
|
||||
|
||||
let cachedClient: ReturnType<typeof createClient> | null = null;
|
||||
|
||||
export function getSupabaseAdminClient() {
|
||||
if (cachedClient) return cachedClient;
|
||||
|
||||
const supabaseUrl = Bun.env.SUPABASE_URL ?? Bun.env.VITE_SUPABASE_URL;
|
||||
const supabaseServiceRoleKey = Bun.env.SUPABASE_SERVICE_ROLE_KEY;
|
||||
|
||||
if (!supabaseUrl || !supabaseServiceRoleKey) {
|
||||
throw new Error(
|
||||
'Missing Supabase admin env vars. Set SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY in backend environment.'
|
||||
);
|
||||
}
|
||||
|
||||
cachedClient = createClient(supabaseUrl, supabaseServiceRoleKey, {
|
||||
auth: {
|
||||
autoRefreshToken: false,
|
||||
persistSession: false,
|
||||
detectSessionInUrl: false,
|
||||
},
|
||||
});
|
||||
|
||||
return cachedClient;
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
import { createClient } from '@supabase/supabase-js';
|
||||
|
||||
const supabaseUrl = Bun.env.SUPABASE_URL ?? Bun.env.VITE_SUPABASE_URL;
|
||||
const supabaseAnonKey = Bun.env.SUPABASE_ANON_KEY ?? Bun.env.VITE_SUPABASE_ANON_KEY;
|
||||
|
||||
if (!supabaseUrl || !supabaseAnonKey) {
|
||||
throw new Error(
|
||||
'Missing Supabase env vars. Set SUPABASE_URL/SUPABASE_ANON_KEY (or VITE_SUPABASE_URL/VITE_SUPABASE_ANON_KEY) in backend environment.'
|
||||
);
|
||||
}
|
||||
|
||||
export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
|
||||
auth: {
|
||||
autoRefreshToken: false,
|
||||
persistSession: false,
|
||||
detectSessionInUrl: false,
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user