Add Better Auth admin authentication

This commit is contained in:
2026-06-04 12:05:07 +02:00
parent 0f10bd6400
commit e660ec24aa
41 changed files with 2225 additions and 284 deletions

6
lib/auth-client.ts Normal file
View File

@@ -0,0 +1,6 @@
import { convexClient } from "@convex-dev/better-auth/client/plugins";
import { createAuthClient } from "better-auth/react";
export const authClient = createAuthClient({
plugins: [convexClient()],
});

14
lib/auth-server.ts Normal file
View File

@@ -0,0 +1,14 @@
import { convexBetterAuthNextJs } from "@convex-dev/better-auth/nextjs";
export const {
handler,
preloadAuthQuery,
isAuthenticated,
getToken,
fetchAuthQuery,
fetchAuthMutation,
fetchAuthAction,
} = convexBetterAuthNextJs({
convexUrl: process.env.NEXT_PUBLIC_CONVEX_URL!,
convexSiteUrl: process.env.NEXT_PUBLIC_CONVEX_SITE_URL!,
});

View File

@@ -1,47 +0,0 @@
export const MOCK_SESSION_COOKIE_NAME = "webdev_pipeline_mock_session";
export const MOCK_SESSION_COOKIE_VALUE = "mock-admin";
export type MockCookieStore = {
get: (name: string) => { name: string; value: string } | undefined;
};
export type MockSession = {
name: string;
email: string;
};
export const MOCK_ADMIN_SESSION: MockSession = {
name: "Matthias Meister",
email: "matthias@webdev-pipeline.local",
};
export function hasMockSession(cookieStore: MockCookieStore) {
return (
cookieStore.get(MOCK_SESSION_COOKIE_NAME)?.value === MOCK_SESSION_COOKIE_VALUE
);
}
export function getMockSession(cookieStore: MockCookieStore) {
return hasMockSession(cookieStore) ? MOCK_ADMIN_SESSION : null;
}
export function createMockSessionCookie() {
return {
name: MOCK_SESSION_COOKIE_NAME,
value: MOCK_SESSION_COOKIE_VALUE,
httpOnly: true,
sameSite: "lax" as const,
secure: true,
path: "/",
maxAge: 60 * 60 * 24 * 7,
};
}
export function createClearedMockSessionCookie() {
return {
name: MOCK_SESSION_COOKIE_NAME,
value: "",
path: "/",
maxAge: 0,
};
}

View File

@@ -1,7 +0,0 @@
import { cookies } from "next/headers";
import { getMockSession } from "@/lib/mock-auth";
export async function getCurrentMockSession() {
return getMockSession(await cookies());
}

View File

@@ -1,11 +0,0 @@
import { MOCK_SESSION_COOKIE_VALUE } from "./mock-auth";
export function shouldRedirectDashboardRequest(
pathname: string,
sessionCookieValue: string | undefined,
) {
return (
pathname.startsWith("/dashboard") &&
sessionCookieValue !== MOCK_SESSION_COOKIE_VALUE
);
}

View File

@@ -1,5 +1,14 @@
import type { MockSession } from "@/lib/mock-auth";
export function getDashboardRedirectPath(session: MockSession | null) {
return session ? null : "/";
export function isDashboardPath(pathname: string) {
return pathname.startsWith("/dashboard");
}
export function shouldRedirectDashboardRequest(
pathname: string,
hasSession: boolean,
) {
return isDashboardPath(pathname) && !hasSession;
}
export function getDashboardRedirectPath(hasSession: boolean) {
return hasSession ? null : "/login";
}