-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
153 lines (138 loc) · 4.67 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { DrizzleAdapter } from '@auth/drizzle-adapter'
import { eq } from 'drizzle-orm'
import type { NextAuthConfig } from 'next-auth'
import NextAuth from 'next-auth'
import Google from 'next-auth/providers/google'
import db from './db/drizzle'
import { users } from './db/schema'
import { processReferral } from './lib/actions/referral.actions'
export const config = {
pages: {
signIn: '/sign-in',
error: '/sign-in',
},
session: {
strategy: 'jwt',
maxAge: 30 * 24 * 60 * 60,
},
adapter: DrizzleAdapter(db),
providers: [
Google({
allowDangerousEmailAccountLinking: true,
authorization: {
params: {
prompt: "select_account",
access_type: "offline",
response_type: "code"
}
}
}),
],
callbacks: {
jwt: async ({ token, user, trigger, session, account, request, response }: any) => {
if (user) {
if (account?.provider === 'google') {
const existingUser = await db.query.users.findFirst({
where: (users, { eq }) => eq(users.email, user.email),
});
if (existingUser) {
token.role = existingUser.role;
} else {
token.role = 'user';
const userId = crypto.randomUUID();
await db.insert(users).values({
id: userId,
name: user.name,
email: user.email,
emailVerified: new Date(),
image: user.image,
role: 'user'
}).onConflictDoUpdate({
target: users.email,
set: {
name: user.name,
image: user.image,
emailVerified: new Date()
}
});
// Handle referral after user creation
try {
const cookies = request?.cookies;
const referralCode = cookies?.get('referral_code')?.value;
if (referralCode) {
await processReferral(referralCode, userId);
// Clear the referral cookie after processing
response?.cookies.delete('referral_code');
}
} catch (error) {
console.error('Error processing referral:', error);
}
}
} else {
token.role = user.role;
}
if (user.name === 'NO_NAME') {
token.name = user.email!.split('@')[0];
await db
.update(users)
.set({ name: token.name })
.where(eq(users.id, user.id));
}
if (trigger === 'signIn' || trigger === 'signUp') {
try {
const response = await fetch(new URL('/api/cart/merge', process.env.NEXTAUTH_URL).toString(), {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ userId: user.id }),
});
if (!response.ok) {
console.error('Failed to merge carts');
}
} catch (error) {
console.error('Error merging carts:', error);
}
}
}
if (session?.user.name && trigger === 'update') {
token.name = session.user.name;
}
return token;
},
session: async ({ session, token }: any) => {
session.user.id = token.sub
session.user.role = token.role
return session
},
authorized({ request, auth }: any) {
const protectedPaths = [
/\/shipping-address/,
/\/payment-method/,
/\/place-order/,
/\/profile/,
/\/onboard/,
/\/payment\/(.*)/,
/\/user\/(.*)/,
/\/order\/(.*)/,
/\/admin/,
]
const { pathname } = request.nextUrl
return !protectedPaths.some((p) => p.test(pathname)) || !!auth
},
signIn: async ({ user, account, request }: any) => {
console.log('🔑 Sign in callback triggered:', { user, account });
try {
const cookies = request?.cookies;
const referralCode = cookies?.get('referral_code')?.value;
if (referralCode && user.id) {
console.log('📨 Processing referral:', { referralCode, userId: user.id });
await processReferral(referralCode, user.id);
}
return true;
} catch (error) {
console.error('🔥 Error in signIn callback:', error);
return true; // Still allow sign in even if referral processing fails
}
},
},
} satisfies NextAuthConfig
export const { handlers, auth, signIn, signOut } = NextAuth(config)