refactor: replace next-auth with better-auth core and client

This commit is contained in:
2026-04-06 22:41:11 -04:00
parent febc57b240
commit 08a894577b
4 changed files with 31 additions and 33 deletions

View File

@@ -0,0 +1,4 @@
import { auth } from "@/auth";
import { toNextJsHandler } from "better-auth/next-js";
export const { GET, POST } = toNextJsHandler(auth);

View File

@@ -1,2 +0,0 @@
import { handlers } from "@/auth";
export const { GET, POST } = handlers;

View File

@@ -1,35 +1,26 @@
import NextAuth, { NextAuthConfig, NextAuthResult } from "next-auth"; import { betterAuth } from "better-auth";
import Authentik from "next-auth/providers/authentik"; import { drizzleAdapter } from "better-auth/adapters/drizzle";
import type { Provider } from "next-auth/providers"; import { genericOAuth } from "better-auth/plugins";
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import { db } from "@/db/index"; import { db } from "@/db/index";
import * as schema from "@/db/schema";
const providers: Provider[] = [ export const auth = betterAuth({
Authentik({ baseURL: process.env.BETTER_AUTH_URL,
clientId: process.env.AUTH_AUTHENTIK_CLIENT_ID, database: drizzleAdapter(db, {
clientSecret: process.env.AUTH_AUTHENTIK_CLIENT_SECRET, provider: "pg",
issuer: process.env.AUTH_AUTHENTIK_ISSUER, schema,
}), }),
]; plugins: [
genericOAuth({
export const providerMap = providers.map((provider) => { config: [
if (typeof provider === "function") { {
const providerData = provider(); providerId: "authentik",
return { id: providerData.id, name: providerData.name }; clientId: process.env.AUTH_AUTHENTIK_CLIENT_ID!,
} else { clientSecret: process.env.AUTH_AUTHENTIK_CLIENT_SECRET!,
return { id: provider.id, name: provider.name }; discoveryUrl: `${process.env.AUTH_AUTHENTIK_ISSUER}/.well-known/openid-configuration`,
} scopes: ["openid", "email", "profile"],
});
const config = {
adapter: DrizzleAdapter(db),
providers,
pages: {
signIn: "/auth/signin",
signOut: "/auth/signout",
error: "/auth/error",
}, },
trustHost: true, ],
} satisfies NextAuthConfig; }),
export const { handlers, signIn, signOut, auth }: NextAuthResult = ],
NextAuth(config); });

5
src/lib/auth-client.ts Normal file
View File

@@ -0,0 +1,5 @@
import { createAuthClient } from "better-auth/react";
export const authClient = createAuthClient();
export const { useSession, signIn, signOut } = authClient;