Next.js Discord

Discord Forum

Redirect error

Answered
Allen's Hummingbird posted this in #help-forum
Open in Discord
Allen's HummingbirdOP
⨯ node_modules\next\dist\client\components\redirect.js (58:18) @ getRedirectError
⨯ unhandledRejection: Error: NEXT_REDIRECT
login component
import {loginAction, logoutAction} from "../utils/actions"

export default function LoginButton(){
    return (
        <form action={loginAction}>
            <button>
                SingIn
            </button>
        </form>
    )
}


action.ts file
"use server"
import { signIn, signOut } from "../auth"

export async function loginAction(){
    signIn("github");
}


This error pops in my terminal, how to fix it.
Answered by joulev
How about signIn("…", { callbackUrl: "/protected" })?
View full answer

18 Replies

Allen's HummingbirdOP
Mabye it's my fault that I was not awaiting signIn
export async function loginAction(){
    try{
        await signIn("github");
    } catch (e){
        if(!isRedirectError(e)){
            throw e;
        }
    }
    redirect("/protected");
}


I change my server action to this and no error comes,
Let me know if I am doing right
Allen's HummingbirdOP
you just copied by code ??
😅
Allen's HummingbirdOP
oh
yeah, you made it stop working
😅
@Allen's Hummingbird you just copied by code ??
He removed the negation in front of isRedirectError. It makes all the difference
You are not supposed to catch redirect errors, but you were catching it
Allen's HummingbirdOP
oh
@joulev You are not supposed to catch redirect errors, but you were catching it
Allen's HummingbirdOP
I mean signIn also throws a navigation error and my redirect also throws the same error,
so I should catch the one raised by signIn else my page doesn't change
Answer
Allen's HummingbirdOP
oh
export async function loginAction(){
    return await signIn("github", {redirectTo: "/protected"});
}

These was no callbackUrl in type defination of signIn function,
but
I found redirectTo,
Thanks though, solved in one line 😄