forked from TEAM-ISAID/ISAID
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
73 lines (68 loc) · 1.94 KB
/
auth.ts
File metadata and controls
73 lines (68 loc) · 1.94 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
import NextAuth from 'next-auth';
import type { User } from 'next-auth';
import Credentials from 'next-auth/providers/credentials';
import { PrismaAdapter } from '@next-auth/prisma-adapter';
import bcrypt from 'bcryptjs';
import { getUserByEmail } from './data/user';
import { prisma } from './lib/prisma';
import { LoginSchema } from './lib/schemas/auth';
export const {
handlers,
auth,
signIn,
signOut,
unstable_update: update,
} = NextAuth({
adapter: PrismaAdapter(prisma),
providers: [
Credentials({
name: 'Credentials',
credentials: {
email: { label: 'Email', type: 'email' },
password: { label: 'Password', type: 'password' },
},
async authorize(credentials) {
const result = LoginSchema.safeParse(credentials);
if (!result.success) return null;
const { email, password } = result.data;
const user = await getUserByEmail(email);
if (!user || !user.password) return null;
const isPasswordValid = await bcrypt.compare(password, user.password);
if (!isPasswordValid) return null;
return {
id: String(user.id),
name: user.name,
email: user.email,
} satisfies User;
},
}),
],
pages: {
signIn: '/login',
},
secret: process.env.AUTH_SECRET ?? '',
session: {
strategy: 'jwt',
maxAge: 60 * 60 * 24, // 24 hours
},
callbacks: {
jwt({ token, user, trigger, session }) {
if (user) {
token.id = String(user.id);
token.name = user.name;
token.email = user.email;
}
if (trigger === 'update' && session?.user) {
token.name = session.user.name;
token.email = session.user.email;
}
return token;
},
session({ session, token }) {
session.user.id = token?.id ? String(token.id) : '';
session.user.name = token?.name ?? '';
session.user.email = token?.email ?? '';
return session;
},
},
});