SignInWithOauth never triggers route.refresh(), Nextjs 13 app directory. #20438
Replies: 7 comments
-
I have an issue with the middleware but I don't know if it's related to your problem : After using signInWithGoogle(), I use // middleware.ts :
const sessionUrls = ['/dashboard', '/marketplace', '/network', '/ressources'];
if (!session.data.session && sessionUrls.includes(req.nextUrl.pathname)) {
console.log('##### Middleware redirect to home');
url.pathname = APP_LINK.home.href;
return NextResponse.redirect(url);
} If the user is not authenticated and tries to access those protected routes, he is redirected to the home page '/'. |
Beta Was this translation helpful? Give feedback.
-
Faced the same issue, here is a solution that worked for me. It takes a bit of time until you are logged in with google, so after the redirect from selecting your account page, you should redirect (using redirectTo parameter in signInWith Auth function) to an unprotected route and then get the session, if session contains an access token then move to a protected route, that way you will wait until the SIGN_IN event has completed its job. |
Beta Was this translation helpful? Give feedback.
-
May I pick this issue, @Arslan-Soomro ? |
Beta Was this translation helpful? Give feedback.
-
Sure. Go ahead |
Beta Was this translation helpful? Give feedback.
-
The solution I have found at the moment is the only one available, however, we recognize that it is not ideal. Correct me if I'm wrong... For example, I have the following structure in Next.js: app After logging in, there is a waiting period until the Google token is returned. When the token arrives, I should redirect the user to the |
Beta Was this translation helpful? Give feedback.
-
Facing same issue, ive managed to work around this without additional route redirects like this: "use client";
const Login = ({user}) => {
const handleSignIn = () => {
supabase.auth
.signInWithOAuth({ provider: "google" })
.then(() => {
router.refresh();
});
};
useEffect(() => {
// It takes some time to retrieve session access token, so until user is ready just try get session
if (!user) {
setTimeout(
() =>
supabase.auth.getSession().then(({ data: { session } }) => {
if (session?.access_token !== undefined) {
router.refresh();
}
}),
100,
);
}
}, [user]);
...
} And in parent server component i'm loading the user: export default async () => {
const cookieStore = cookies();
const supabase = createServerComponentClient({ cookies: () => cookieStore });
const {
data: { user },
error: getUserError,
} = await supabase.auth.getUser();
return (
<>
<Login user={user} />
</>
);
}; It's far from ideal and there should be some timeout mechanism in useEffect, to avoid inf loop |
Beta Was this translation helpful? Give feedback.
-
There's some new documentation on Nextjs over at It would be useful to set up a clean example install to use along side your apps to see how we handle it. |
Beta Was this translation helpful? Give feedback.
-
We need to refresh route after Google sign In, so that session is updated . As per examples given in supabase we use
But after signing in with google i redirected to home page , but
router.refresh();
is never triggered and this is causing problem and many server component is not rendered which depends upon session.Beta Was this translation helpful? Give feedback.
All reactions