-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
54 lines (51 loc) · 1.46 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
import NextAuth from 'next-auth';
import type { Provider } from 'next-auth/providers';
import { DrizzleAdapter } from '@auth/drizzle-adapter';
import Credentials from 'next-auth/providers/credentials';
import { eq } from 'drizzle-orm';
import { user } from '@/schema/user';
import bcrypt from 'bcrypt';
import { db } from '@/lib/db';
const providers: Provider[] = [
Credentials({
credentials: {
email: {},
password: { type: 'password' },
},
authorize: async (credentials) => {
const email = credentials.email as string;
const password = credentials.password as string;
const userRes = await db.query.user.findFirst({
where: eq(user.email, email),
});
if (!userRes) {
throw new Error('User not found.');
}
if (!userRes.password) {
throw new Error('Password not found.');
}
const valid = bcrypt.compareSync(password, userRes.password);
if (!valid) {
throw new Error('Invalid password.');
}
return userRes;
},
}),
];
export const providerMap = providers.map((provider) => {
if (typeof provider === 'function') {
const providerData = provider();
return { id: providerData.id, name: providerData.name };
}
return { id: provider.id, name: provider.name };
});
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: DrizzleAdapter(db),
session: {
strategy: 'jwt',
},
providers,
pages: {
signIn: '/signin',
},
});