-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.ts
91 lines (78 loc) · 2.09 KB
/
auth.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
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
90
91
import NextAuth, { NextAuthConfig, User } from "next-auth";
import Credentials from "next-auth/providers/credentials";
import { LoginFormSchema } from "./lib/schema";
import { prisma } from "./server/db";
import { $Enums } from "@prisma/client";
declare module "next-auth" {
interface User {
id: string;
username: string;
role: $Enums.Role;
}
}
export const authConfig = {
pages: {
signIn: "/login",
},
session: {
strategy: "jwt",
maxAge: 1 * 24 * 60 * 60,
},
callbacks: {
authorized: ({ auth, request: { nextUrl } }) => {
return !!auth?.user;
},
jwt: async ({ token, user, trigger, session }) => {
if (trigger === "update") {
token.user = session.user as User;
return token;
}
if (user) {
token.user = user as User;
}
return token;
},
session: async ({ session, token }) => {
session.user = token.user as User;
return session;
},
redirect: async ({ url, baseUrl }) => {
// Allows relative callback URLs
if (url.startsWith("/")) return `${baseUrl}${url}`;
// Allows callback URLs on the same origin
else if (new URL(url).origin === baseUrl) return url;
return baseUrl;
},
},
providers: [
Credentials({
credentials: {
username: { label: "ID", type: "text" },
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
const parsedCredentials = LoginFormSchema.safeParse(credentials);
if (parsedCredentials.success) {
const { username, password } = parsedCredentials.data;
const user = await prisma.support.findFirst({
where: {
username,
password,
},
});
if (!user) return null;
return { id: user.id, username: user.username, role: user.role };
}
return null;
},
}),
],
secret: process.env.AUTH_SECRET,
} satisfies NextAuthConfig;
export const {
handlers: { GET, POST },
auth,
update,
signIn,
signOut,
} = NextAuth(authConfig);