vaibhav07112004 commited on
Commit
1d3ec52
·
verified ·
1 Parent(s): a3fe2b2

Update components/auth-guard.tsx

Browse files
Files changed (1) hide show
  1. components/auth-guard.tsx +19 -36
components/auth-guard.tsx CHANGED
@@ -1,7 +1,6 @@
1
  "use client"
2
 
3
  import type React from "react"
4
-
5
  import { useState, useEffect } from "react"
6
  import { useRouter, usePathname } from "next/navigation"
7
  import { ThemeProvider } from "@/components/theme-provider"
@@ -13,46 +12,32 @@ export default function AuthGuard({ children }: { children: React.ReactNode }) {
13
  const pathname = usePathname()
14
 
15
  useEffect(() => {
16
- // Check for user data in URL (from Google OAuth)
17
- const url = new URL(window.location.href)
18
- const userDataParam = url.searchParams.get("userData")
19
-
20
- if (userDataParam) {
21
  try {
22
- const userData = JSON.parse(decodeURIComponent(userDataParam))
23
- localStorage.setItem("user", JSON.stringify(userData))
24
-
25
- // Clean URL by removing the userData parameter
26
- url.searchParams.delete("userData")
27
- window.history.replaceState({}, document.title, url.toString())
 
 
28
  } catch (error) {
29
- console.error("Error parsing user data from URL:", error)
 
 
 
30
  }
31
  }
32
 
33
- // Check if user is authenticated with more robust validation
34
- let isAuth = false
35
- try {
36
- const userStr = localStorage.getItem("user")
37
- if (userStr && userStr !== "undefined" && userStr !== "null") {
38
- const user = JSON.parse(userStr)
39
- // Check if user object has required properties
40
- isAuth = !!(user && user.id && user.email)
41
- }
42
- } catch (error) {
43
- console.error("Error parsing user data:", error)
44
- localStorage.removeItem("user") // Clear invalid data
45
- }
46
-
47
- setIsAuthenticated(isAuth)
48
- setIsLoading(false)
49
 
50
- // Simple redirect logic - only redirect if trying to access protected pages while not authenticated
51
- // Never redirect away from auth pages - let users access them freely
52
- if (!isAuth && pathname && !pathname.startsWith("/auth/") && pathname !== "/") {
53
- router.push("/auth/login")
54
  }
55
- }, [pathname, router])
56
 
57
  if (isLoading) {
58
  return (
@@ -67,8 +52,6 @@ export default function AuthGuard({ children }: { children: React.ReactNode }) {
67
  )
68
  }
69
 
70
- // Always allow access to auth pages and home page
71
- // Only protect other pages if not authenticated
72
  return (
73
  <ThemeProvider attribute="class" defaultTheme="dark" enableSystem={false} disableTransitionOnChange>
74
  {children}
 
1
  "use client"
2
 
3
  import type React from "react"
 
4
  import { useState, useEffect } from "react"
5
  import { useRouter, usePathname } from "next/navigation"
6
  import { ThemeProvider } from "@/components/theme-provider"
 
12
  const pathname = usePathname()
13
 
14
  useEffect(() => {
15
+ async function checkAuth() {
 
 
 
 
16
  try {
17
+ const response = await fetch(`${process.env.NEXT_PUBLIC_BACKEND_URL}/auth/me`, {
18
+ credentials: 'include',
19
+ })
20
+ if (response.ok) {
21
+ setIsAuthenticated(true)
22
+ } else {
23
+ setIsAuthenticated(false)
24
+ }
25
  } catch (error) {
26
+ console.error('Error checking auth status:', error)
27
+ setIsAuthenticated(false)
28
+ } finally {
29
+ setIsLoading(false)
30
  }
31
  }
32
 
33
+ checkAuth()
34
+ }, [pathname, router])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ useEffect(() => {
37
+ if (!isLoading && !isAuthenticated && pathname && !pathname.startsWith('/auth/') && pathname !== '/') {
38
+ router.push('/auth/login')
 
39
  }
40
+ }, [isLoading, isAuthenticated, pathname, router])
41
 
42
  if (isLoading) {
43
  return (
 
52
  )
53
  }
54
 
 
 
55
  return (
56
  <ThemeProvider attribute="class" defaultTheme="dark" enableSystem={false} disableTransitionOnChange>
57
  {children}