Spaces:
Running

MicroFunnel commited on
Commit
c41be9b
·
1 Parent(s): d614ea9

Fix: Add SSR guards for localStorage/document in AuthContext

Browse files
Files changed (1) hide show
  1. frontend/contexts/AuthContext.tsx +42 -20
frontend/contexts/AuthContext.tsx CHANGED
@@ -34,6 +34,11 @@ export function AuthProvider({ children }: { children: ReactNode }) {
34
 
35
  // Check for existing token on mount - HARDENED VERSION
36
  useEffect(() => {
 
 
 
 
 
37
  // Only run once on mount
38
  if (hasCheckedAuth) {
39
  return
@@ -173,9 +178,13 @@ export function AuthProvider({ children }: { children: ReactNode }) {
173
 
174
  const data = await response.json()
175
 
176
- // Save token to localStorage and cookie
177
- localStorage.setItem('auth_token', data.access_token)
178
- document.cookie = `auth_token=${data.access_token}; path=/; max-age=${7 * 24 * 60 * 60}; SameSite=Lax`
 
 
 
 
179
  setToken(data.access_token)
180
  setUser(data.user)
181
 
@@ -224,18 +233,22 @@ export function AuthProvider({ children }: { children: ReactNode }) {
224
 
225
  const data = await response.json()
226
 
227
- // Save token to localStorage and cookie
228
- localStorage.setItem('auth_token', data.access_token)
229
- document.cookie = `auth_token=${data.access_token}; path=/; max-age=${7 * 24 * 60 * 60}; SameSite=Lax`
 
 
 
 
 
 
 
230
  setToken(data.access_token)
231
  setUser(data.user)
232
 
233
  // Reset auth check flag to allow re-checking on next mount
234
  setHasCheckedAuth(false)
235
 
236
- // Show success message with referral code
237
- alert(`✅ Регистрация успешна!\n\nВаш реферальный код: ${data.user.referral_code}`)
238
-
239
  // Redirect to dashboard
240
  router.push('/dashboard')
241
  } catch (error) {
@@ -262,9 +275,13 @@ export function AuthProvider({ children }: { children: ReactNode }) {
262
 
263
  const data = await response.json()
264
 
265
- // Save token to localStorage and cookie
266
- localStorage.setItem('auth_token', data.access_token)
267
- document.cookie = `auth_token=${data.access_token}; path=/; max-age=${7 * 24 * 60 * 60}; SameSite=Lax`
 
 
 
 
268
  setToken(data.access_token)
269
  setUser(data.user)
270
 
@@ -279,19 +296,24 @@ export function AuthProvider({ children }: { children: ReactNode }) {
279
  const logout = () => {
280
  console.log('🚪 [AuthContext] Logging out...')
281
 
282
- // Clear all auth state
283
- localStorage.removeItem('auth_token')
284
- localStorage.removeItem('tenant_id') // Clear any cached data
285
-
286
- // Remove cookie
287
- document.cookie = 'auth_token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT'
 
 
 
288
 
289
  setToken(null)
290
  setUser(null)
291
 
292
  // Immediate redirect using window.location for hard refresh
293
- console.log('🔄 [AuthContext] Redirecting to login...')
294
- window.location.href = '/login'
 
 
295
  }
296
 
297
  return (
 
34
 
35
  // Check for existing token on mount - HARDENED VERSION
36
  useEffect(() => {
37
+ // SSR guard: только в браузере
38
+ if (typeof window === 'undefined') {
39
+ return
40
+ }
41
+
42
  // Only run once on mount
43
  if (hasCheckedAuth) {
44
  return
 
178
 
179
  const data = await response.json()
180
 
181
+ // SSR guard: только в браузере
182
+ if (typeof window !== 'undefined') {
183
+ // Save token to localStorage and cookie
184
+ localStorage.setItem('auth_token', data.access_token)
185
+ document.cookie = `auth_token=${data.access_token}; path=/; max-age=${7 * 24 * 60 * 60}; SameSite=Lax`
186
+ }
187
+
188
  setToken(data.access_token)
189
  setUser(data.user)
190
 
 
233
 
234
  const data = await response.json()
235
 
236
+ // SSR guard: только в браузере
237
+ if (typeof window !== 'undefined') {
238
+ // Save token to localStorage and cookie
239
+ localStorage.setItem('auth_token', data.access_token)
240
+ document.cookie = `auth_token=${data.access_token}; path=/; max-age=${7 * 24 * 60 * 60}; SameSite=Lax`
241
+
242
+ // Show success message with referral code
243
+ alert(`✅ Регистрация успешна!\n\nВаш реферальный код: ${data.user.referral_code}`)
244
+ }
245
+
246
  setToken(data.access_token)
247
  setUser(data.user)
248
 
249
  // Reset auth check flag to allow re-checking on next mount
250
  setHasCheckedAuth(false)
251
 
 
 
 
252
  // Redirect to dashboard
253
  router.push('/dashboard')
254
  } catch (error) {
 
275
 
276
  const data = await response.json()
277
 
278
+ // SSR guard: только в браузере
279
+ if (typeof window !== 'undefined') {
280
+ // Save token to localStorage and cookie
281
+ localStorage.setItem('auth_token', data.access_token)
282
+ document.cookie = `auth_token=${data.access_token}; path=/; max-age=${7 * 24 * 60 * 60}; SameSite=Lax`
283
+ }
284
+
285
  setToken(data.access_token)
286
  setUser(data.user)
287
 
 
296
  const logout = () => {
297
  console.log('🚪 [AuthContext] Logging out...')
298
 
299
+ // SSR guard: только в браузере
300
+ if (typeof window !== 'undefined') {
301
+ // Clear all auth state
302
+ localStorage.removeItem('auth_token')
303
+ localStorage.removeItem('tenant_id') // Clear any cached data
304
+
305
+ // Remove cookie
306
+ document.cookie = 'auth_token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT'
307
+ }
308
 
309
  setToken(null)
310
  setUser(null)
311
 
312
  // Immediate redirect using window.location for hard refresh
313
+ if (typeof window !== 'undefined') {
314
+ console.log('🔄 [AuthContext] Redirecting to login...')
315
+ window.location.href = '/login'
316
+ }
317
  }
318
 
319
  return (