Redirect error
Answered
Allen's Hummingbird posted this in #help-forum
Allen's HummingbirdOP
⨯ node_modules\next\dist\client\components\redirect.js (58:18) @ getRedirectError
⨯ unhandledRejection: Error: NEXT_REDIRECT
This error pops in my terminal, how to fix it.
⨯ unhandledRejection: Error: NEXT_REDIRECT
login componentimport {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.
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 Hummingbird ts
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
export async function loginAction(){
try{
await signIn("github");
redirect("/protected");
} catch (e){
if(isRedirectError(e)){
throw e;
}
}
}Allen's HummingbirdOP
you just copied by code ??
😅
Allen's HummingbirdOP
oh
yeah, you made it stop working
😅
@Allen's Hummingbird ts
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
this makes it work correct
@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
so I should catch the one raised by
signIn else my page doesn't change@Allen's Hummingbird 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
How about signIn("…", { callbackUrl: "/protected" })?
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 😄
@Allen's Hummingbird ts
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 😄
Ohh, pretty sure it’s called callbackUrl in v4, but yeah they probably renamed it. Yup that’s how you are expected to redirect when signing in