Skip to content

[BUG] Fix: Use useRouter instead of redirect in UserContext client component #83

Description

@Caktusuki

Browser

Brave

Operating System

Windows 11

What happened?

The application threw an error when trying to log in or log out. The issue occurred because the redirect() function from next/navigation was used inside a client component (UserContext.tsx). Since redirect() only works in server components, this caused runtime errors and prevented proper navigation.

What I expected to happen:
The app should log users in and out smoothly and navigate to the appropriate pages without errors.

Additional context:
Replacing redirect() with the Next.js useRouter() hook and calling router.push('/') fixed the issue. This ensures client-side navigation works as expected.

Proposed fix:

Remove the redirect import from next/navigation.
Import and initialize useRouter().
Update the logout function to use router.push('/') instead of redirect('/').

Steps to Reproduce

1.Start the development server using npm run dev or yarn dev.
2.Open the app in the Brave browser.
3.Log in to your account (or use any test credentials).
4.Attempt to log out from the dashboard.
5.Observe the console or browser error message.

Relevant log output

Unhandled Runtime Error: Error: redirect() is not supported in client components.
    at redirect (next/navigation)
    at logout (UserContext.tsx:45)
    at onClick (LogoutButton.tsx:12)

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions