Redirect user after oAuth login
Unanswered
Borsoon posted this in #help-forum
BorsoonOP
I'm trying to make a basic login page, where you have the option to login with google or github. The problem is, that after logging in with Google / Github, I get redirected back to the login page, instead of the / page (localhost:3000/).
The reason this is happening, is because I have a middleware file that keeps the user out of the website before logging in. But right after authenticating with Oauth, the user is still not authenticated, so it pushes him back to the login page (I checked with console log).
After that, if you try to change the url yourself, then you are allowed to do so. If I try the default middleware it works, but the user is allowed to browse the site freely without logging in. (I use supabase as a place for the Oauth keys etc.)
1st photo: before login
2nd photo: after login (with google)
3rd photo: updated middleware
4th photo: default
The reason this is happening, is because I have a middleware file that keeps the user out of the website before logging in. But right after authenticating with Oauth, the user is still not authenticated, so it pushes him back to the login page (I checked with console log).
After that, if you try to change the url yourself, then you are allowed to do so. If I try the default middleware it works, but the user is allowed to browse the site freely without logging in. (I use supabase as a place for the Oauth keys etc.)
// oAuth-actions.ts
"use server"
import { redirect } from "next/navigation";
import { createClient } from "@/utils/supabase/server";
export async function signInWithGoogle() {
const supabase = createClient();
const { data, error } = await supabase.auth.signInWithOAuth({
provider: "google",
options: {
queryParams: {
access_type: "offline",
prompt: "consent",
},
redirectTo: '/test' //Doesn't work so whatever
},
});
if (error) {
console.log(error);
redirect("/error");
}
redirect(data.url);
}
export async function signInWithGithub() {
const supabase = createClient();
const { data, error } = await supabase.auth.signInWithOAuth({
provider: "github",
options: {
queryParams: {
access_type: "offline",
prompt: "consent",
redirectTo: '/' //Doesn't work so whatever
},
},
});
if (error) {
console.log(error);
redirect("/error");
}
redirect(data.url);
}1st photo: before login
2nd photo: after login (with google)
3rd photo: updated middleware
4th photo: default