Replies: 1 comment 3 replies
-
You're trying to do it right in your SignIn.js component. If that's the case you can handle the redirection yourself: //other imports
import { getSession, signIn, signOut } from "next-auth/react";
import { useRouter } from "next/navigation";
const Login = () => {
//other logic
const route = useRouter();
const formik = useFormik({
//other logic
onSubmit: async (values) => {
setIsLoading(true);
// Handle form submission logic here
// console.log("Form submitted:", values);
const res = await signIn("credentials", {
email: values.email,
password: values.password,
//Set redirect to false to handle it yourself
redirect: false,
});
const session = await getSession();
//Redirect based on the role of the user
route.push(`/${session.user.role === "user" ? "user" : "admin" }`) //This is just for illustration, you can approach it as you want, with a switch, if conditions, etc.
//rest of the logic Hope it helps! |
Beta Was this translation helpful? Give feedback.
-
Hi Devs,
I am working on a project where I need to handle redirection based on multiple roles after login. Specifically, I want to redirect users to different pages depending on their roles. For example, if the role is user, the user should be redirected to the user page, if the role is admin, they should be redirected to the admin page, and so on.
Here's my project URL: https://github.com/walifile/nextjs-nextauth-kit
I would appreciate any guidance or examples on how to implement this functionality effectively.
This is a part of my login function:
const res = await signIn("credentials", { email: values.email, password: values.password, redirect: true, callbackUrl: "/user", });
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions