Replies: 18 comments 22 replies
-
This is a legitimate issue that takes away from the beauty of SSR authentication. I also came across this issue and it took a part of my soul. |
Beta Was this translation helpful? Give feedback.
-
Has anyone found a good solution? |
Beta Was this translation helpful? Give feedback.
-
You can fix this by storing the logged-in state into localstorage or checking against the supabase entry in localstorage. I've done this in a svelte app and it works really well. |
Beta Was this translation helpful? Give feedback.
-
I did this solution // Signup using Google
async function signUpWithGoogle() {
const { error } = await supabase.auth.signIn(
{
provider: 'google'
},
{ redirectTo: 'http://localhost:3000/provider?refresh=true' }
);
if (error) {
alert(error.message);
}
} Like you saw in the above code, I have used a custom redirection The route is just for getting the
<!-- this is my route provider.svelte -->
<script>
/*
This route is just for singup/login with providers
to set the session-cookie for the server
just a redirect route :)
*/
import { supabase } from '$private/supabase';
import { page } from '$app/stores';
import { setAuthCookie } from '$lib/utils/session';
import { onMount } from 'svelte';
onMount(async () => {
setTimeout(async () => {
// Check the query: refresh
if ($page.query.get('refresh') == 'true') {
// get the session from client side
const _session = supabase.auth.session();
// send it to server side
await setAuthCookie(_session);
}
// redirect to the home page anyway
window.location.replace('/');
}, 2000);
});
</script> |
Beta Was this translation helpful? Give feedback.
-
The actuality solution is to use the |
Beta Was this translation helpful? Give feedback.
-
I am encountering the same issue here. When |
Beta Was this translation helpful? Give feedback.
-
Any update on this? In my server-side code ( |
Beta Was this translation helpful? Give feedback.
-
So what is the best up-to-date solution? I'm asking because I'm experiencing a weird issue. I have supabase auth and auth UI with discord as the provider. I worked on my app locally on port 3000. Everything is smooth. Both email/pass and discord auth flows. When deployed to production, desktop Chrome version discord auth flow works fine, yet on mobile, after I use the sign-in with discord after successful discord login, it sends me to localhost:3000, which ruins the complete auth flow experience. |
Beta Was this translation helpful? Give feedback.
-
I had the same issue following the official sveltekit guide. The login wouldn't, at least on chrome/firefox, redirect after login. Sveltekit SolutionFixed this by checking for $: data.session and reloading by invalidating the page (reruns page load functions) on clientside +page.svelte. $: {
// IMPORTANT: detect login was successful
// supabase sveltekit auth ui does not work otherwise
const hasSession = data?.session
if (hasSession) {
console.log('hasSession')
// reruns all associated page load functions
// which will redirect to the correct path
invalidateAll()
}
} |
Beta Was this translation helpful? Give feedback.
-
Hi all, Do we have a workaround here? I followed the tutorial and make sure
Thanks for reading this! |
Beta Was this translation helpful? Give feedback.
-
Any solution for this? |
Beta Was this translation helpful? Give feedback.
-
Found a solution that works with NextJs Pages directory. Shout out to zakaria-chahboun for the solution up earlier in the thread. I've adapted that solution for NextJS below. When signing in with an OAuth provider, redirect to a route client side where you can refresh the session ("provider.tsx"), then redirect user to desired route ("/admin") in this case. The downside to this method is the user will see a flash for the route that does the redirecting. Wish there was a reliable way to do this in middleware.
|
Beta Was this translation helpful? Give feedback.
-
Context: NextJS 13 with Pages Router. Hey guys, I was struggling with session not being available directly after the redirect as well, but then I noticed that supabase passes
import { NextApiHandler } from 'next'
import { createPagesServerClient } from '@supabase/auth-helpers-nextjs'
const handler: NextApiHandler = async (req, res) => {
const { code } = req.query
if (code) {
const supabase = createPagesServerClient({ req, res })
try {
await supabase.auth.exchangeCodeForSession(String(code))
res.redirect('/dashboard')
} catch (e) {
console.error('Failed to exchange code for session:', e)
res.redirect('/')
}
} else {
res.redirect('/')
}
}
export default handler
<Auth
supabaseClient={supabaseClient}
onlyThirdPartyProviders={true}
providers={['github']}
redirectTo={`${window.location.origin}/api/auth/callback`}
appearance={{ theme: ThemeSupa }}
/>
I wish this was explained a little better in the documentation. My understanding is that by default React / NextJS setup uses PKCE flow that gives you the Auth Code that is supposed to be exchanged for a session. What is confusing is that even if you don't do exactly that, it seems that Supabase's React helpers manage to do the same job asynchronously in the background, so that's why solution with some kind of a "loader" page that just waits until session becomes available and then does the redirect also works. Hope this helps. |
Beta Was this translation helpful? Give feedback.
-
Context - App Router NextJS 14 - Using magic-link and PKCE with easy provider drop ins. Be sure you have all URLs added to Supabase URL config. Hope this helps! --./supabase-provider.tsx
-- ./Middleware.tsx
-- ./callback/route.tsx
--@/lib/supabase.ts --types.tsx - Auth specific types
--./auth-form.tsx -- replace Supa Theme with a tailwind theme
|
Beta Was this translation helpful? Give feedback.
-
Does anyone have a SvelteKit 2 solution to this? |
Beta Was this translation helpful? Give feedback.
-
Context: SvelteKit & Supabase, using the Supabase Auth UI components Issue: as referenced a few times in this discussion, the <Auth ... > component would redirect but require a refresh to update the session data on the client side After wrestling with this for a couple of hours, this solution works well: Check out this guide in the Supabase docs and implement the 'src/routes/auth/callback/+server.ts' as described, then set your Auth redirect like so:
... and it should work. |
Beta Was this translation helpful? Give feedback.
-
Does anyone have a solution for Vite React apps? I'm using Supabase with Google OAuth as:
And after successful authentication, I want the user to be directed to the "/dashboard" route and not the "/" route. Howerver it seems the user always ends up in the I also have a Context where I keep track of the auth state and listen to
But not sure how to make sure logged in users always end up at "/dashboard" and logged out users always end up at "/" home page. |
Beta Was this translation helpful? Give feedback.
-
I'm also fighting with this since a while.. how is there still no good, official solution for react / next? |
Beta Was this translation helpful? Give feedback.
-
I'm looking to redirect users from all pages to the login page unless they are already signed in. I've built a ProtectedRoute component that checks if there is a user logged in and whether the path is protected, and will router.push to /login accordingly.
The issue im having is that after
await supabase.signIn()
is called, I’m being sent back to my login page. What I think is happening is that after signin I'm redirected to my homepage, but after the redirectsupabase.auth.user()
is still null, and my ProtectedRoute component is then redirecting back to /login because of this. If I enter the url to go to the home (or other) page again, it won’t redirect me back to login,user
is no longer null and they are signed in.In other posts I saw
onAuthStateChange
suggested to be used. But I am already using an AuthProvider similar to the examples that utiliseonAuthStateChange
. I think in my case it may be that the change in path is triggering a redirect before the user state is changed (also perhaps before the cookie is set?). But I'm not sure how to fix this.I see in a few examples people redirect with getServerSideProps. But if I did this, all of my pages would go from client side rendering to SSR, and I’m not sure if that would benefit me since I'm not interested in SEO. Although I’m not a fan of the loading page flash that occurs when a protected route is being client side redirected to the login page.
Beta Was this translation helpful? Give feedback.
All reactions