The Right Way to do Auth with the Next.js App Router - UPDATED

  Рет қаралды 21,625

Supabase

Supabase

Күн бұрын

Пікірлер: 96
@Supabase
@Supabase 5 ай бұрын
Check out the full playlist to learn about Postgres Views, Functions, Triggers and RLS: kzbin.info/www/bejne/f4C8d4ZjZ9F5oLc
@amccullough19
@amccullough19 5 күн бұрын
(nextjs noob here) The whole way through this video I was wondering why you were using a Layout instead of middleware for protecting routes, glad I stayed until the end :D Nice video 👍
@luuks1609
@luuks1609 5 ай бұрын
I would love to see how we can build proper role based authorization
@Monstermash355
@Monstermash355 4 ай бұрын
Already outdated
@yarapolana
@yarapolana 5 ай бұрын
how would you validate routes? some routes are plublic others are not, should we do an util with an array of “includes”|"startsWith"?
@JonMeyers
@JonMeyers 5 ай бұрын
Yeah, you would need to add separate checks for the routes you want to be protected. A utility with an array of protected routes could be a good way to solve this 👍
@rtorcato
@rtorcato 5 ай бұрын
why doesn't supabase just work on an official nextjs adapter?
@boyo_23
@boyo_23 5 ай бұрын
If I am not mistaken, there was an adapter before. However, they moved to this architecture because more of the popular meta-frameworks are moving towards SSR. Hence, by having a general-purpose SSR package, it will be easier to maintain and replicate on different meta-frameworks.
@JonMeyers
@JonMeyers 5 ай бұрын
@@boyo_23 Yeah, we very unfortunately developed this just before the App Router was released and everything Next.js changed! 😆The SSR package is designed to configure Supabase to use cookies in any framework, you just need to specify how to get, set and remove cookies for the specific framework. We are looking at ways to simplify this though!
@zwikhd
@zwikhd 5 ай бұрын
Your videos are so solid Jon! Keep up your incredible contribution to the community :D
@iamstarcode
@iamstarcode 5 ай бұрын
Nice, I have always imagine there could be a new way Auth will be handled in the Supabase and NextJs, there's now a slight change compared to when using the -e with-supabse of create-next-app in the utils middleware, let me go and edit my previous project accordingly, Thanks John 👌
@funkdefied1
@funkdefied1 5 ай бұрын
[EDIT 13:45 - This was addressed] Very nice, but my understanding is that putting auth in Layouts is insecure. You need to use Middleware to keep is secure.
@JonMeyers
@JonMeyers 5 ай бұрын
Yep, that was the reason for the updated version of this video! 😆
@codePerfect
@codePerfect 5 ай бұрын
I just uploaded a video today which involves supabase auth and I was like wth changed🤣🤣. Glad it was just that
@mahdiboughanmi6781
@mahdiboughanmi6781 5 ай бұрын
Hello can u make a full stack multivendor ecomm with supabase !! supabase is lacking too many resources for beginners honestly !! i subscribed
@chucknorris4803
@chucknorris4803 14 күн бұрын
You are SOO ZOOMED IN I CANT SEE ANYTHING
@dziaddy
@dziaddy Ай бұрын
supabase/ssr opts out of caching in nextjs so if I want to fetch any data with this package it totally ruins caching. How to handle this?
@creatorsremose
@creatorsremose 5 ай бұрын
Extremely helpful, thank you Supabaggins!
@JonMeyers
@JonMeyers 5 ай бұрын
You're welcome, Sam!
@jonny555333
@jonny555333 4 ай бұрын
Wouldn't the env variables be exposed to the client if prefixed with "NEXT_PUBLIC"? Is that not a security vulnerability?
@suzma_a
@suzma_a 5 ай бұрын
can you in middleware also have information added to custom JWT token, like user's subscription tier? thanks!
@mikejohneviota9293
@mikejohneviota9293 5 ай бұрын
is supabase fix that weird spam log in SvelteKit supabase ssr package?
@appstuff6565
@appstuff6565 5 ай бұрын
Hey thanks for this. I have literally set up everything using auth helpers with cookies, from signup form to middleware to callback. Im using the NextJS approuter with basic email and pass signup offered by supabase atm. Do you plan to do a simple migration video on that as well?
@nicolascalderon9366
@nicolascalderon9366 5 ай бұрын
why in the middleware didn't you have to exclude the "/auth/callback" route too? Every time the sign-in process starts, an intermediate redirection to that route ("auth/callback") will happen to exchange the code for the session token, and at that point the user is not retrieved yet -> so the middleware is invoked -> user is redirected to login again always
@emptytank604
@emptytank604 5 ай бұрын
I had this issue trying to implement the google provider while following the video, so I added the auth route to my excluded routes in my middleware config: export const config = { matcher: ["/((?!_next/static|_next/image|auth|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)"]}
@nicolascalderon9366
@nicolascalderon9366 5 ай бұрын
@@emptytank604 exactly!
@_rkrd
@_rkrd 4 ай бұрын
They really screwed up the documentation on this one. Everyone must be stumbling because of this.
@heramoti123
@heramoti123 3 ай бұрын
@@emptytank604 thank you for this ❤❤
@jonathangamble
@jonathangamble 5 ай бұрын
This means you have to make 2 calls on every request to Supabase, even when there is no user. This solution has slowed down all server apps! The SvelteKit version does not do this.
@JonMeyers
@JonMeyers 5 ай бұрын
Yep, it is not ideal! We are looking into ways to make this more efficient on the Supabase side, but this is mainly due to the technical limitations of streaming which means Server Components can only have read access to cookies. If you have an expired session by the time you reach the Server Component it will have no way of refreshing it. This issue does not exist in SvelteKit!
@santiskiffa
@santiskiffa 5 ай бұрын
The github repo has not been updated to include this new way of protecting in middleware right? Would love to take a closer look.
@JeffreyHo565
@JeffreyHo565 5 ай бұрын
The docs show the middleware refreshing the token and storing it, but wouldn't it also be possible to include the if (!user) redirect to /login inside the middleware as well to do both?
@quangminhtran7592
@quangminhtran7592 5 ай бұрын
I love your humor in the video! 🤣
@JonMeyers
@JonMeyers 5 ай бұрын
Thanks! Glad you enjoyed it! 🙌
@emptytank604
@emptytank604 5 ай бұрын
Is there a way to do "Remember me" checkboxes with supabase auth? I haven't seen any docs or instructions on how to do this properly.
@marcusgamboa6018
@marcusgamboa6018 5 ай бұрын
Do you create a new client on every request? Or just create it once and use it for all requests?
@yarapolana
@yarapolana 5 ай бұрын
Does the middleware also run on api routes and auth/callback route? Is the validation needed for those routes?
@boyo_23
@boyo_23 5 ай бұрын
yes and yes
@mangakalatino
@mangakalatino 3 ай бұрын
hi, i have this message in my middleware Failed to parse cookie string: SyntaxError: Unexpected token 'b', "base64-eyJ"... is not valid JSON The error occurs because the supabase jwt is not a json string, I have already checked on github and I cannot find an explanation.
@dheerajs2838
@dheerajs2838 5 ай бұрын
hat is back :)
@JonMeyers
@JonMeyers 5 ай бұрын
I did a Twitter poll for this and the community has spoken! The hat stays! 😆
@williamliu796
@williamliu796 5 ай бұрын
Hey Jon, great video. I'm using middleware to protect my routes, but one super annoying thing is the supabase types still show that my user might be undefined and I have to handle that everywhere. I'd love some way of telling the supabase client that the user is guaranteed via nextjs middleware on specific routes.
@JonMeyers
@JonMeyers 5 ай бұрын
Good suggestion. Might be a bit challenging as the Supabase Client does not know about whether or not it is within a route that is protected by middleware. The types are correct here, but with the current routing logic, the user should always be present. Might be a good use case for using the ! operation. Or setting the variable with `as User` telling typescript that there will definitely be a user present.
@pep-r1g
@pep-r1g 3 ай бұрын
why db postgress and not supabase, it is confusing becuase is calling db from vercel so which db is this from ?
@Omar_Al_Seddik
@Omar_Al_Seddik 4 ай бұрын
Can you please cover storage with Next.js?
@prateekjain2013
@prateekjain2013 2 ай бұрын
@supabase i have exact same setup But facing a issue fom past 1 week I am using supabase auth and supabase db with NExt.js and in my API route I couldn't able to access user using getUser method session is also null when consoled, due to which I am unable to seed the db with user ID please help all possible ways I tried but now couldn't find a way to get out of this
@janglad9136
@janglad9136 5 ай бұрын
4:40 "any logic we add before this return statement is going to be run for any of the child routes of this component". This is not true. Layouts do not rerun if you navigate between routes that are underneath them. If you put this auth check high up it may never rerun unless you do a hard refresh. I know you did end up using middleware at the end of the video but I honestly don't understand why you even show the layout way in the first place. Again, it doesn't rerun for all route changes, doesn't support static rendering and most of all is just not secure at all due to how streaming of RSCs work.
@Marcustoth-u5y
@Marcustoth-u5y Ай бұрын
The github link is not updated based on the code written here.
@boyo_23
@boyo_23 5 ай бұрын
Can you also make an updated tutorial on RBAC?
@viniciusbecaleti
@viniciusbecaleti 5 ай бұрын
I have a barbershop table and a customer table in my project. A customer is related to a barbershop, and I prefer separate authentications, how would I do this?
@JonMeyers
@JonMeyers 5 ай бұрын
You can do your own custom authentication outside the Supabase Client. Just make sure your tables have RLS enabled, and you are enforcing authorization rules server-side
@boyo_23
@boyo_23 5 ай бұрын
Will you update the docs as well?
@JonMeyers
@JonMeyers 5 ай бұрын
The docs also use Middleware 👍
@boyo_23
@boyo_23 5 ай бұрын
@@JonMeyers But it does not show how to do route protection through the middleware
@boyo_23
@boyo_23 5 ай бұрын
I just thought and most likely other found in confusing on how to protect routes through the middleware until this video. (Btw, thanks alot!)
@phantomx3710
@phantomx3710 3 ай бұрын
Can I still use this or do I need to change to 'getAll()' and 'setAll()'?
@baguingi5589
@baguingi5589 5 ай бұрын
Mmm ok, so I'm a little confused, I used the create-next-app -e with-supabase and the starting code has no logic for protecting the app trough the middleware, so I guess he means that the starter code only sets up the app "except" the protection of pages? I thing this should be part of the starter code, as this information is not even on the supabase documentation yet! Otherwise, great video, I can finally correctly secure my pages.
@baguingi5589
@baguingi5589 5 ай бұрын
I should also add that this BETTER be part of the new docs, looking at my statistics, when using the old supabase-auth-helpers package instead of the new ssr package mixed with the old way of securing pages, I was getting 10-15 auth requests on login ONLY wich is crazy! Thanks to this it gets reduced to 3 auth requests, wich is much more reasonable.
@NilsWestgardh
@NilsWestgardh 4 ай бұрын
The login redirect check at 15:10: if (!user && !request.nextUrl.pathname.startsWith('/login') { return NextResponse.redirect(new URL('/login', request.url)); } is different than the same check in the repo: if (!user) { return NextResponse.redirect(new URL('/login', request.url)); } Why is that?
@dkaigorodov
@dkaigorodov 5 ай бұрын
The Right Way to do Auth with the Next.js: I've just started using Astro instead
@JonMeyers
@JonMeyers 5 ай бұрын
That does get around 90% of the config in this video! 😆
@kausikmr
@kausikmr 5 ай бұрын
can someone help me how to get the rows which are added today based on the created_at
@Dom-zy1qy
@Dom-zy1qy 5 ай бұрын
We can call signInWithOAuth on the client side securely right? You're just doing it on the server for SSR?
@JonMeyers
@JonMeyers 5 ай бұрын
Yep, correct!
@frontend_ko
@frontend_ko 5 ай бұрын
you used middleware to refresh expired cookie. but middleware has caching, which has a possibility to miss expired cookie. how can i make middleware check every request? Checking user in Middleware everytime looks inefficient...
@JonMeyers
@JonMeyers 5 ай бұрын
Middleware is not cached, it will check on every request 👍 It is inefficient! We are looking at ways to solve this elegantly in supabase-js, but the underlying issue is that Server Components only have read access to cookies, due to the technical limitations of streaming.
@frontend_ko
@frontend_ko 5 ай бұрын
@@JonMeyers thanks for nice answer!
@joepetrillo6185
@joepetrillo6185 5 ай бұрын
Can anyone explain why it’s ok for middleware to make a call to getUser every time? Every single route will have to make a fetch to Supabase in middleware? I thought this was bad practice? Does refreshing the session (getUser) in this case just mean getting a new JWT?
@edgarasben
@edgarasben 5 ай бұрын
I would like to know that too. Maybe JWT is stored locally, and fetch to Supabase only happens when it must refresh.
@JonMeyers
@JonMeyers 5 ай бұрын
This example is a catch-all for all routes, but likely you would want to scope this down to just the routes that you want to be protected - require authentication getUser will ensure the user's session is not expired and also validate the JWT
@michaelfonda5409
@michaelfonda5409 5 ай бұрын
If I build a web3 auth flow how can I submit that to the team for integration?
@JonMeyers
@JonMeyers 5 ай бұрын
I don't personally have any experience with web3, but Supabase and the SSR package are open source! So any one can open a PR or discussion 👍
@michaelfonda5409
@michaelfonda5409 5 ай бұрын
@@JonMeyers sweet, I'll open a discussion first and maybe can find some people who can do the more technical parts of the integration. The auth process itself is quite simple, I use it in all my projects but it's done on the backend through webhooks instead of logging a user in. I've used cookies +JWT before but it wasn't as eligant as I'd like. I'm sure opening a discussion will help!
@Tanner-cz4bd
@Tanner-cz4bd 5 ай бұрын
Docs need to be kept upto date
@wiesson1546
@wiesson1546 5 ай бұрын
Have the official examples on the website been adapted? If so, the link please :)
@gioelem5957
@gioelem5957 5 ай бұрын
Please do the same for SolidJs
@dongums
@dongums 5 ай бұрын
hope show some love for svelte
@tejassethi9319
@tejassethi9319 5 ай бұрын
I wish supabase had ttl so i could switch over
@anstapol
@anstapol 5 ай бұрын
What about oauth handler 😂 There’s no session yet when you get there. Haven’t you just prohibited the access on that route as well?
@JonMeyers
@JonMeyers 5 ай бұрын
That is a very good point! Might be a better idea to reverse this logic, and check for the routes that you want to be protected, rather than excluding the ones that should be public 👍
@sicario55
@sicario55 5 ай бұрын
The right Way to do Auth starts with docs that don't require the same cognitive load to find the Right Way to do Auth & not having to revert to a video about the The Right Way to do Auth. Your docs? all over the shop, zero version separation from Pages to App router as well as client server & the search functionality is so slow making it unusable.
@Bruno_OX
@Bruno_OX 5 ай бұрын
I see a change in auth/callback to auth/confirm in the docs the real problem the GitHub of the update use the old version, but the docs show the new way of do. Wait for me work about this or someone resolve. haha
@JonMeyers
@JonMeyers 5 ай бұрын
You can use either. The auth/confirm version in the docs accounts for an issue users were experiencing when triggering an OTP from a separate device
@julienblanchon6082
@julienblanchon6082 5 ай бұрын
Please do the Svelte equivalent
@raykay6613
@raykay6613 2 ай бұрын
why you jump so much...
@malinaeemi8603
@malinaeemi8603 26 күн бұрын
this didn't age well..
@igorkovacevic2792
@igorkovacevic2792 24 күн бұрын
why not?
@Devine-q2i
@Devine-q2i 4 ай бұрын
Outdated ! Do not use this
@pratikkulkarni891
@pratikkulkarni891 3 ай бұрын
Wait, is this video outdated already? It was like a month since it has been released. I am going through the docs and they are slightly different which is why I'm a bit confused
@phantomx3710
@phantomx3710 3 ай бұрын
@@pratikkulkarni891 Yes, because `get,set and remove` is deprecated
@igorkovacevic2792
@igorkovacevic2792 24 күн бұрын
@@phantomx3710 Then what else are we suppose to use?
СОБАКА И  ТРИ ТАБАЛАПКИ Ч.2 #shorts
00:33
INNA SERG
Рет қаралды 1,7 МЛН
2 MAGIC SECRETS @denismagicshow @roman_magic
00:32
MasomkaMagic
Рет қаралды 28 МЛН
Wait for the last one 🤣🤣 #shorts #minecraft
00:28
Cosmo Guy
Рет қаралды 22 МЛН
10 common mistakes with the Next.js App Router
20:37
Vercel
Рет қаралды 225 М.
The Right Way to do Auth with the Next.js App Router!
13:41
Supabase
Рет қаралды 24 М.
3 reasons you should use Postgres Functions and Transactions
16:58
Every React Concept Explained in 12 Minutes
11:53
Code Bootcamp
Рет қаралды 757 М.
8 things you should NEVER do in a Database!
22:56
Supabase
Рет қаралды 12 М.
"The Life & Death of htmx" by Alexander Petros at Big Sky Dev Con 2024
23:01
Montana Programmers
Рет қаралды 67 М.
Next.js Authentication - AuthJS / NextAuth for Role-Based Security
1:26:55
freeCodeCamp.org
Рет қаралды 114 М.
СОБАКА И  ТРИ ТАБАЛАПКИ Ч.2 #shorts
00:33
INNA SERG
Рет қаралды 1,7 МЛН