-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
123 lines (118 loc) · 3.63 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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 { carts, users } from './db/schema'
import { cookies } from 'next/headers'
import { NextResponse } from 'next/server'
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: {
jwt: async ({ token, user, trigger, session }: any) => {
if (user) {
token.role = user.role
if (trigger === 'signIn' || trigger === 'signUp') {
const sessionCartId = cookies().get('sessionCartId')?.value
if (!sessionCartId) throw new Error('Session Cart Not Found')
const sessionCartExists = await db.query.carts.findFirst({
where: eq(carts.sessionCartId, sessionCartId),
})
if (sessionCartExists && !sessionCartExists.userId) {
const userCartExists = await db.query.carts.findFirst({
where: eq(carts.userId, user.id),
})
if (userCartExists) {
cookies().set('beforeSigninSessionCartId', sessionCartId)
cookies().set('sessionCartId', userCartExists.sessionCartId)
} else {
db.update(carts)
.set({ userId: user.id })
.where(eq(carts.id, sessionCartExists.id))
}
}
}
}
if (session?.user.name && trigger === 'update') {
token.name = session.user.name
}
return token
},
session: async ({ session, user, trigger, token }: any) => {
session.user.id = token.sub
session.user.role = token.role
if (trigger === 'update') {
session.user.name = user.name
}
return session
},
authorized({ request, auth }: any) {
const protectedPaths = [
/\/shipping-address/,
/\/payment-method/,
/\/place-order/,
/\/profile/,
/\/user\/(.*)/,
/\/order\/(.*)/,
/\/admin/,
]
const { pathname } = request.nextUrl
if (!auth && protectedPaths.some((p) => p.test(pathname))) return false
if (!request.cookies.get('sessionCartId')) {
const sessionCartId = crypto.randomUUID()
const newRequestHeaders = new Headers(request.headers)
const response = NextResponse.next({
request: {
headers: newRequestHeaders,
},
})
response.cookies.set('sessionCartId', sessionCartId)
return response
} else {
return true
}
},
},
} satisfies NextAuthConfig
export const { handlers, auth, signIn, signOut } = NextAuth(config)