-
Notifications
You must be signed in to change notification settings - Fork 230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sveltekit helpers update #245
Conversation
Firstly, thank you for your work bringing the auth helpers up to date with the newest version of sveltekit. I have one question. I don't see any example or possibility to trigger an action with authentication. All I see is authenticated data retrieval, but not authenticated data posting. Am I seeing this correctly, or how would I go about making an authenticated insert/update/delete from the server side? |
Using I´ll add an example though! Edit: Completely misread your comment, there is missing a helper for actions. The same logic would apply though. Something like this: export const load: Actions = {
createPost: actionWithSession(
{ status: 401, '/signin' },
async ({ session, getSupabaseClient }) => {
// do something
}
)
} |
To clarify a bit: The helpers (load, serverLoad, server) check if there is a logged in user and if not redirects with the status and location provided. So the callback only runs when there is a session, which makes sense to me. There won´t be many pages that check if there is no session right? There could be added even more to the event than just the session. export const load: PageLoad = loadWithSession(
{ status: 303, location: '/' }, // the user is redirected if there is no session
async ({ session }) => { // we have a session and can make authenticated requests
const { data: testTable } = await supabaseServerClient(session.accessToken)
.from<TestTable>('test')
.select('*');
return {
testTable,
user: session.user // the user is correctly typed, no need to check if it´s nullish
};
}
); Since the helper has access to the session this is possible aswell: export const load: PageLoad = loadWithSession(
{ status: 303, location: '/' },
async ({ getSupabaseClient }) => {
const { data: testTable } = await getSupabaseClient()
.from<TestTable>('test')
.select('*');
return {
testTable,
};
}
); Thoughts? |
Ok, thank you, I figured as much, but how do I get the access token in an action as there is a loadWithSession function but no “action with session” function. Maybe I am confused. Imagine a scenario like this, where I want to make sure only authenticated users can insert into the database. export const actions: Actions = {
add: async ({ request }) => {
const json = await request.json();
supabaseServerClient(json.accessToken)
.from<definitions['Food']>('Food')
.insert({ bar_code: json.barCode });
}
} |
You can use export const actions: Actions = {
add: async ({ request, locals }) => {
if (!locals.accessToken) {
throw redirect(303, '/signin');
}
const json = await request.json();
supabaseServerClient(locals.accessToken)
.from<definitions['Food']>('Food')
.insert({ bar_code: json.barCode });
}
} |
Okay, thank you so much, I will give it a try! Sorry for taking your time. Having an example for actions would be really helpful. I could think one up once this PR gets merged. |
Feel free to leave some comments |
I guess this dangerous SvelteKit data leaking in SSR issue should be checked as well. |
Good catch, i found a few more issues in the readme. |
I noticed one problem though that is also included in the current version.
I can think of two possible solutions:
Option 3 sounds nice, but since we don´t have access to locals in I prefer option 1 as there is no reason to use the helper method if you want to serve public data as you can simply use the global supabaseClient instance. That way you get feedback if you are doing something wrong. |
@david-plugge please, can you check in your updated docs (Server-side data fetching with RLS and Protecting API routes) whether your imports are right? Thank you very much. |
nice work guys, i really need this to be merged 😅 |
Great stuff guys, really appreciated all the work that has gone here! Edit: I've forked and published the new version via npm, but getting an error when trying to install. |
Have you published the package with npm or pnpm? I think npm doesn´t replace And you didn´t publish the auth-helpers-shared package right? pnpm i @maxkozlowski/auth-helpers-sveltekit
> ERR_PNPM_NO_MATCHING_VERSION_INSIDE_WORKSPACE In : No matching version found for @supabase/auth-helpers-shared@* inside the workspace |
Have you published the package with npm or pnpm? And you didn´t publish the auth-helpers-shared package right? Happy to try to publish both libraries with pnpm instead if that should work? |
Hopefully, it will be merged soon. It was just the weekend, and this is a company, so I assume they don't work on the weekends. |
It should, if not there is a bug. Make sure to change the package name of the shared helpers in the dependencies of the sveltekit helpers too |
Thanks for this @david-plugge, I will get a release out during this week after writing up the migration docs. |
Thank you for merging the change and releasing so quickly! I've downloaded the library but I'm getting an error with one of imports in
Does anyone experience the same? Not sure how to fix this 🤔 |
Yeah that´s a known issue. If you have multiple entry points it´s hard to get ts intellisense working if they are not located in the packages root folder. I´ve setup "typesVersions": {
"*": {
"server": ["./dist/server/index.d.ts"],
"./": ["./dist/index.d.ts"]
}
} I created a PR #267 |
Thank you David! Does that mean that we'll need to wait for the new release now? |
@silentworks, was |
I'm getting ERR_MODULE_NOT_FOUND due to imports not having the extensions specified eg. How are people getting this to work? |
In the meantime you can use this alias in your svelte.config.js alias: {
'@supabase/auth-helpers-sveltekit/server': './node_modules/@supabase/auth-helpers-sveltekit/dist/server'
} But i think there are a few more issues that need to be addressed. It seems like |
Before the update there was a script that adds |
So rather than it living in node_modules, it should live in your project root? |
No i mean in the folder |
Ah, OK I see. Got you, thanks |
Has anyone got the new release to work in the end? I am unable to I'm afraid. It would be fantastic to either get clear instructions as to how to get it to work or ETA when we might get a release that works. |
Looks like the new release works well 👏 Has anyone come across this? |
@maxkozlowski You need to create named action.
to
and in your form use |
Take a look at this example https://github.com/supabase/auth-helpers/blob/main/examples/sveltekit-email-password/src/routes/(app)/%2Bpage.server.ts :) |
or it’s auto handled if we use the
|
|
Thanks both! One more question: I'm getting some issues when trying to do password reset. I use Even though the user appears to be logged in, when I try to do
I get an error Thank you 🙏 |
I followed all the steps on how to setup @supabase/auth-helpers-sveltekit. Everything seems so trivial, but when I try to sign into my page using an authentication provider like I am sure I am missing something here but I just can't figure it out. On calling signing method like this
It will just return null for every property
It is the same even if I use an authentication provider name, that is unsupported by Supabase. P. S. Any ideas are more than welcome, Thanks! |
@mitjakukovec you can´t get the session like that! So in case you are trying to achieve oauth login on the server, you simply can´t at the moment (maybe possible in the future). Just call |
Thank you @david-plugge, this is exactly what I was doing wrong! I was trying to log in on the server and then somehow sync the session back to the client and not vice versa. As you can see I am not a web developer by trade, but C++ guy with mainly embedded systems experience. Does it make sense to do the signin call on the server and then redirect the client to the received URL? Something like this
|
Not really, since you need client js anyway to retrieve the token. |
Closes #230
What kind of change does this PR introduce?
Support new versions of sveltekit (around 1.0.0-next.440 upwards, random guess based on their changelog)
What is the current behavior?
We can´t use auth helpers with the current version of sveltekit.
Additional context
Since there is no need for a Svelte component we could build the sveltekit helpers with tsup like the other packages.
PS:
SvelteKit is in release candidate phase!
Edit: very sorry for this huge commit :/