refactor: replace next-auth with better-auth core and client
This commit is contained in:
4
src/app/api/auth/[...all]/route.ts
Normal file
4
src/app/api/auth/[...all]/route.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { auth } from "@/auth";
|
||||
import { toNextJsHandler } from "better-auth/next-js";
|
||||
|
||||
export const { GET, POST } = toNextJsHandler(auth);
|
||||
@@ -1,2 +0,0 @@
|
||||
import { handlers } from "@/auth";
|
||||
export const { GET, POST } = handlers;
|
||||
53
src/auth.ts
53
src/auth.ts
@@ -1,35 +1,26 @@
|
||||
import NextAuth, { NextAuthConfig, NextAuthResult } from "next-auth";
|
||||
import Authentik from "next-auth/providers/authentik";
|
||||
import type { Provider } from "next-auth/providers";
|
||||
import { DrizzleAdapter } from "@auth/drizzle-adapter";
|
||||
import { betterAuth } from "better-auth";
|
||||
import { drizzleAdapter } from "better-auth/adapters/drizzle";
|
||||
import { genericOAuth } from "better-auth/plugins";
|
||||
import { db } from "@/db/index";
|
||||
import * as schema from "@/db/schema";
|
||||
|
||||
const providers: Provider[] = [
|
||||
Authentik({
|
||||
clientId: process.env.AUTH_AUTHENTIK_CLIENT_ID,
|
||||
clientSecret: process.env.AUTH_AUTHENTIK_CLIENT_SECRET,
|
||||
issuer: process.env.AUTH_AUTHENTIK_ISSUER,
|
||||
export const auth = betterAuth({
|
||||
baseURL: process.env.BETTER_AUTH_URL,
|
||||
database: drizzleAdapter(db, {
|
||||
provider: "pg",
|
||||
schema,
|
||||
}),
|
||||
];
|
||||
|
||||
export const providerMap = providers.map((provider) => {
|
||||
if (typeof provider === "function") {
|
||||
const providerData = provider();
|
||||
return { id: providerData.id, name: providerData.name };
|
||||
} else {
|
||||
return { id: provider.id, name: provider.name };
|
||||
}
|
||||
});
|
||||
|
||||
const config = {
|
||||
adapter: DrizzleAdapter(db),
|
||||
providers,
|
||||
pages: {
|
||||
signIn: "/auth/signin",
|
||||
signOut: "/auth/signout",
|
||||
error: "/auth/error",
|
||||
plugins: [
|
||||
genericOAuth({
|
||||
config: [
|
||||
{
|
||||
providerId: "authentik",
|
||||
clientId: process.env.AUTH_AUTHENTIK_CLIENT_ID!,
|
||||
clientSecret: process.env.AUTH_AUTHENTIK_CLIENT_SECRET!,
|
||||
discoveryUrl: `${process.env.AUTH_AUTHENTIK_ISSUER}/.well-known/openid-configuration`,
|
||||
scopes: ["openid", "email", "profile"],
|
||||
},
|
||||
trustHost: true,
|
||||
} satisfies NextAuthConfig;
|
||||
export const { handlers, signIn, signOut, auth }: NextAuthResult =
|
||||
NextAuth(config);
|
||||
],
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
5
src/lib/auth-client.ts
Normal file
5
src/lib/auth-client.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { createAuthClient } from "better-auth/react";
|
||||
|
||||
export const authClient = createAuthClient();
|
||||
|
||||
export const { useSession, signIn, signOut } = authClient;
|
||||
Reference in New Issue
Block a user