VER-228 - Handle redirect after logged in#204
Conversation
WalkthroughThe pull request includes modifications across several components to enhance error handling and improve user navigation. Key changes involve adding null checks and optional chaining in the Changes
Possibly related PRs
Suggested reviewers
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Outside diff range and nitpick comments (3)
src/components/LanguageTab.tsx (1)
33-33: Simplify the null checkThe current implementation has redundant null checks. Since
&&already ensuressourceLanguageexists, the optional chaining operator?.is unnecessary.- const isSourceEnglish = sourceLanguage && sourceLanguage?.toLowerCase() === 'english' + const isSourceEnglish = sourceLanguage && sourceLanguage.toLowerCase() === 'english'src/components/LoginPage.tsx (1)
Line range hint
52-58: Consider handling loading state for Google sign-inThe Google sign-in flow should indicate loading state to prevent multiple clicks.
Add loading state management:
const handleGoogleSignIn = async () => { + setIsLoading(true) const { error } = await loginWithGoogle(redirectTo) if (error) { setError('root', { message: error.message }) } + setIsLoading(false) }src/components/PublicHeaderBar.tsx (1)
21-23: Consider adding loading state to login buttonThe login button should indicate when the navigation is in progress.
Add loading state:
-<Button variant='ghost' onClick={handleLogin} className='text-white hover:bg-blue-100 hover:text-blue-700'> +<Button + variant='ghost' + onClick={handleLogin} + className='text-white hover:bg-blue-100 hover:text-blue-700' + disabled={isNavigating} +> - Login + {isNavigating ? 'Redirecting...' : 'Login'} </Button>
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (6)
src/components/LanguageTab.tsx(1 hunks)src/components/LoginPage.tsx(5 hunks)src/components/PublicHeaderBar.tsx(2 hunks)src/components/SnippetAudioPlayer.tsx(0 hunks)src/components/SnippetDetail.tsx(3 hunks)src/providers/auth.tsx(2 hunks)
💤 Files with no reviewable changes (1)
- src/components/SnippetAudioPlayer.tsx
🧰 Additional context used
🪛 eslint
src/components/PublicHeaderBar.tsx
[error] 3-3: Missing file extension for "@/components/ui/button"
(import/extensions)
🔇 Additional comments (4)
src/components/SnippetDetail.tsx (1)
Line range hint 1-447: Implementation looks solid!
The component demonstrates good practices:
- Proper error handling for async operations
- Optimistic updates with rollback
- Clear user feedback through toasts
- Type-safe implementation
src/providers/auth.tsx (1)
9-9: LGTM: Interface update is consistent with implementation
The AuthContextType interface correctly reflects the new optional redirectTo parameter.
src/components/LoginPage.tsx (1)
36-39: LGTM: Proper dependency handling in useEffect
The useEffect hook correctly includes redirectTo in its dependency array, ensuring proper reactivity.
src/components/PublicHeaderBar.tsx (1)
9-12: LGTM: Proper URL handling in login redirect
The handleLogin function correctly handles the redirect URL construction and encoding.
| }) | ||
| const [snippetLanguage, setSnippetLanguage] = useState<string | undefined>( | ||
| snippet?.language?.primary_language.toLowerCase() | ||
| snippet?.language?.primary_language?.toLowerCase() |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Standardize fallback language handling
While the optional chaining is good for safety, the fallback to 'english' is only implemented in one location. Consider standardizing the fallback across all usages for consistency.
- const [snippetLanguage, setSnippetLanguage] = useState<string | undefined>(
- snippet?.language?.primary_language?.toLowerCase()
- )
+ const [snippetLanguage, setSnippetLanguage] = useState<string>(
+ snippet?.language?.primary_language?.toLowerCase() || 'english'
+ )
- setSnippetLanguage(snippet.language?.primary_language?.toLowerCase())
+ setSnippetLanguage(snippet.language?.primary_language?.toLowerCase() || 'english')
sourceLanguage={snippet.language?.primary_language?.toLowerCase() || 'english'}This ensures consistent behavior when the language is undefined and removes the need for undefined in the state type.
Also applies to: 183-183, 411-411
src/providers/auth.tsx
Outdated
| const loginWithGoogle = async (redirectTo?: string): Promise<{ error: AuthError | null }> => { | ||
| try { | ||
| const redirectUrl = (import.meta.env.VITE_AUTH_REDIRECT_URL as string) || '/dashboard' | ||
| const redirectUrl = `${window.location.origin}${redirectTo}` |
There was a problem hiding this comment.
Add URL validation to prevent open redirect vulnerabilities
The current implementation directly concatenates the redirectTo parameter with origin, which could lead to open redirect vulnerabilities if not properly validated.
Consider adding URL validation:
const loginWithGoogle = async (redirectTo?: string): Promise<{ error: AuthError | null }> => {
try {
- const redirectUrl = `${window.location.origin}${redirectTo}`
+ // Validate that redirectTo starts with '/' and only contains allowed paths
+ const validatedPath = redirectTo && /^\/(?:search|snippet\/[\w-]+|onboarding)(?:\?.*)?$/.test(redirectTo)
+ ? redirectTo
+ : '/search'
+ const redirectUrl = `${window.location.origin}${validatedPath}`
const { error } = await supabase.auth.signInWithOAuth({📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const loginWithGoogle = async (redirectTo?: string): Promise<{ error: AuthError | null }> => { | |
| try { | |
| const redirectUrl = (import.meta.env.VITE_AUTH_REDIRECT_URL as string) || '/dashboard' | |
| const redirectUrl = `${window.location.origin}${redirectTo}` | |
| const loginWithGoogle = async (redirectTo?: string): Promise<{ error: AuthError | null }> => { | |
| try { | |
| // Validate that redirectTo starts with '/' and only contains allowed paths | |
| const validatedPath = redirectTo && /^\/(?:search|snippet\/[\w-]+|onboarding)(?:\?.*)?$/.test(redirectTo) | |
| ? redirectTo | |
| : '/search' | |
| const redirectUrl = `${window.location.origin}${validatedPath}` |
| // Extract the redirect path from query parameters | ||
| const params = new URLSearchParams(location.search) | ||
| const redirectTo = params.get('redirect') || '/search' |
There was a problem hiding this comment.
Sanitize URL parameters to prevent XSS attacks
The URL parameter extraction should include sanitization to prevent potential XSS attacks through malicious redirect URLs.
Add URL encoding/decoding:
// Extract the redirect path from query parameters
const params = new URLSearchParams(location.search)
-const redirectTo = params.get('redirect') || '/search'
+const redirectTo = decodeURIComponent(params.get('redirect') || '/search')Committable suggestion skipped: line range outside the PR's diff.
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (1)
src/providers/auth.tsx (1)
81-81: Simplify the conditional expressionThe ternary operator is unnecessary when providing a default value.
-const redirectUrl = `${window.location.origin}${redirectTo ? redirectTo : import.meta.env.VITE_AUTH_REDIRECT_URL}` +const redirectUrl = `${window.location.origin}${redirectTo || import.meta.env.VITE_AUTH_REDIRECT_URL}`🧰 Tools
🪛 eslint
[error] 81-81: Unnecessary use of conditional expression for default assignment.
(no-unneeded-ternary)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
src/providers/auth.tsx(2 hunks)
🧰 Additional context used
🪛 eslint
src/providers/auth.tsx
[error] 81-81: Unnecessary use of conditional expression for default assignment.
(no-unneeded-ternary)
🔇 Additional comments (2)
src/providers/auth.tsx (2)
9-9: LGTM: Interface change maintains backward compatibility
The addition of the optional redirectTo parameter is well-typed and maintains backward compatibility with existing code.
79-81:
Add URL validation to prevent open redirect vulnerabilities
The current implementation directly concatenates the redirectTo parameter with origin, which could lead to open redirect vulnerabilities if not properly validated.
Consider adding URL validation:
const loginWithGoogle = async (redirectTo?: string): Promise<{ error: AuthError | null }> => {
try {
- const redirectUrl = `${window.location.origin}${redirectTo ? redirectTo : import.meta.env.VITE_AUTH_REDIRECT_URL}`
+ // Validate that redirectTo starts with '/' and only contains allowed paths
+ const validatedPath = redirectTo && /^\/(?:search|snippet\/[\w-]+|onboarding)(?:\?.*)?$/.test(redirectTo)
+ ? redirectTo
+ : import.meta.env.VITE_AUTH_REDIRECT_URL
+ const redirectUrl = `${window.location.origin}${validatedPath}`
const { error } = await supabase.auth.signInWithOAuth({🧰 Tools
🪛 eslint
[error] 81-81: Unnecessary use of conditional expression for default assignment.
(no-unneeded-ternary)
Summary by CodeRabbit
Release Notes
New Features
Bug Fixes
Documentation