mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-06 20:37:44 +00:00
247 lines
6.0 KiB
TypeScript
247 lines
6.0 KiB
TypeScript
import { betterAuth } from "better-auth";
|
|
import {
|
|
bearer,
|
|
admin,
|
|
multiSession,
|
|
organization,
|
|
twoFactor,
|
|
oneTap,
|
|
oAuthProxy,
|
|
openAPI,
|
|
customSession,
|
|
deviceAuthorization,
|
|
lastLoginMethod,
|
|
} from "better-auth/plugins";
|
|
import { reactInvitationEmail } from "./email/invitation";
|
|
import { LibsqlDialect } from "@libsql/kysely-libsql";
|
|
import { reactResetPasswordEmail } from "./email/reset-password";
|
|
import { resend } from "./email/resend";
|
|
import { MysqlDialect } from "kysely";
|
|
import { createPool } from "mysql2/promise";
|
|
import { nextCookies } from "better-auth/next-js";
|
|
import { passkey } from "better-auth/plugins/passkey";
|
|
import { stripe } from "@better-auth/stripe";
|
|
import { Stripe } from "stripe";
|
|
|
|
const from = process.env.BETTER_AUTH_EMAIL || "delivered@resend.dev";
|
|
const to = process.env.TEST_EMAIL || "";
|
|
|
|
const libsql = new LibsqlDialect({
|
|
url: process.env.TURSO_DATABASE_URL || "",
|
|
authToken: process.env.TURSO_AUTH_TOKEN || "",
|
|
});
|
|
|
|
const mysql = process.env.USE_MYSQL
|
|
? new MysqlDialect(createPool(process.env.MYSQL_DATABASE_URL || ""))
|
|
: null;
|
|
|
|
const dialect = process.env.USE_MYSQL ? mysql : libsql;
|
|
|
|
if (!dialect) {
|
|
throw new Error("No dialect found");
|
|
}
|
|
|
|
const baseURL: string | undefined =
|
|
process.env.VERCEL === "1"
|
|
? process.env.VERCEL_ENV === "production"
|
|
? process.env.BETTER_AUTH_URL
|
|
: process.env.VERCEL_ENV === "preview"
|
|
? `https://${process.env.VERCEL_URL}`
|
|
: undefined
|
|
: undefined;
|
|
|
|
const cookieDomain: string | undefined =
|
|
process.env.VERCEL === "1"
|
|
? process.env.VERCEL_ENV === "production"
|
|
? ".better-auth.com"
|
|
: process.env.VERCEL_ENV === "preview"
|
|
? `.${process.env.VERCEL_URL}`
|
|
: undefined
|
|
: undefined;
|
|
|
|
export const auth = betterAuth({
|
|
appName: "Better Auth Demo",
|
|
baseURL,
|
|
database: {
|
|
dialect: libsql,
|
|
type: "sqlite",
|
|
},
|
|
emailVerification: {
|
|
async sendVerificationEmail({ user, url }) {
|
|
const res = await resend.emails.send({
|
|
from,
|
|
to: to || user.email,
|
|
subject: "Verify your email address",
|
|
html: `<a href="${url}">Verify your email address</a>`,
|
|
});
|
|
console.log(res, user.email);
|
|
},
|
|
},
|
|
account: {
|
|
accountLinking: {
|
|
trustedProviders: ["google", "github", "demo-app"],
|
|
},
|
|
},
|
|
emailAndPassword: {
|
|
enabled: true,
|
|
async sendResetPassword({ user, url }) {
|
|
await resend.emails.send({
|
|
from,
|
|
to: user.email,
|
|
subject: "Reset your password",
|
|
react: reactResetPasswordEmail({
|
|
username: user.email,
|
|
resetLink: url,
|
|
}),
|
|
});
|
|
},
|
|
},
|
|
socialProviders: {
|
|
facebook: {
|
|
clientId: process.env.FACEBOOK_CLIENT_ID || "",
|
|
clientSecret: process.env.FACEBOOK_CLIENT_SECRET || "",
|
|
},
|
|
github: {
|
|
clientId: process.env.GITHUB_CLIENT_ID || "",
|
|
clientSecret: process.env.GITHUB_CLIENT_SECRET || "",
|
|
},
|
|
google: {
|
|
clientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID || "",
|
|
clientSecret: process.env.GOOGLE_CLIENT_SECRET || "",
|
|
},
|
|
discord: {
|
|
clientId: process.env.DISCORD_CLIENT_ID || "",
|
|
clientSecret: process.env.DISCORD_CLIENT_SECRET || "",
|
|
},
|
|
microsoft: {
|
|
clientId: process.env.MICROSOFT_CLIENT_ID || "",
|
|
clientSecret: process.env.MICROSOFT_CLIENT_SECRET || "",
|
|
},
|
|
twitch: {
|
|
clientId: process.env.TWITCH_CLIENT_ID || "",
|
|
clientSecret: process.env.TWITCH_CLIENT_SECRET || "",
|
|
},
|
|
twitter: {
|
|
clientId: process.env.TWITTER_CLIENT_ID || "",
|
|
clientSecret: process.env.TWITTER_CLIENT_SECRET || "",
|
|
},
|
|
paypal: {
|
|
clientId: process.env.PAYPAL_CLIENT_ID || "",
|
|
clientSecret: process.env.PAYPAL_CLIENT_SECRET || "",
|
|
},
|
|
},
|
|
plugins: [
|
|
organization({
|
|
async sendInvitationEmail(data) {
|
|
await resend.emails.send({
|
|
from,
|
|
to: data.email,
|
|
subject: "You've been invited to join an organization",
|
|
react: reactInvitationEmail({
|
|
username: data.email,
|
|
invitedByUsername: data.inviter.user.name,
|
|
invitedByEmail: data.inviter.user.email,
|
|
teamName: data.organization.name,
|
|
inviteLink:
|
|
process.env.NODE_ENV === "development"
|
|
? `http://localhost:3000/accept-invitation/${data.id}`
|
|
: `${
|
|
process.env.BETTER_AUTH_URL ||
|
|
"https://demo.better-auth.com"
|
|
}/accept-invitation/${data.id}`,
|
|
}),
|
|
});
|
|
},
|
|
}),
|
|
twoFactor({
|
|
otpOptions: {
|
|
async sendOTP({ user, otp }) {
|
|
await resend.emails.send({
|
|
from,
|
|
to: user.email,
|
|
subject: "Your OTP",
|
|
html: `Your OTP is ${otp}`,
|
|
});
|
|
},
|
|
},
|
|
}),
|
|
passkey(),
|
|
openAPI(),
|
|
bearer(),
|
|
admin({
|
|
adminUserIds: ["EXD5zjob2SD6CBWcEQ6OpLRHcyoUbnaB"],
|
|
}),
|
|
multiSession(),
|
|
oAuthProxy(),
|
|
nextCookies(),
|
|
|
|
oneTap(),
|
|
customSession(async (session) => {
|
|
return {
|
|
...session,
|
|
user: {
|
|
...session.user,
|
|
dd: "test",
|
|
},
|
|
};
|
|
}),
|
|
stripe({
|
|
stripeClient: new Stripe(process.env.STRIPE_KEY || "sk_test_"),
|
|
stripeWebhookSecret: process.env.STRIPE_WEBHOOK_SECRET!,
|
|
subscription: {
|
|
enabled: true,
|
|
allowReTrialsForDifferentPlans: true,
|
|
plans: () => {
|
|
const PRO_PRICE_ID = {
|
|
default:
|
|
process.env.STRIPE_PRO_PRICE_ID ??
|
|
"price_1RoxnRHmTADgihIt4y8c0lVE",
|
|
annual:
|
|
process.env.STRIPE_PRO_ANNUAL_PRICE_ID ??
|
|
"price_1RoxnoHmTADgihItzFvVP8KT",
|
|
};
|
|
const PLUS_PRICE_ID = {
|
|
default:
|
|
process.env.STRIPE_PLUS_PRICE_ID ??
|
|
"price_1RoxnJHmTADgihIthZTLmrPn",
|
|
annual:
|
|
process.env.STRIPE_PLUS_ANNUAL_PRICE_ID ??
|
|
"price_1Roxo5HmTADgihItEbJu5llL",
|
|
};
|
|
|
|
return [
|
|
{
|
|
name: "Plus",
|
|
priceId: PLUS_PRICE_ID.default,
|
|
annualDiscountPriceId: PLUS_PRICE_ID.annual,
|
|
freeTrial: {
|
|
days: 7,
|
|
},
|
|
},
|
|
{
|
|
name: "Pro",
|
|
priceId: PRO_PRICE_ID.default,
|
|
annualDiscountPriceId: PRO_PRICE_ID.annual,
|
|
freeTrial: {
|
|
days: 7,
|
|
},
|
|
},
|
|
];
|
|
},
|
|
},
|
|
}),
|
|
deviceAuthorization({
|
|
expiresIn: "3min",
|
|
interval: "5s",
|
|
}),
|
|
lastLoginMethod(),
|
|
],
|
|
trustedOrigins: ["exp://"],
|
|
advanced: {
|
|
crossSubDomainCookies: {
|
|
enabled: process.env.NODE_ENV === "production",
|
|
domain: cookieDomain,
|
|
},
|
|
},
|
|
});
|