-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathauth.ts
56 lines (46 loc) · 1.56 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
import NextAuth, { CredentialsSignin } from 'next-auth';
import Credentials from 'next-auth/providers/credentials';
import { compare } from 'bcryptjs';
import { prisma } from './lib/db';
import { PrismaAdapter } from '@auth/prisma-adapter';
export const { handlers, signIn, signOut, auth } = NextAuth({
adapter: PrismaAdapter(prisma),
session: {
strategy: 'jwt',
},
providers: [
Credentials({
name: 'credentials',
credentials: {
email: { label: 'email', type: 'email' },
password: { label: 'password', type: 'password' },
},
authorize: async (credentials) => {
const email = credentials.email as string | undefined;
const password = credentials.password as string | undefined;
if (!email || !password)
throw new CredentialsSignin(
'Please provide both email and password.'
);
const user = await prisma.user.findFirst({
where: {
email,
},
});
if (!user) throw new Error('Invalid credentials.');
if (!user.password) throw new Error('Invalid password');
const isMatch = compare(password, user.password);
if (!isMatch) throw new Error('Incorrect Password.');
const userData = {
name: user.name,
email: user.email,
id: user.id,
};
return userData;
},
}),
],
pages: {
signIn: '/login',
},
});