forked from basir/next-pg-shadcn-ecommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
63 lines (60 loc) · 1.6 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
57
58
59
60
61
62
63
import { DrizzleAdapter } from '@auth/drizzle-adapter'
import { compareSync } from 'bcrypt-ts-edge'
import { eq } from 'drizzle-orm'
import type { NextAuthConfig } from 'next-auth'
import NextAuth from 'next-auth'
import CredentialsProvider from 'next-auth/providers/credentials'
import db from './db/drizzle'
import { users } from './db/schema'
export const config = {
pages: {
signIn: '/sign-in',
error: '/sign-in',
},
session: {
strategy: 'jwt',
maxAge: 30 * 24 * 60 * 60,
},
adapter: DrizzleAdapter(db),
providers: [
CredentialsProvider({
credentials: {
email: {
type: 'email',
},
password: { type: 'password' },
},
async authorize(credentials) {
if (credentials == null) return null
const user = await db.query.users.findFirst({
where: eq(users.email, credentials.email as string),
})
if (user && user.password) {
const isMatch = compareSync(
credentials.password as string,
user.password
)
if (isMatch) {
return {
id: user.id,
name: user.name,
email: user.email,
role: user.role,
}
}
}
return null
},
}),
],
callbacks: {
session: async ({ session, user, trigger, token }: any) => {
session.user.id = token.sub
if (trigger === 'update') {
session.user.name = user.name
}
return session
},
},
} satisfies NextAuthConfig
export const { handlers, auth, signIn, signOut } = NextAuth(config)