-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
117 lines (105 loc) · 3.16 KB
/
auth.ts
File metadata and controls
117 lines (105 loc) · 3.16 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import NextAuth, { DefaultSession } from 'next-auth';
import 'next-auth/jwt';
import Credentials from 'next-auth/providers/credentials';
import { compare } from 'bcryptjs';
import { z } from 'zod';
import { GetManyUsers } from '@datalib/users/getUser';
declare module 'next-auth' {
interface User {
id?: string;
name?: string | null;
email?: string | null;
role: string;
position?: string;
is_beginner?: boolean;
}
interface Session extends DefaultSession {
user: {
id: string;
email: string;
role: string;
position?: string;
is_beginner?: boolean;
} & DefaultSession['user'];
}
}
declare module 'next-auth/jwt' {
interface JWT {
id: string;
name?: string | null;
email: string;
role: string;
position?: string;
is_beginner?: boolean;
}
}
const emailSchema = z.string().email('Invalid email address.');
const passwordSchema = z
.string()
.min(6, { message: 'Password must be at least 6 characters long.' })
.max(20, { message: 'Password cannot be longer than 20 characters.' });
export const { auth, handlers, signIn, signOut } = NextAuth({
session: { strategy: 'jwt' },
providers: [
Credentials({
credentials: {
email: { label: 'email', type: 'text' },
password: { label: 'password', type: 'password' },
},
async authorize(credentials) {
try {
const email = credentials.email as string;
const password = credentials.password as string;
emailSchema.parse(email);
passwordSchema.parse(password);
const response = await GetManyUsers({ email });
if (!response.ok || response.body.length === 0) {
throw new Error(response.error ?? 'User not found.');
}
const user = response.body[0];
const passwordCorrect = await compare(password, user.password);
if (!passwordCorrect) {
throw new Error('Invalid email address or password.');
}
return {
id: user._id,
name: user.name,
email: user.email,
role: user.role,
position: user.position,
is_beginner: user.is_beginner,
};
} catch (error) {
if (error instanceof z.ZodError) {
const errorMessage = error.errors.map((e) => e.message).join(' ');
throw new Error(errorMessage);
}
throw error;
}
},
}),
],
callbacks: {
async jwt({ token, user }) {
if (user) {
token.id = user.id ?? 'User ID not found';
token.name = user.name;
token.email = user.email ?? 'User email not found';
token.role = user.role;
token.position = user.position;
token.is_beginner = user.is_beginner;
}
return token;
},
async session({ session, token }) {
session.user.id = token.id;
session.user.name = token.name;
session.user.email = token.email;
session.user.role = token.role;
session.user.position = token.position;
session.user.is_beginner = token.is_beginner;
return session;
},
},
secret: process.env.AUTH_SECRET,
});