import { betterAuth } from "better-auth";
import { Pool } from "pg";
import { createAuthMiddleware, APIError } from "better-auth/api";
import {nextCookies} from "better-auth/next-js";
import {sendEmail} from "@/app/lib/email/send-email";
export const auth = betterAuth({
database: new Pool({
connectionString: `postgres://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASSWORD}@postgres-db:5432/${process.env.POSTGRES_DB}`,
}),
socialProviders: {
google: {
enabled: true,
prompt: "select_account consent",
accessType: "offline",
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
}
},
account: {
accountLinking: {
enabled: true
}
},
emailVerification: {
autoSignInAfterVerification: true,
sendVerificationEmail: async ({url, user}) => {
await sendEmail(url, user);
}
},
plugins: [
nextCookies(),
],
hooks: {
after: createAuthMiddleware(async (ctx) => {
const user = ctx.context.user;
if (user?.email && !user.email.endsWith('@ucsc.edu')) {
throw new APIError("FORBIDDEN", {
message: 'Only @ucsc.edu email addresses are allowed'
});
}
}),
},
});
import { betterAuth } from "better-auth";
import { Pool } from "pg";
import { createAuthMiddleware, APIError } from "better-auth/api";
import {nextCookies} from "better-auth/next-js";
import {sendEmail} from "@/app/lib/email/send-email";
export const auth = betterAuth({
database: new Pool({
connectionString: `postgres://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASSWORD}@postgres-db:5432/${process.env.POSTGRES_DB}`,
}),
socialProviders: {
google: {
enabled: true,
prompt: "select_account consent",
accessType: "offline",
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
}
},
account: {
accountLinking: {
enabled: true
}
},
emailVerification: {
autoSignInAfterVerification: true,
sendVerificationEmail: async ({url, user}) => {
await sendEmail(url, user);
}
},
plugins: [
nextCookies(),
],
hooks: {
after: createAuthMiddleware(async (ctx) => {
const user = ctx.context.user;
if (user?.email && !user.email.endsWith('@ucsc.edu')) {
throw new APIError("FORBIDDEN", {
message: 'Only @ucsc.edu email addresses are allowed'
});
}
}),
},
});