Add Better Auth admin authentication
This commit is contained in:
6
lib/auth-client.ts
Normal file
6
lib/auth-client.ts
Normal 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
14
lib/auth-server.ts
Normal 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!,
|
||||
});
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
import { getMockSession } from "@/lib/mock-auth";
|
||||
|
||||
export async function getCurrentMockSession() {
|
||||
return getMockSession(await cookies());
|
||||
}
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user