#Redirect not work in try and catch for async actions

17 messages · Page 1 of 1 (latest)

trim temple
#

I'm using an async action function under "use server" directive term, this function handles the login process for the user. I wanna redirect the user if the credentials right to another route for example the /dashboard route, so I should insert the redirect('/dashboard') into the try wrapper, but it not working until I add it to out of the try and catch blocks.
is there another way to handle this process?

actions.ts

export async function handleSignIn(
  username: string,
  password: string,
  remeberMe: boolean
) {

  const FD: FormData = new FormData();
  FD.append("UserName", username);
  FD.append("Password", password);
  try {
    const res = await http.post(endpoints.auth.signin, FD);

    const token = await res.data.data.accessToken;
    const refreshToken = await res.data.data.refreshToken;
    if (token) {
      cookies().set("token", token, { path: "/", domain: "localhost" });
      cookies().set("refresh-token", JSON.stringify(refreshToken));
      console.log("token saved");
    }

    console.log("successfully logined");

    revalidatePath("/dashboard");
    redirect("/dashboard");
  } catch (error) {
    console.log(error);
  }
  //!NOTE: if I added the redirect in this scope it works fine, but it redirect the user even if his credentials wrong
  // revalidatePath("/dashboard");
  // redirect("/dashboard");
}
terse raftBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

mild blade
#

you should check if it is redirect error with isRedirectError and rethrow the error

trim temple
mild blade
trim temple
#

??????????? is that the solution?

#

it works

#

how it comes?

mild blade
#

you rethrow the error when it is redirect error which nextjs knows how to handle internally

#

if you catch it, there's no "NEXT_REDIRECT" error for nextjs to handle as it is handled by your catch block

trim temple
#

thank you man

#

you saved me

#

I've over 10 posts this week in the @help-forum about server ,client and router issues, am I a dump or Nextjs concept is different?

terse raftBOT
#
✅ Success!

This question has been marked as answered! If you have any other questions, feel free to create another post

Jump to answer

[Click here](#1277630953262415904 message)

#
✅ Success!

This question has been marked as answered! If you have any other questions, feel free to create another post

Jump to answer

[Click here](#1277630953262415904 message)

mild blade
#

the new router is hard to wrap your around me, even I have problems sometime