-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathauth.ts
More file actions
73 lines (63 loc) · 2.16 KB
/
auth.ts
File metadata and controls
73 lines (63 loc) · 2.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
import NextAuth, { CredentialsSignin } from 'next-auth';
import Credentials from 'next-auth/providers/credentials';
import { compare } from 'bcryptjs';
import { PrismaAdapter } from '@auth/prisma-adapter';
import Google from 'next-auth/providers/google';
import GitHub from 'next-auth/providers/github';
import Twitter from 'next-auth/providers/twitter';
import { prisma } from './lib/db';
export const { handlers, signIn, signOut, auth } = NextAuth({
adapter: PrismaAdapter(prisma as any),
session: {
strategy: 'jwt',
},
providers: [
Google({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
GitHub({
clientId: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
}),
Twitter({
clientId: process.env.TWITTER_CLIENT_ID,
clientSecret: process.env.TWITTER_CLIENT_SECRET,
}),
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('Please try another login method.');
const isMatch = await compare(password, user.password);
if (!isMatch) throw new Error('Invalid credentials.');
const userData = {
name: user.name,
email: user.email,
id: user.id,
};
return userData;
},
}),
],
pages: {
signIn: '/login',
error: '/login',
},
});