Supabase + Sveltekit Auth #5218
-
With Sveltekit's breaking update 10 days ago, I have not been able to get Supabase auth work with the new Sveltekit. Client side authentication works fine, but there's no way to call Has anyone been able to the new sveltekit work with Supabase? I would very much appreciate some help. Perhaps the great Supabase team can create a sample app with sveltekit? |
Beta Was this translation helpful? Give feedback.
Replies: 13 comments 30 replies
-
Not sure if he checks here, but go to Supabase Discord and shout out @silentworks with your question. He is a mod there and uses and comments on Sveltekit stuff a lot. He might be interested in this if he has not hit it yet. |
Beta Was this translation helpful? Give feedback.
-
@akiarostami you can have a look at my application which I upgraded yesterday to use the latest SvelteKit. The main changes are that now you have to use the native Request over the custom one SvelteKit had implemented. So take a look at the following files: hooks.jsI've abstracted most of the code from this into handleAuth.jsIn this file we now get the request from expressify.jsI've updated the I hope this is all helpful, you can clone the project to see how it works and just copy those 3 files into your own project and it should work as intended. https://github.com/silentworks/waiting-list Thanks for the ping @GaryAustin1. |
Beta Was this translation helpful? Give feedback.
-
@silentworks thanks for the solution. I played around with your code, and it definitely solves my "express" issue with the new format. Thank you for that. I also found another approach by @eikaramba which was a great one, and it also solved the token time-out / refresh problem (which has been very annoying). I suggest taking a look at his code (which does need some cleanup). I can post my cleaned-up code here as well, if that helps. |
Beta Was this translation helpful? Give feedback.
-
Below my code based on @eikaramba's code, modified toz work with the new breaking updates of sveltekit. I have not fully tested all corners, but I haven't had any problem so far. Auth.js
API
hooks
and finally add this to: __layout.js
This should address authentication, server-side cookies, and auto-refreshing the server-side cookies. User object will be available in all pages via session. |
Beta Was this translation helpful? Give feedback.
-
I don't have a lot of time to get into that this week. However, the oauth
examples are certainly on my list for finishing this up. Thank you for
helping out with the snippet!
…On Tue, Mar 8, 2022 at 9:29 AM Liam Sanft ***@***.***> wrote:
btw i had a quite interesting issue on safari on ios too. Just food for
thoughts (i don't necessarly believe this is the issue, but maybe who
knows) safari is not setting a 3rd party domain cookie if you come from a
redirect. e.g. you want to use oauth and then the flow is like this
mywebsite -> apple.com (for oauth) -> mywebsite (now setting cookie for
mywebsite)
in this case the cookie will not be set! in other browsers it works. Quite
interesting, but there is an easy workaround. just let the page refresh
itself again automatically
function buildquery(query){
if(query && Array.from(query).length>0){
return "?"+query.toString()+"&refresh=true";
}else{
return "?refresh=true";
}}
//in some endpoint first serve a blank page that refreshes itself again automaticallyexport async function get({ locals, request, url, params }) {let cookies = cookie.parse(request.headers.get("cookie") || "");if (!cookies.refresh_token) {
if(!url.searchParams.get('refresh')){
return {
status: 200,
body: `<html><head> <meta http-equiv="refresh" content="0; url=${buildquery(url.searchParams)}"> </head></html>`
};
}
}//the actual code
...}
This only happened to me when using "SameSite=Strict". If you use
"SameSite=Lax", it should work in Safari.
—
Reply to this email directly, view it on GitHub
<#5218 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACS3J7VZJDGBPLRBTRAPH3U65P3TANCNFSM5NCDZJJA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
So far I have done client side auth with Supabase and that works well. However I am new to SSR auth with SvelteKit and that seems a) @eikaramba Concerning the issue with the cookie this post might help that was raised in this SvelteKit issue. Just last night I happen to stumble across this treasure trove of content and hope to see an official way documented by Supabase using SvelteKit with SSR. Thank you all for your input here. 👍 |
Beta Was this translation helpful? Give feedback.
-
In the May 2022 edition of the Svelte Blog there is this article that does SvelteKit SSR Auth with Supabase. https://svelte.dev/blog/whats-new-in-svelte-may-2022 SvelteKit JWT authentication tutorial by pilcrowOnPaper |
Beta Was this translation helpful? Give feedback.
-
After using @akiarostami s code I get this error when I try to log in. Anyone seen anything similar and/or know how to solve it?
|
Beta Was this translation helpful? Give feedback.
-
One thing it is worth noting in the abstract, is that the supabase
isomorphic client, and in fact, more broadly, browsers in general, do not
have a clientside security model that behaves the ways that users have come
to expect.
Specifically, "sessions", by which we mean an instance of a browser, when
the browser is closed the "session" ends, are not supported for client side
Supabase.
if you need database access, and you have your website go direct to
supabase, you have two choices. Either you use in memory supabase client,
in which case the client will sign out if you refresh the page, or you use
persistent access, in which case the client will automatically remain
signed in for the duration of your refresh token.
Neither of these behaviours are expected by the end user, which means, in
my case, I will not use the supabase client side data funnel for this
reason.
People are not talking about this. Its deeply important that end users
understand the security model of their web applications.
Where can I go to discuss this seriously with supabase and others? Any idea?
Christopher Reay (they / them)
Be prepared to have your predictions come true
…On Fri, 3 Jun 2022 at 23:18, Ahmad Kiarostami ***@***.***> wrote:
Honestly I haven't touched this code for a long while, and I haven't done
anything with Supabase / SvelteKit since then. SvelteKit is still changing
so I don't know if anything has changed in their API or not. But Supabase
*just* published their new auth helpers. That might be a more reliable
path if you're implementing an Auth for Supabase / SvelteKit.
This is the helper repo
<https://github.com/supabase-community/supabase-auth-helpers>.
—
Reply to this email directly, view it on GitHub
<#5218 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAB2PPRT6K7SLCDBROETFY3VNKABPANCNFSM5NCDZJJA>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Right, but the cookies should be http only, at which point what do you do
with them? You can use them for the server side access, but for client side
access... how do you structure the system. I cant think of a way
tytyt
Christopher Reay (they / them)
Be prepared to have your predictions come true
…On Sat, 4 Jun 2022 at 16:39, wwall3r ***@***.***> wrote:
Session cookies can be used to store the tokens, which would effectively
log the user out on browser close, since neither the access nor refresh
token would be available to be sent, even if they are still valid.
—
Reply to this email directly, view it on GitHub
<#5218 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAB2PPTQ3NK32V7WKMG2P2DVNN2DZANCNFSM5NCDZJJA>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
By the way, in case you're deploying to Vercel, you can just use Vercel's serverless functions instead of SvelteKit endpoints. Might be the easiest solution in many cases! |
Beta Was this translation helpful? Give feedback.
-
Can you give example?
Den lör 9 juli 2022 10:51Kabir Goel ***@***.***> skrev:
… By the way, in case you're deploying to Vercel, you can just use Vercel's
serverless functions instead of SvelteKit endpoints. Might be the easiest
solution in many cases!
—
Reply to this email directly, view it on GitHub
<#5218 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADGUFGWI37TGBUDR36SQTSDVTE4QLANCNFSM5NCDZJJA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Dropping this here in case it helps anyone out: https://github.com/supabase-community/auth-helpers/tree/main/examples/sveltekit-email-password There seem to be some rough edges in that helper but it's brand new so I expect it'll get those smoothed out soon |
Beta Was this translation helpful? Give feedback.
Below my code based on @eikaramba's code, modified toz work with the new breaking updates of sveltekit.
I have not fully tested all corners, but I haven't had any problem so far.
Auth.js