Skip to content

Fix Google OAuth onboarding redirection issue #92

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Aditya30ag
Copy link
Contributor

@Aditya30ag Aditya30ag commented Jul 9, 2025

Fix Google OAuth onboarding redirection issue

📌 Issue Overview

After signing up or logging in with Google, new users are not redirected to the onboarding process as expected. Instead, they are taken directly to the dashboard/home page, even if they have not completed onboarding.

🔧 Changes Made

1. Fixed TypeScript Linter Error

  • Added proper error handling to the checkUserOnboarding function
  • Used Boolean() to ensure hasOnboarding is always a boolean type
  • Added try-catch block for better error handling

2. Updated Authentication State Change Handler

  • Modified the onAuthStateChange listener to check onboarding status after successful sign-in
  • Added logic to redirect users to the appropriate page based on their onboarding status and role:
    • New users/onboarding
    • Existing creators/dashboard
    • Existing brands/brand/dashboard

3. Enhanced Initial Session Check

  • Added onboarding status check during the initial app load
  • Prevents unnecessary redirects when users are already on dashboard/onboarding pages
  • Handles both OAuth and direct navigation scenarios

4. Removed Direct Navigation from Login Function

  • The login() function no longer directly navigates to /dashboard
  • Navigation is now properly handled by the authentication state change listener

🎯 How It Works Now

  1. User signs in with Googlesupabase.auth.signInWithOAuth is called
  2. Supabase triggers auth state changeonAuthStateChange listener is activated
  3. System checks onboarding status → Queries database for user's completion status
  4. Smart redirection happens:
    • If onboarding incomplete → redirects to /onboarding
    • If onboarding complete → redirects to appropriate dashboard

🧪 Testing

  • Fixed TypeScript linter error
  • Updated auth state change handler
  • Enhanced initial session check
  • Removed direct navigation conflicts
  • Google OAuth flow now properly redirects to onboarding

📁 Files Modified

  • Frontend/src/context/AuthContext.tsx - Main authentication context with onboarding logic

Closes #89

Summary by CodeRabbit

  • Bug Fixes

    • Improved error handling during onboarding status checks to prevent unexpected failures.
  • New Features

    • Enhanced onboarding status checks and user redirection based on onboarding completion and user role.
    • Centralized redirection logic ensures users are guided to the appropriate dashboard or onboarding page after sign-in or page load.
  • Chores

    • Refined navigation flow to avoid interfering with password reset and onboarding processes.

- Fixed TypeScript linter error in checkUserOnboarding function
- Updated onAuthStateChange listener to check onboarding status and redirect appropriately
- Added initial session check to handle direct navigation scenarios
- Removed direct navigation from login() function to prevent conflicts
- Users now properly redirected to onboarding when incomplete or dashboard when complete
Copy link
Contributor

coderabbitai bot commented Jul 9, 2025

Walkthrough

The changes introduce error handling and explicit boolean checks in the onboarding status function. The main authentication effect and Supabase auth state listener now both check onboarding status and redirect users accordingly, ensuring new users are routed to onboarding if incomplete. Navigation after login is deferred to these centralized checks.

Changes

File(s) Change Summary
Frontend/src/context/AuthContext.tsx Wrapped onboarding check in try-catch, added explicit boolean logic, centralized onboarding/role-based redirection in useEffect and auth listener, removed direct navigation from login.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant App (AuthContext)
    participant Supabase

    User->>App (AuthContext): Loads app or signs in
    App (AuthContext)->>Supabase: Fetch user & onboarding status
    alt Error or user not onboarded
        App (AuthContext)->>User: Redirect to /onboarding
    else Onboarding complete & role=brand
        App (AuthContext)->>User: Redirect to /brand/dashboard
    else Onboarding complete & other role
        App (AuthContext)->>User: Redirect to /dashboard
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Ensure new Google users are redirected to onboarding if not completed (#89)
Centralize onboarding check and redirection after Google authentication (#89)
Add error handling and debug logging for onboarding status checks (#89)

Poem

A bunny hopped in code today,
To guide new users on their way.
If onboarding’s not yet done,
To /onboarding they must run!
With checks and hops and careful logs,
No dashboard leap for unready frogs.
🐰✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (1)
Frontend/src/context/AuthContext.tsx (1)

84-111: Good error handling, but consider optimizing database queries.

The try-catch wrapper and explicit boolean coercion improve robustness. However, the function makes three sequential database calls that could be optimized.

Consider combining the queries for better performance:

-    try {
-      // Check if user has completed onboarding by looking for social profiles or brand data
-      const { data: socialProfiles } = await supabase
-        .from("social_profiles")
-        .select("id")
-        .eq("user_id", userToUse.id)
-        .limit(1);
-      
-      const { data: brandData } = await supabase
-        .from("brands")
-        .select("id")
-        .eq("user_id", userToUse.id)
-        .limit(1);
-      
-      const hasOnboarding = Boolean((socialProfiles && socialProfiles.length > 0) || (brandData && brandData.length > 0));
-      
-      // Get user role
-      const { data: userData } = await supabase
-        .from("users")
-        .select("role")
-        .eq("id", userToUse.id)
-        .single();
-      
-      return { hasOnboarding, role: userData?.role || null };
+    try {
+      // Get user data and check onboarding status in parallel
+      const [socialResult, brandResult, userResult] = await Promise.all([
+        supabase.from("social_profiles").select("id").eq("user_id", userToUse.id).limit(1),
+        supabase.from("brands").select("id").eq("user_id", userToUse.id).limit(1),
+        supabase.from("users").select("role").eq("id", userToUse.id).single()
+      ]);
+      
+      const hasOnboarding = Boolean(
+        (socialResult.data && socialResult.data.length > 0) || 
+        (brandResult.data && brandResult.data.length > 0)
+      );
+      
+      return { hasOnboarding, role: userResult.data?.role || null };
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3437948 and 645fdec.

📒 Files selected for processing (1)
  • Frontend/src/context/AuthContext.tsx (4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
Frontend/src/context/AuthContext.tsx (1)
Frontend/src/utils/supabase.tsx (1)
  • supabase (11-11)
🔇 Additional comments (2)
Frontend/src/context/AuthContext.tsx (2)

193-213: Consistent redirection logic - good implementation.

The auth state change listener properly handles the SIGNED_IN event with appropriate safeguards for password reset flows.

The logic correctly:

  • Only redirects on SIGNED_IN events
  • Avoids conflicts during password reset
  • Implements the same role-based redirection as the initial check

235-235: Good architectural decision to centralize navigation.

Removing direct navigation from the login function and deferring to the auth state change listener creates a cleaner, more predictable flow.

This change ensures all navigation logic is handled in one place, reducing the chance of conflicts between different navigation triggers.

@Aditya30ag
Copy link
Contributor Author

@Saahi30
you can also review this branch code
as it resolve this Google oauth issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG:New Google Users Sent Directly to Dashboard Instead of Onboarding
1 participant