-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
85 lines (72 loc) · 1.81 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
import NextAuth, { AuthError, type DefaultSession } from "next-auth";
import credentials from "next-auth/providers/credentials";
import { Users } from "./lib/Users";
import { signInSchema } from "./lib/zod";
import { ZodError } from "zod";
declare module "next-auth" {
interface Session {
user: {
userId: string ;
} & DefaultSession["user"];
}
}
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
credentials({
credentials: {
email: {
label: "Email",
type: "text",
},
password: {
label: "Password",
type: "text",
},
},
authorize: async (credentials) => {
try {
let user = null;
const { email, password } = signInSchema.parse(credentials);
user = await Users.getUser(email);
if (!user) {
throw new Error("User not found");
}
// check password
if (password !== user.password) {
throw new Error("Password is incorrect");
}
return user;
} catch (error) {
if (error instanceof ZodError) {
// Return `null` to indicate that the credentials are invalid
return null;
}
return null;
}
},
}),
],
callbacks: {
async signIn(params) {
return true;
},
async session({ session, user, token }) {
// console.log(`Token is ${JSON.stringify(token)}`);
session.user.userId = token.sub ||"";
return session;
},
async jwt({ token, user, account, profile }) {
// console.log(token);
return token;
},
},
pages: {
signIn: "/signin",
error: "/error",
},
session: {
strategy: "jwt",
},
trustHost: true,
secret: process.env.AUTH_SECRET,
});