-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.config.ts
61 lines (56 loc) · 1.38 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
54
55
56
57
58
59
60
61
import "next-auth/jwt";
import type { NextAuthConfig, DefaultSession } from "next-auth";
import { loginSchema } from "@/schemas/login-schema";
import { db } from "@/server/db";
import Credentials from "next-auth/providers/credentials";
declare module "next-auth" {
interface Session extends DefaultSession {
user: {
id: string;
nim: string;
voted: boolean | null;
} & DefaultSession["user"];
}
interface User {
nim: string;
voted: boolean | null;
}
}
declare module "next-auth/jwt" {
interface JWT {
nim: string;
voted: boolean | null;
}
}
export default {
callbacks: {
session: async ({ session, token }) => {
if (session.user && token.sub) {
session.user.id = token.sub;
session.user.nim = token.nim;
session.user.voted = token.voted;
}
return session;
},
jwt: async ({ user, token }) => {
if (user) {
token.nim = user.nim;
token.voted = user.voted;
}
return token;
},
},
providers: [
Credentials({
credentials: { nim: {} },
authorize: async (creds) => {
const { nim } = await loginSchema.parseAsync(creds);
const user = await db.query.users.findFirst({
where: (users, { eq }) => eq(users.nim, nim),
});
if (user) return user;
return null;
},
}),
],
} satisfies NextAuthConfig;