-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth.config.ts
53 lines (50 loc) · 1.56 KB
/
auth.config.ts
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
import { NextAuthConfig } from "next-auth";
import { AuthUser } from "./app/lib/definitions";
export const authConfig = {
logger: {
warn: () => {},
},
providers: [],
pages: {
signIn: "/login",
},
callbacks: {
async authorized({ auth, request }) {
const isLoggedIn = !!auth?.user;
const isApiEndpoint =
request.headers.get("accept") === "application/json";
if (isApiEndpoint) {
const isCallbackLogin = request.nextUrl.pathname === "/";
if (isLoggedIn) {
if (!isCallbackLogin) return true;
return Response.redirect(
new URL("/api/auth/session", request.nextUrl),
);
}
return Response.json({ message: "Not authenticated" }, { status: 401 });
}
const isOnDashboard = request.nextUrl.pathname.startsWith("/dashboard");
if (isOnDashboard) {
if (isLoggedIn) return true;
return false; // Redirect unauthenticated users to login page
} else if (isLoggedIn) {
return Response.redirect(new URL("/dashboard", request.nextUrl));
}
return true;
},
jwt({ token, user }) {
if (user && (user as AuthUser).sessionId) {
token.sessionId = (user as AuthUser).sessionId;
token.id = user.id;
}
return token;
},
session({ session, token }) {
if (token.sessionId && session.user) {
(session.user as AuthUser).id = token.id as string;
(session.user as AuthUser).sessionId = token.sessionId as string;
}
return session;
},
},
} as NextAuthConfig;