New to Next.js and backend in general. Trying to redirect to home page when user successfully login
Unanswered
American Sable posted this in #help-forum
American SableOP
I have set up my auth in supabase correctly and followed all the steps in their documentation here: https://supabase.com/docs/guides/auth/server-side/nextjs?queryGroups=router&router=app
But I can't figure out how to redirect the user to / when the authentication is successful. Currently my login function as
The problem is, my middleware function calls
This is copied from the supabase documentation. The getUser() function is returning a null when my login function should have authenticated a user. My redirect sends me to the "/", but I instantly get sent back to the "/login" page by my middleware because it doesn't detect a user. I assume this is because of a mismatch between the SSR and client-side, but I'm pretty new to Next.js and backend in general, so I'm having trouble figuring out how to ensure that the client and server's cookies match.
But I can't figure out how to redirect the user to / when the authentication is successful. Currently my login function as
type userData = {
email: string;
password: string;
};
export async function login(userData: userData): Promise<void | Error> {
const { error } = await supabase.auth.signInWithPassword(userData);
if (error) {
return error;
}
revalidatePath("/", "layout");
redirect("/");
}The problem is, my middleware function calls
const {
data: { user },
} = await supabase.auth.getUser();
if (
!user &&
!request.nextUrl.pathname.startsWith("/login") &&
!request.nextUrl.pathname.startsWith("/auth")
) {
// no user, potentially respond by redirecting the user to the login page
const url = request.nextUrl.clone();
url.pathname = "/login";
return NextResponse.redirect(url);
}This is copied from the supabase documentation. The getUser() function is returning a null when my login function should have authenticated a user. My redirect sends me to the "/", but I instantly get sent back to the "/login" page by my middleware because it doesn't detect a user. I assume this is because of a mismatch between the SSR and client-side, but I'm pretty new to Next.js and backend in general, so I'm having trouble figuring out how to ensure that the client and server's cookies match.