-
Notifications
You must be signed in to change notification settings - Fork 1
fixing the convex error #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fde761a
fixing the convex error
Jackson57279 666c278
making sure that clerk is working
Jackson57279 79bf603
trying to make sure that our fucking auth will fucking work
Jackson57279 5dca1aa
I broke my auth on vite somehow
Jackson57279 7c2ef3d
Making auth work
Jackson57279 40f9e66
I am a retard
Jackson57279 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import React, { createContext, useContext, useEffect, useState, useCallback } from 'react'; | ||
| import { useAuth as useClerkAuth, useUser } from '@clerk/clerk-react'; | ||
| import { AuthCookies } from '@/lib/auth-cookies'; | ||
|
|
||
| interface AuthContextType { | ||
| isAuthenticated: boolean; | ||
| isLoading: boolean; | ||
| user: unknown; | ||
| token: string | null; | ||
| refreshAuth: () => Promise<void>; | ||
| } | ||
|
|
||
| const AuthContext = createContext<AuthContextType | undefined>(undefined); | ||
|
|
||
| export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { | ||
| const { getToken, isSignedIn, isLoaded } = useClerkAuth(); | ||
| const { user } = useUser(); | ||
| const [token, setToken] = useState<string | null>(null); | ||
| const [isLoading, setIsLoading] = useState(true); | ||
|
|
||
| const refreshAuth = useCallback(async () => { | ||
| setIsLoading(true); | ||
| try { | ||
| if (isSignedIn && isLoaded) { | ||
| const newToken = await getToken(); | ||
| if (newToken) { | ||
| setToken(newToken); | ||
| AuthCookies.set(newToken); | ||
| } | ||
| } else { | ||
| setToken(null); | ||
| AuthCookies.remove(); | ||
| } | ||
| } catch (error) { | ||
| console.error('Failed to refresh auth token:', error); | ||
| // Try to use cached token if available | ||
| const cachedToken = AuthCookies.get(); | ||
| if (cachedToken && AuthCookies.isValid()) { | ||
| setToken(cachedToken); | ||
| } else { | ||
| setToken(null); | ||
| AuthCookies.remove(); | ||
| } | ||
| } finally { | ||
| setIsLoading(false); | ||
| } | ||
| }, [isSignedIn, isLoaded, getToken]); | ||
|
|
||
| useEffect(() => { | ||
| refreshAuth(); | ||
| }, [isSignedIn, isLoaded, user?.id, refreshAuth]); | ||
|
|
||
| // Periodic token refresh to prevent expiration | ||
| useEffect(() => { | ||
| if (isSignedIn) { | ||
| const interval = setInterval(() => { | ||
| refreshAuth(); | ||
| }, 4 * 60 * 1000); // Refresh every 4 minutes | ||
|
|
||
| return () => clearInterval(interval); | ||
| } | ||
| }, [isSignedIn, refreshAuth]); | ||
|
|
||
| // Initialize token from cookie on app start | ||
| useEffect(() => { | ||
| const cachedToken = AuthCookies.get(); | ||
| if (cachedToken && AuthCookies.isValid() && !token) { | ||
| setToken(cachedToken); | ||
| } | ||
| setIsLoading(false); | ||
| }, [token]); | ||
|
|
||
| const value: AuthContextType = { | ||
| isAuthenticated: isLoaded && isSignedIn && !!token, | ||
| isLoading: !isLoaded || isLoading, | ||
| user, | ||
| token, | ||
| refreshAuth, | ||
| }; | ||
|
|
||
| return ( | ||
| <AuthContext.Provider value={value}> | ||
| {children} | ||
| </AuthContext.Provider> | ||
| ); | ||
| }; | ||
|
|
||
| export const useAuthContext = () => { | ||
| const context = useContext(AuthContext); | ||
| if (context === undefined) { | ||
| throw new Error('useAuthContext must be used within an AuthProvider'); | ||
| } | ||
| return context; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import React, { useEffect } from 'react'; | ||
| import { useConvexAuth } from 'convex/react'; | ||
| import { useAuth as useClerkAuth } from '@clerk/clerk-react'; | ||
| import { AuthCookies, useAuthCookies } from '@/lib/auth-cookies'; | ||
|
|
||
| interface AuthWrapperProps { | ||
| children: React.ReactNode; | ||
| } | ||
|
|
||
| export const AuthWrapper: React.FC<AuthWrapperProps> = ({ children }) => { | ||
| const convexAuth = useConvexAuth(); | ||
| const clerkAuth = useClerkAuth(); | ||
| const { getStoredToken, clearToken } = useAuthCookies(); | ||
|
|
||
| useEffect(() => { | ||
| // Handle authentication recovery on page load/refresh | ||
| const handleAuthRecovery = async () => { | ||
| // If Convex shows not authenticated but we have a valid cookie token | ||
| if (!convexAuth.isAuthenticated && !convexAuth.isLoading) { | ||
| const storedToken = getStoredToken(); | ||
|
|
||
| if (storedToken && AuthCookies.isValid()) { | ||
| // Try to refresh Clerk session if needed | ||
| try { | ||
| if (clerkAuth.isSignedIn) { | ||
| const freshToken = await clerkAuth.getToken({ skipCache: true }); | ||
| if (freshToken) { | ||
| AuthCookies.set(freshToken); | ||
| // Let Convex naturally re-authenticate without forcing reload | ||
| console.log('Auth token refreshed, waiting for Convex sync'); | ||
| } | ||
| } | ||
| } catch (error) { | ||
| console.warn('Auth recovery failed:', error); | ||
| clearToken(); | ||
| } | ||
| } else if (storedToken) { | ||
| // Remove invalid token | ||
| clearToken(); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // Run recovery after initial auth check | ||
| const timeout = setTimeout(handleAuthRecovery, 1000); | ||
| return () => clearTimeout(timeout); | ||
| }, [convexAuth.isAuthenticated, convexAuth.isLoading, clerkAuth.isSignedIn, getStoredToken, clearToken, clerkAuth]); | ||
|
|
||
| // Handle sign out cleanup | ||
| useEffect(() => { | ||
| if (!clerkAuth.isSignedIn && clerkAuth.isLoaded) { | ||
| clearToken(); | ||
| } | ||
| }, [clerkAuth.isSignedIn, clerkAuth.isLoaded, clearToken]); | ||
|
|
||
| return <>{children}</>; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { useAuth as useClerkAuth } from '@clerk/clerk-react'; | ||
| import { useEffect } from 'react'; | ||
|
|
||
| // Cookie utilities for auth token management | ||
| export const AuthCookies = { | ||
| TOKEN_KEY: 'clerk_session_token', | ||
|
|
||
| set(token: string, expiresInDays = 7) { | ||
| const expires = new Date(); | ||
| expires.setTime(expires.getTime() + (expiresInDays * 24 * 60 * 60 * 1000)); | ||
| document.cookie = `${this.TOKEN_KEY}=${token}; expires=${expires.toUTCString()}; path=/; secure; samesite=strict`; | ||
| }, | ||
|
|
||
| get(): string | null { | ||
| const cookies = document.cookie.split(';'); | ||
| for (const cookie of cookies) { | ||
| const [name, value] = cookie.trim().split('='); | ||
| if (name === this.TOKEN_KEY) { | ||
| return decodeURIComponent(value); | ||
| } | ||
| } | ||
| return null; | ||
| }, | ||
|
|
||
| remove() { | ||
| document.cookie = `${this.TOKEN_KEY}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`; | ||
| }, | ||
|
|
||
| isValid(): boolean { | ||
| const token = this.get(); | ||
| if (!token) return false; | ||
|
|
||
| try { | ||
| // Basic JWT validation - check if it's properly formatted | ||
| const parts = token.split('.'); | ||
| if (parts.length !== 3) return false; | ||
|
|
||
| // Decode payload to check expiration | ||
| const payload = JSON.parse(atob(parts[1])); | ||
| const now = Math.floor(Date.now() / 1000); | ||
|
|
||
| return payload.exp > now; | ||
| } catch (error) { | ||
| console.warn('Invalid token format:', error); | ||
| return false; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // Hook to manage auth cookies automatically | ||
| export const useAuthCookies = () => { | ||
| const { getToken, isSignedIn } = useClerkAuth(); | ||
|
|
||
| useEffect(() => { | ||
| const syncToken = async () => { | ||
| if (isSignedIn) { | ||
| try { | ||
| const token = await getToken(); | ||
| if (token) { | ||
| AuthCookies.set(token); | ||
| } | ||
| } catch (error) { | ||
| console.error('Failed to get or set auth token:', error); | ||
| } | ||
| } else { | ||
| AuthCookies.remove(); | ||
| } | ||
| }; | ||
|
|
||
| syncToken(); | ||
|
|
||
| // Set up periodic token refresh | ||
| const interval = setInterval(syncToken, 5 * 60 * 1000); // Every 5 minutes | ||
|
|
||
| return () => clearInterval(interval); | ||
| }, [isSignedIn, getToken]); | ||
|
|
||
| return { | ||
| getStoredToken: () => AuthCookies.get(), | ||
| isTokenValid: () => AuthCookies.isValid(), | ||
| clearToken: () => AuthCookies.remove() | ||
| }; | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / ESLint
Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components. Warning