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");
}