Skip to content

Commit 9fae5f7

Browse files
committed
Refactor signup to use DB trigger for profile creation
Profile creation during signup is now handled by the database trigger 'on_auth_user_created' instead of client-side code. This change avoids race conditions and client-side RLS issues. Minor formatting improvements were also made throughout the file.
1 parent a89469d commit 9fae5f7

1 file changed

Lines changed: 34 additions & 46 deletions

File tree

lib/features/auth/auth-service.ts

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,20 @@ export function getCurrentUser(): User | null {
3636
// Async version that fetches from Supabase and updates cache
3737
export async function getCurrentUserAsync(): Promise<User | null> {
3838
if (typeof window === "undefined") return null
39-
39+
4040
try {
4141
const supabase = createClient()
4242
const { data: { user: authUser }, error } = await supabase.auth.getUser()
43-
43+
4444
if (error || !authUser) return null
45-
45+
4646
// Fetch user profile from database
4747
const { data: profile, error: profileError } = await supabase
4848
.from('profiles')
4949
.select('*')
5050
.eq('id', authUser.id)
5151
.single()
52-
52+
5353
if (profileError || !profile) {
5454
// Return basic user info if profile doesn't exist
5555
return {
@@ -60,7 +60,7 @@ export async function getCurrentUserAsync(): Promise<User | null> {
6060
language: 'en'
6161
}
6262
}
63-
63+
6464
return {
6565
id: profile.id,
6666
email: authUser.email || '',
@@ -95,7 +95,7 @@ export async function signUp(
9595
): Promise<{ success: boolean; error?: string; user?: User }> {
9696
try {
9797
const supabase = createClient()
98-
98+
9999
// Sign up with Supabase Auth
100100
const { data: authData, error: signUpError } = await supabase.auth.signUp({
101101
email,
@@ -107,41 +107,29 @@ export async function signUp(
107107
}
108108
}
109109
})
110-
110+
111111
if (signUpError) {
112112
return { success: false, error: signUpError.message }
113113
}
114-
114+
115115
if (!authData.user) {
116116
return { success: false, error: 'Sign up failed' }
117117
}
118-
119-
// Create user profile in database
120-
const { error: profileError } = await supabase
121-
.from('profiles')
122-
.insert({
123-
id: authData.user.id,
124-
email,
125-
name,
126-
language: language || 'en',
127-
created_at: new Date().toISOString()
128-
})
129-
130-
if (profileError) {
131-
console.error('Error creating profile:', profileError)
132-
}
133-
118+
119+
// Profile creation is handled by the database trigger 'on_auth_user_created'
120+
// This allows us to avoid race conditions and client-side RLS issues during signup
121+
134122
const newUser: User = {
135123
id: authData.user.id,
136124
email,
137125
name,
138126
createdAt: new Date().toISOString(),
139127
language: language || 'en',
140128
}
141-
129+
142130
// Cache user in localStorage
143131
localStorage.setItem("lab68_session", JSON.stringify(newUser))
144-
132+
145133
return { success: true, user: newUser }
146134
} catch (error: any) {
147135
return { success: false, error: error.message || 'Sign up failed' }
@@ -156,27 +144,27 @@ export async function signIn(
156144
): Promise<{ success: boolean; error?: string; user?: User }> {
157145
try {
158146
const supabase = createClient()
159-
147+
160148
const { data: authData, error: signInError } = await supabase.auth.signInWithPassword({
161149
email,
162150
password,
163151
})
164-
152+
165153
if (signInError) {
166154
return { success: false, error: signInError.message }
167155
}
168-
156+
169157
if (!authData.user) {
170158
return { success: false, error: 'Sign in failed' }
171159
}
172-
160+
173161
// Fetch user profile
174162
const { data: profile } = await supabase
175163
.from('profiles')
176164
.select('*')
177165
.eq('id', authData.user.id)
178166
.single()
179-
167+
180168
const user: User = profile ? {
181169
id: profile.id,
182170
email: authData.user.email || '',
@@ -194,16 +182,16 @@ export async function signIn(
194182
createdAt: authData.user.created_at,
195183
language: 'en'
196184
}
197-
185+
198186
// Cache user in localStorage
199187
localStorage.setItem("lab68_session", JSON.stringify(user))
200-
188+
201189
if (rememberMe) {
202190
localStorage.setItem("lab68_remember", "true")
203191
} else {
204192
localStorage.removeItem("lab68_remember")
205193
}
206-
194+
207195
return { success: true, user }
208196
} catch (error: any) {
209197
return { success: false, error: error.message || 'Sign in failed' }
@@ -240,7 +228,7 @@ export async function updateUserProfile(
240228
): Promise<{ success: boolean; error?: string; user?: User }> {
241229
try {
242230
const supabase = createClient()
243-
231+
244232
// Update profile in database
245233
const { data, error } = await supabase
246234
.from('profiles')
@@ -255,18 +243,18 @@ export async function updateUserProfile(
255243
.eq('id', userId)
256244
.select()
257245
.single()
258-
246+
259247
if (error) {
260248
return { success: false, error: error.message }
261249
}
262-
250+
263251
// Get updated user
264252
const currentUser = await getCurrentUserAsync()
265-
253+
266254
if (currentUser && currentUser.id === userId) {
267255
localStorage.setItem("lab68_session", JSON.stringify(currentUser))
268256
}
269-
257+
270258
return { success: true, user: currentUser || undefined }
271259
} catch (error: any) {
272260
return { success: false, error: error.message || 'Update failed' }
@@ -275,25 +263,25 @@ export async function updateUserProfile(
275263

276264
export async function checkRememberMe(): Promise<User | null> {
277265
if (typeof window === "undefined") return null
278-
266+
279267
try {
280268
const remember = localStorage.getItem("lab68_remember")
281269
if (!remember) return null
282-
270+
283271
const supabase = createClient()
284272
const { data: { user: authUser } } = await supabase.auth.getUser()
285-
273+
286274
if (!authUser) return null
287-
275+
288276
// Fetch user profile
289277
const { data: profile } = await supabase
290278
.from('profiles')
291279
.select('*')
292280
.eq('id', authUser.id)
293281
.single()
294-
282+
295283
if (!profile) return null
296-
284+
297285
const user: User = {
298286
id: profile.id,
299287
email: authUser.email || '',
@@ -305,7 +293,7 @@ export async function checkRememberMe(): Promise<User | null> {
305293
website: profile.website,
306294
avatar: profile.avatar
307295
}
308-
296+
309297
// Restore session cache
310298
localStorage.setItem("lab68_session", JSON.stringify(user))
311299
return user

0 commit comments

Comments
 (0)