This repository was archived by the owner on Feb 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
89 lines (78 loc) · 2.37 KB
/
auth.ts
File metadata and controls
89 lines (78 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { headers } from "next/headers";
import { redirect, unauthorized } from "next/navigation";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { betterAuth } from "better-auth/minimal";
import { nextCookies } from "better-auth/next-js";
import { db } from "@/lib/db";
import { cache } from "react";
export const auth = betterAuth({
baseURL: process.env.NEXT_PUBLIC_BASE_URL,
telemetry: {
enabled: false,
},
database: drizzleAdapter(db, {
provider: "pg",
}),
plugins: [nextCookies()],
// https://www.better-auth.com/docs/concepts/session-management#session-caching
session: {
cookieCache: {
enabled: true,
maxAge: 5 * 60, // 5 minutes
},
},
// https://www.better-auth.com/docs/concepts/oauth
socialProviders: {
github: {
clientId: process.env.GITHUB_CLIENT_ID!,
clientSecret: process.env.GITHUB_CLIENT_SECRET!,
},
google: {
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
},
},
// https://www.better-auth.com/docs/authentication/email-password
emailAndPassword: {
enabled: true,
},
experimental: {
// https://www.better-auth.com/docs/adapters/drizzle#joins-experimental
joins: true,
},
});
/**
* Retrieves the session and user data if valid.
* Can be used in server components, API route handlers, and server actions.
*
* @param disableCookieCache - Fetch the latest session from the database, ignoring the cookie cache.
*/
export const getAuthSession = cache(async (disableCookieCache = false) => {
return auth.api.getSession({
headers: await headers(),
query: {
disableCookieCache,
},
});
});
/**
* Same as `getAuthSession`, but redirects to the specified URL or `unauthorized.tsx` if the user is not authenticated.
*
* @param redirectUrl - Optional, uses the `unauthorized.tsx` page if not provided.
* @param disableCookieCache - Fetch the latest session from the database, ignoring the cookie cache.
*/
export async function authGuard(
redirectUrl?: string | null,
disableCookieCache?: boolean,
) {
const session = disableCookieCache
? await getAuthSession(true)
: await getAuthSession();
if (!session && redirectUrl) {
redirect(redirectUrl);
} else if (!session) {
// https://nextjs.org/docs/app/api-reference/functions/unauthorized
unauthorized();
}
return session;
}