Redirect dose NOT work in server action
Answered
Woodwasp posted this in #help-forum
WoodwaspOP
hello there, I created a server action for signup using authjs library. I'm trying to redirect user base on his role after logged in but redirect function doesn't redirect.
} catch (err) {
if (err instanceof AuthError) {
switch (err.type) {
case "CredentialsSignin":
return { isError: true, message: "email or password is invalid" };
case "CallbackRouteError":
return { isError: true, message: "email or password in incurrect" };
default:
return { isError: true, message: err.message };
}
}
}
return { isError: false, message: "LoggedIn. Please refresh" };
};
``"use server";<:next1:1065672519144714322>
import { signIn } from "@/auth";
import { LoginFormSchema } from "@/types/Form/FormSchemas";
import { AuthError } from "next-auth";
import { redirect } from "next/navigation";
import { z } from "zod";
export const LoginAction = async (
data: z.infer<typeof LoginFormSchema>
): Promise<{ isError: boolean; message: string }> => {
const validateData = LoginFormSchema.safeParse(data);
if (!validateData.success) {
return { isError: true, message: "Credentials is Invalid" };
}
const { password, email } = validateData.data;
try {
await signIn("credentials", {
email,
password,
});
//! rediect dose NOT work
redirect(/dashboard/user`);} catch (err) {
if (err instanceof AuthError) {
switch (err.type) {
case "CredentialsSignin":
return { isError: true, message: "email or password is invalid" };
case "CallbackRouteError":
return { isError: true, message: "email or password in incurrect" };
default:
return { isError: true, message: err.message };
}
}
}
return { isError: false, message: "LoggedIn. Please refresh" };
};
9 Replies
Answer
check good to know second point
@James4u redirect doesn't work inside try catch
WoodwaspOP
Why?
yr welcome! just mark solution if it helped you