-
Notifications
You must be signed in to change notification settings - Fork 1
Fixing errors #48
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
Merged
Merged
Fixing errors #48
Changes from all commits
Commits
Show all changes
9 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 98ca211
Fixing security mistakes
Jackson57279 7ef9b9e
Fixing the conflict error
Jackson57279 4b79f3c
Resolve merge conflict in convex/rateLimit.ts - keep more aggressive …
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
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,73 @@ | ||
| import React, { Component, ErrorInfo, ReactNode } from 'react'; | ||
|
|
||
| interface Props { | ||
| children: ReactNode; | ||
| fallback?: ReactNode; | ||
| } | ||
|
|
||
| interface State { | ||
| hasError: boolean; | ||
| error?: Error; | ||
| } | ||
|
|
||
| export class AuthErrorBoundary extends Component<Props, State> { | ||
| constructor(props: Props) { | ||
| super(props); | ||
| this.state = { hasError: false }; | ||
| } | ||
|
|
||
| static getDerivedStateFromError(error: Error): State { | ||
| return { hasError: true, error }; | ||
| } | ||
|
|
||
| componentDidCatch(error: Error, errorInfo: ErrorInfo) { | ||
| // Log error without exposing sensitive auth details | ||
| console.error('Authentication error caught by boundary:', { | ||
| message: error.message, | ||
| stack: error.stack, | ||
| componentStack: errorInfo.componentStack | ||
| }); | ||
| } | ||
|
|
||
| render() { | ||
| if (this.state.hasError) { | ||
| return this.props.fallback || ( | ||
| <div className="flex items-center justify-center min-h-screen bg-gray-50"> | ||
| <div className="text-center"> | ||
| <div className="mb-4"> | ||
| <div className="inline-flex items-center justify-center w-16 h-16 bg-red-100 rounded-full"> | ||
| <svg | ||
| className="w-8 h-8 text-red-600" | ||
| fill="none" | ||
| stroke="currentColor" | ||
| viewBox="0 0 24 24" | ||
| > | ||
| <path | ||
| strokeLinecap="round" | ||
| strokeLinejoin="round" | ||
| strokeWidth={2} | ||
| d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L3.732 16.5c-.77.833.192 2.5 1.732 2.5z" | ||
| /> | ||
| </svg> | ||
| </div> | ||
| </div> | ||
| <h2 className="text-xl font-semibold text-gray-900 mb-2"> | ||
| Authentication Error | ||
| </h2> | ||
| <p className="text-gray-600 mb-4"> | ||
| There was a problem with authentication. Please refresh the page to try again. | ||
| </p> | ||
| <button | ||
| onClick={() => window.location.reload()} | ||
| className="inline-flex items-center px-4 py-2 bg-blue-600 text-white text-sm font-medium rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500" | ||
| > | ||
| Refresh Page | ||
| </button> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return this.props.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import React, { createContext, useContext, useEffect, useState, useCallback } from 'react'; | ||
| import { useAuth as useClerkAuth, useUser } from '@clerk/clerk-react'; | ||
| import type { UserResource } from '@clerk/types'; | ||
| import { useAuthToken } from '@/lib/auth-token'; | ||
|
|
||
| interface AuthContextType { | ||
| isAuthenticated: boolean; | ||
| isLoading: boolean; | ||
| user: UserResource | null | undefined; | ||
| token: string | null; | ||
| refreshAuth: () => Promise<void>; | ||
| } | ||
|
|
||
| const AuthContext = createContext<AuthContextType | undefined>(undefined); | ||
|
|
||
| export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { | ||
| const { isSignedIn, isLoaded } = useClerkAuth(); | ||
| const { user } = useUser(); | ||
| const { getValidToken, clearStoredToken } = useAuthToken(); | ||
| const [token, setToken] = useState<string | null>(null); | ||
| const [isLoading, setIsLoading] = useState(true); | ||
|
|
||
| const refreshAuth = useCallback(async () => { | ||
| try { | ||
| if (isSignedIn && isLoaded) { | ||
| const newToken = await getValidToken(); | ||
| setToken(newToken); | ||
| } else { | ||
| setToken(null); | ||
| clearStoredToken(); | ||
| } | ||
| } catch (error) { | ||
| console.error('Failed to refresh auth token'); | ||
| setToken(null); | ||
| clearStoredToken(); | ||
| } | ||
| }, [isSignedIn, isLoaded, getValidToken, clearStoredToken]); | ||
|
|
||
| // Main auth effect - handles initial load and auth state changes | ||
| useEffect(() => { | ||
| const handleAuth = async () => { | ||
| if (!isLoaded) { | ||
| // Still loading Clerk, keep loading state | ||
| return; | ||
| } | ||
|
|
||
| setIsLoading(true); | ||
| await refreshAuth(); | ||
| setIsLoading(false); | ||
| }; | ||
|
|
||
| handleAuth(); | ||
| }, [isSignedIn, isLoaded, user?.id, refreshAuth]); | ||
|
|
||
| // Periodic token refresh to prevent expiration | ||
| useEffect(() => { | ||
| if (isSignedIn && isLoaded) { | ||
| const interval = setInterval(refreshAuth, 4 * 60 * 1000); // Every 4 minutes | ||
| return () => clearInterval(interval); | ||
| } | ||
| }, [isSignedIn, isLoaded, refreshAuth]); | ||
|
|
||
| const value: AuthContextType = { | ||
| isAuthenticated: isLoaded && isSignedIn && !!token, | ||
| isLoading: isLoading || !isLoaded, | ||
| 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,47 @@ | ||
| import React, { useEffect } from 'react'; | ||
| import { useConvexAuth } from 'convex/react'; | ||
| import { useAuth as useClerkAuth } from '@clerk/clerk-react'; | ||
| import { useAuthToken } from '@/lib/auth-token'; | ||
|
|
||
| interface AuthWrapperProps { | ||
| children: React.ReactNode; | ||
| } | ||
|
|
||
| export const AuthWrapper: React.FC<AuthWrapperProps> = ({ children }) => { | ||
| const convexAuth = useConvexAuth(); | ||
| const clerkAuth = useClerkAuth(); | ||
| const { getValidToken, clearStoredToken } = useAuthToken(); | ||
|
|
||
| useEffect(() => { | ||
| // Handle authentication recovery on page load/refresh | ||
| const handleAuthRecovery = async () => { | ||
| // If Convex shows not authenticated but Clerk is signed in | ||
| if (!convexAuth.isAuthenticated && !convexAuth.isLoading && clerkAuth.isSignedIn && clerkAuth.isLoaded) { | ||
| try { | ||
| // Get a fresh token to sync with Convex | ||
| const freshToken = await getValidToken(); | ||
| if (freshToken) { | ||
| // Let Convex naturally re-authenticate without forcing reload | ||
| console.log('Auth token refreshed for Convex sync'); | ||
| } | ||
| } catch (error) { | ||
| console.error('Auth recovery failed'); | ||
| clearStoredToken(); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // Run recovery after initial auth check | ||
| const timeout = setTimeout(handleAuthRecovery, 1000); | ||
| return () => clearTimeout(timeout); | ||
| }, [convexAuth.isAuthenticated, convexAuth.isLoading, clerkAuth.isSignedIn, clerkAuth.isLoaded, getValidToken, clearStoredToken]); | ||
|
|
||
| // Handle sign out cleanup | ||
| useEffect(() => { | ||
| if (!clerkAuth.isSignedIn && clerkAuth.isLoaded) { | ||
| clearStoredToken(); | ||
| } | ||
| }, [clerkAuth.isSignedIn, clerkAuth.isLoaded, clearStoredToken]); | ||
|
|
||
| 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,42 @@ | ||
| import { useAuth as useClerkAuth } from '@clerk/clerk-react'; | ||
| import { useConvexAuth } from 'convex/react'; | ||
| import { useAuthToken } from './auth-token'; | ||
|
|
||
| // Enhanced auth hook that combines Clerk and Convex auth with secure token management | ||
| export const useAuthWithTokens = () => { | ||
| const clerkAuth = useClerkAuth(); | ||
| const convexAuth = useConvexAuth(); | ||
| const { getValidToken, clearStoredToken } = useAuthToken(); | ||
|
|
||
| // Enhanced getToken that uses secure memory-based storage | ||
| const getTokenSecure = async () => { | ||
| try { | ||
| // Get token using the secure token manager | ||
| if (clerkAuth.isSignedIn && clerkAuth.isLoaded) { | ||
| return await getValidToken(); | ||
| } | ||
|
|
||
| return null; | ||
| } catch (error) { | ||
| console.error('Error getting auth token'); | ||
| clearStoredToken(); | ||
| return null; | ||
| } | ||
| }; | ||
|
|
||
| // Clear tokens on sign out | ||
| const signOut = async () => { | ||
| clearStoredToken(); | ||
| if (clerkAuth.signOut) { | ||
| await clerkAuth.signOut(); | ||
| } | ||
| }; | ||
|
|
||
| return { | ||
| ...clerkAuth, | ||
| ...convexAuth, | ||
| getToken: getTokenSecure, | ||
| signOut, | ||
| isFullyAuthenticated: convexAuth.isAuthenticated && clerkAuth.isSignedIn, | ||
| }; | ||
| }; |
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