Skip to content

CallbackClient's redirectForRoles(user?.roles) reads a stale pre-login closure value, likely still always redirecting new sign-ins to /dashboard/contributor (#77 regression) #413

Closed

Description

@abayomicornelius

Problem

src/app/auth/callback/CallbackClient.tsx was updated to close #77 ("CallbackClient always redirects to /dashboard/contributor after GitHub OAuth; AuthUser.roles is fetched but never read anywhere") by adding role-aware redirect logic:

useEffect(() => {
  const token = searchParams.get("token");
  if (!token) { setError(...); return; }

  window.history.replaceState({}, "", window.location.pathname);

  login(token)
    .then(() => {
      router.replace(redirectForRoles(user?.roles));
    })
    .catch(() => setError("Could not complete sign-in. Please try again."));
}, [searchParams, login, router, user]);

user comes from useAuth() (AuthContext), and AuthContext.login() is persistToken(token); await refresh(); — it resolves Promise<void>, it does not return the freshly-fetched user. The .then(() => router.replace(redirectForRoles(user?.roles))) callback is a closure over whatever user was at the time this particular effect invocation was created — which, for someone landing on /auth/callback to complete their first sign-in, is null (they aren't authenticated yet). login()'s own internal refresh() call is what eventually calls setUser(profile) with the real, role-bearing profile — but that state update happens after this closure was already created and captured user = null.

So on the effect's first pass, redirectForRoles(user?.roles) evaluates with roles as undefined, which redirectForRoles explicitly falls back to ROLE_REDIRECT_MAP.contributor for — the exact "always contributor" behavior #77 was filed to fix, just now hidden behind a role-precedence map that looks correct but never actually receives the resolved roles on the pass that matters.

Compounding this: user is listed in this effect's own dependency array, so once login()'s refresh() call does update user, the effect re-runs. On that second run, searchParams.get("token") is read again — useSearchParams() does not reactively reflect the raw window.history.replaceState() call from the first run (that API doesn't trigger Next.js's router-state update), so token is likely still truthy, meaning login(token) can fire a second time with the same token, triggering another redundant /auth/me + /users/:id round-trip and a second router.replace(...) call.

Impact

Every first-time OAuth sign-in is at real risk of landing on /dashboard/contributor regardless of the user's actual role(s) — the specific bug #77 is titled after — because the redirect decision is made from a stale, pre-login user value. On top of that, the same effect's dependency on user creates a realistic path to a redundant second login() call and a second navigation, which at best wastes two network round-trips and at worst produces a visible flash to the wrong dashboard followed by a second redirect (or, if searchParams behaves differently than expected on the re-run, an "No token was returned" error flashing immediately after a successful sign-in).

Suggested fix

Have AuthContext.login() return the resolved AuthUser (or resolve refresh()'s result) so CallbackClient can redirect based on the value login() actually just fetched, rather than reading a separate, independently-updating user from context:

// AuthContext.tsx
const login = useCallback(async (token: string): Promise<AuthUser | null> => {
  persistToken(token);
  return refresh(); // have refresh() return the resolved user (or null)
}, [refresh]);
// CallbackClient.tsx
login(token)
  .then((resolvedUser) => {
    router.replace(redirectForRoles(resolvedUser?.roles));
  })
  .catch(...);

and drop user from this effect's dependency array (it's no longer needed once the redirect reads login()'s own resolved value), which also removes the redundant-second-run risk.

Activity

  1. shadrach68 commented on Sep 17, 2026

    @shadrach68

    Hi Maintainer,
    I'd like to take on this issue. I've reviewed the requirements, understand the scope, and can deliver a tested solution that aligns with the project's standards.

    Thanks.

  2. ranjeet150 commented on Sep 17, 2026

    @ranjeet150

    Hello @maintainers,

    I can tackle CallbackClient's redirectForRoles(user?.roles) reads a stale pre-login closure value, likely still always redirecting new sign-ins to /dashboard/contributor (#77 regression). Proposed implementation roadmap:

    • Analysis: Inspect current behavior and edge cases in MergeFi/frontend
    • Core Implementation: Deliver CallbackClient's redirectForRoles(user?.roles) reads a stale pre-login closure value, likely still always redirecting new sign-ins to /dashboard/contributor (#77 regression) with proper loading/error states and clean component architecture.
    • Quality Assurance: Test across viewport breakpoints and ensure 0 TypeScript or linting errors.

    Happy to pick this up today—please assign to me!

  3. added
    bug
    on Sep 17, 2026
  4. added
    Stellar Wave
    on Sep 24, 2026
  5. Eeboade commented on Sep 24, 2026

    @Eeboade

    @Eeboade has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

    Hi, kindly assign, i would love to fix

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @Eeboade to this issue.

  6. innocentharuna commented on Sep 24, 2026

    @innocentharuna

    @innocentharuna has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

    hey maintainer, please kindly assign this to me, i can surely fix it

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @innocentharuna to this issue.

  7. VeronicDev commented on Sep 24, 2026

    @VeronicDev

    @VeronicDev has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

    Hello Maintainer, I can fix this please assign.

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @VeronicDev to this issue.

  8. Confidence001Jewel-cmd commented on Sep 24, 2026

    @Confidence001Jewel-cmd

    @Confidence001Jewel-cmd has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

    Dear Maintainer,

    I would like to work on this issue. I have experience with React/Next.js authentication flows, TypeScript, and API-driven user state.

    For this fix, I will update AuthContext.login() to return the resolved AuthUser from refresh(), allowing CallbackClient to perform role-based redirects using the freshly fetched user instead of stale context state. I will also remove user from the callback effect dependencies to prevent redundant login calls and navigation. I will add or update tests to verify role-based redirects and ensure the OAuth callback only processes the token once.

    Thank you for considering my proposal. I look forward to contributing to the project.

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @Confidence001Jewel-cmd to this issue.

  9. stpatrickghost commented on Sep 24, 2026

    @stpatrickghost

    @stpatrickghost has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

    Please maintainer... Assign me this issue.

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @stpatrickghost to this issue.

  10. Noragiftfr commented on Sep 24, 2026

    @Noragiftfr
    Contributor

    @Noragiftfr has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

    Hi Maintainer, I’d like to handle this

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @Noragiftfr to this issue.

  11. drips-wave commented on Sep 24, 2026

    @drips-wave

    Congratulations, @Noragiftfr! 🎉 Your application was accepted by the repo's maintainers, and the issue is due on September 30, 2026.

    🧑‍💻 @Noragiftfr: Please resolve the issue such that the repo's maintainers have enough time to review your contribution before the due date. You'll earn Points for completing the issue on-time, which will make you eligible for a share of the Stellar Wave Program's reward pool.

    Warning

    When opening a PR, please link it to this issue to ensure it gets tracked accurately. Points are awarded when this issue is marked as completed by the maintainer.

    🤠 Repo maintainers: Please keep an eye on the contributor's progress and review their work before the due date. You can manage this issue, including adjusting its complexity and points, here.

    🌊 Happy Wave 🌊

  12. added 2 commits that reference this issue on Sep 26, 2026
  13. drips-wave commented on Sep 26, 2026

    @drips-wave

    This issue has been completed by @Noragiftfr as part of the Stellar Wave Program's 9th Wave 🥳

    😎 @Noragiftfr: You earned 200 Points for completing this issue! After the current Wave ends, you'll be eligible for a percentage of the Wave's reward pool based on the percentage of total points you've earned. Learn more here. You can also Leave a review to share your experience working on this issue.

    🧑‍💻 Repo maintainers: How'd the contributor do? Leave a review to share your experience working with them.

  14. removed
    Stellar Wave
    on Oct 15, 2026
  15. added
    Stellar Wave
    on Oct 15, 2026
  16. removed
    Stellar Wave
    on Oct 15, 2026
  17. added
    Stellar Wave
    on Oct 15, 2026
  18. removed
    Stellar Wave
    on Oct 15, 2026
  19. added
    Stellar Wave
    on Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Stellar Wavebug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions