|
| 1 | +/** |
| 2 | + * Authentication middleware for protecting routes |
| 3 | + * This is a client-side authentication check that can be used in components |
| 4 | + */ |
| 5 | + |
| 6 | +import { getCurrentUser } from '../lib/supabase/auth'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Check if user is authenticated and redirect if not |
| 10 | + * @param {string} redirectTo - Path to redirect to if not authenticated |
| 11 | + * @returns {Promise<boolean>} - True if authenticated, false if redirected |
| 12 | + */ |
| 13 | +export async function requireAuth(redirectTo = '/login') { |
| 14 | + try { |
| 15 | + const user = await getCurrentUser(); |
| 16 | + if (!user) { |
| 17 | + // Redirect to login page |
| 18 | + window.location.href = redirectTo; |
| 19 | + return false; |
| 20 | + } |
| 21 | + return true; |
| 22 | + } catch (error) { |
| 23 | + console.error('Auth check failed:', error); |
| 24 | + window.location.href = redirectTo; |
| 25 | + return false; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Check if user is authenticated without redirecting |
| 31 | + * @returns {Promise<boolean>} - True if authenticated |
| 32 | + */ |
| 33 | +export async function isAuthenticated() { |
| 34 | + try { |
| 35 | + const user = await getCurrentUser(); |
| 36 | + return !!user; |
| 37 | + } catch (error) { |
| 38 | + console.error('Auth check failed:', error); |
| 39 | + return false; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Get current user or redirect to login |
| 45 | + * @param {string} redirectTo - Path to redirect to if not authenticated |
| 46 | + * @returns {Promise<Object|null>} - User object or null if redirected |
| 47 | + */ |
| 48 | +export async function getUserOrRedirect(redirectTo = '/login') { |
| 49 | + try { |
| 50 | + const user = await getCurrentUser(); |
| 51 | + if (!user) { |
| 52 | + window.location.href = redirectTo; |
| 53 | + return null; |
| 54 | + } |
| 55 | + return user; |
| 56 | + } catch (error) { |
| 57 | + console.error('Auth check failed:', error); |
| 58 | + window.location.href = redirectTo; |
| 59 | + return null; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Protected route wrapper component |
| 65 | + * This can be used to wrap components that require authentication |
| 66 | + */ |
| 67 | +export function withAuth(WrappedComponent, redirectTo = '/login') { |
| 68 | + return function ProtectedComponent(props) { |
| 69 | + const [isAuth, setIsAuth] = React.useState(null); |
| 70 | + const [loading, setLoading] = React.useState(true); |
| 71 | + |
| 72 | + React.useEffect(() => { |
| 73 | + const checkAuth = async () => { |
| 74 | + const authenticated = await isAuthenticated(); |
| 75 | + setIsAuth(authenticated); |
| 76 | + setLoading(false); |
| 77 | + |
| 78 | + if (!authenticated) { |
| 79 | + window.location.href = redirectTo; |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + checkAuth(); |
| 84 | + }, [redirectTo]); |
| 85 | + |
| 86 | + if (loading) { |
| 87 | + return ( |
| 88 | + <div style={{ |
| 89 | + display: 'flex', |
| 90 | + justifyContent: 'center', |
| 91 | + alignItems: 'center', |
| 92 | + height: '100vh' |
| 93 | + }}> |
| 94 | + <div>Loading...</div> |
| 95 | + </div> |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + if (!isAuth) { |
| 100 | + return null; // Will redirect |
| 101 | + } |
| 102 | + |
| 103 | + return <WrappedComponent {...props} />; |
| 104 | + }; |
| 105 | +} |
0 commit comments