Next.js App Router Authentication (Sessions, Cookies, JWTs)

  Рет қаралды 158,286

leerob

leerob

4 ай бұрын

Learn the foundations of session based authentication through cookies in the Next.js App Router, and how community libraries like Auth.js build on top of this model. Auth is a deep topic, so let me know what else you want to see here!
◆ Basic: github.com/balazsorban44/auth...
◆ NextAuth: github.com/vercel/next.js/tre...
◆ Docs: nextjs.org/docs/app/building-...

Пікірлер: 252
@codewithantonio
@codewithantonio 4 ай бұрын
Always lovely to see official guides on these important topics :)
@eddi3ms
@eddi3ms 4 ай бұрын
Lee Robinson is awesome, always with videos right on point. Also thanks for your guide on next-auth, I'm almost done with it.. 1 or 2 hours left xD. Learning a lot
@filthynirvana5553
@filthynirvana5553 4 ай бұрын
hey there ! just wanted to tell you that your videos are amazing. thank you for your efforts, keep UP the good work
@masaratech
@masaratech 4 ай бұрын
@codewithantonio thank you for your efforts, your work is admirable and helped me alot.
@rig-xf6or
@rig-xf6or 4 ай бұрын
Antonio the goat of web tutorials. Really enjoyed your next-auth v5 tutorial.
@Pra2wins
@Pra2wins 4 ай бұрын
Thanks Antonio for your auth tutorial
@Leo-dx6uc
@Leo-dx6uc 4 ай бұрын
Looking for this tutorial for months. So nice to have a official tutorial to follow. Thanks!
@franknguyen8396
@franknguyen8396 4 ай бұрын
Nice and concise tutorial on a subject matter that tend to be very tedious and confusing. Great job Lee!
@WebDevEducation
@WebDevEducation 4 ай бұрын
Awesome stuff! And really appreciate how the Next docs are being filled out to cover these things ❤
@williamschaefermeyer7007
@williamschaefermeyer7007 4 ай бұрын
Please keep uploading these videos how you have been the last bit I've been really enjoying them and finding them useful
@skverskk
@skverskk 4 ай бұрын
Finally, an easy tutorial explaining Next Authentication using Jose. Well done. I;m a new subscriber.
@daytatech-youtube
@daytatech-youtube 3 ай бұрын
I think authentication is a super important topic for web development and next.js and I think you guys should expand more on this. Security is the most important thing for modern web applications and I think you guys should keep talking about it. Great work.
@user-nk6ih7uh1h
@user-nk6ih7uh1h 4 ай бұрын
Great video! Can't wait to see the full guide on the updated next-auth lib!
@Adrian_Galilea
@Adrian_Galilea 4 ай бұрын
This was incredibly insightful, thanks Lee
@RaulHernandez-to3ce
@RaulHernandez-to3ce 4 ай бұрын
You have no idea how helpfull is this video to me. Thanks a lot for an insightful explanation and for the amazing content
@user-ty5tg2ji1x
@user-ty5tg2ji1x 4 ай бұрын
look at this how awesome. giving lightweith version of topic is really good. thanks man
@DominikSipowicz
@DominikSipowicz 4 ай бұрын
The way this video explains Auth in Next.js reminds me of how Dan explained React Redux. Next.js App Router next-auth changes are also super exciting!
@yanmoenaing7248
@yanmoenaing7248 4 ай бұрын
Exactly what I've been waiting for
@randomforest_dev
@randomforest_dev 4 ай бұрын
This is what I have been waiting for. thanks!
@etsonrap9867
@etsonrap9867 3 ай бұрын
Awesome! I was looking for this explanation, thanks!!
@UmairSadaqat
@UmairSadaqat 4 ай бұрын
I love to watch videos where the focus is on proper documentation
@zykloprt3391
@zykloprt3391 4 ай бұрын
Great tutorial on Next.js authentication, very clear and straightforward! Quick note, though: using both "expires" and the official "exp" claim for JWT expiration seems redundant, as most libraries handle expiration with "exp" directly. This could simplify things: - Stick to "exp" and drop the custom "expires" claim. - Use `setExpirationTime` for consistency across your session and JWT. This keeps your code clean and in line with standard practices. Keep up the great work! 👍
@ilyaaron
@ilyaaron 4 ай бұрын
Great addition! 🙂
@iqbalmuhtadin5614
@iqbalmuhtadin5614 4 ай бұрын
I just finished this feature tonight. after that I watched KZfaq and saw that you had made the tutorial. this is such a coincidence. keep up the spirit, Bro. 😁
@alexg7282
@alexg7282 4 ай бұрын
Thanks ! we need more content about authentification. Very usefull topic.
@ryanbell5779
@ryanbell5779 Ай бұрын
Wow finally, thank you for this video. This really helped my understanding of how to implement my auth flow!
@mohamedaminebouguerra1514
@mohamedaminebouguerra1514 4 ай бұрын
Thanks Lee, always helpful!
@damalo
@damalo 3 ай бұрын
Great video, would love to learn more about auth!
@mhmmdrezalif
@mhmmdrezalif 4 ай бұрын
this tutorial is my life saver, thanks a lot lee!
@TechCode50
@TechCode50 4 ай бұрын
Thank you !! I've been banging my head against the wall on this for the past 2 hours! More on authentication with auth.js/App/RSC/Google Login please !
@stefanomazziotta1270
@stefanomazziotta1270 4 ай бұрын
The guide is so useful. Thanks a lot!
@user-nt8pn2dg1k
@user-nt8pn2dg1k 4 ай бұрын
Спасибо! В документации не нашел примера нужного, а тут стало понятнее.
@wayslow
@wayslow 4 ай бұрын
Thanks for Lucia shout out, I've integrated the V3 recently and it was a breeze.
@Santiego
@Santiego 4 ай бұрын
Yup, Lucia is great
@user-eb4jr3qr5h
@user-eb4jr3qr5h 4 ай бұрын
Thanks for the most needed content ❤
@Oxygen.O2
@Oxygen.O2 2 ай бұрын
Sweet Jesus, where were you the last couple of weeks when I was trying to implement it the correct way? Your solution is so simple yet powerful and God thanks you did it in less thans 12 minutes, thanks man!
@juanpabloaddeo4197
@juanpabloaddeo4197 4 ай бұрын
Thanks for this Lee!
@6099x
@6099x 4 ай бұрын
awesome to see attention to auth* - its such a crucial topic! Would love to see ways to work with open source solutions like keycloak or Ory + Next. They solve nearly anything a large scale product might need while being completely open source
@vincentjohnflorio
@vincentjohnflorio 2 ай бұрын
Just saw the news about the promotion ( a few months after it happened ) -- congrats!
@user-nb7xd5gz4j
@user-nb7xd5gz4j 4 ай бұрын
Pretty good video, as usual!
@farookquisar1
@farookquisar1 4 ай бұрын
good session. yes we need more detail session on this. As this is related to auth or security we don't need to learn this from anywhere else.
@typesafeui
@typesafeui 4 ай бұрын
Love that you did this without libs 🙌
@TheRcfrias
@TheRcfrias 3 ай бұрын
Awesome video, congrats! Specially due to explanation of the missing sign in part that is not in the Next.js docs. Thank you.
@justusadams2770
@justusadams2770 4 ай бұрын
This is very helpful, Great !!!
@thedelanyo
@thedelanyo 4 ай бұрын
This is more informative beyond nextjs and React... well the first example 😊😊
@shakti_pattanaik01
@shakti_pattanaik01 4 ай бұрын
Great video.. a deep dive using supabase auth will be helpful
@danielmajer1648
@danielmajer1648 2 ай бұрын
Bravo Lee and Balázs! I just copied the code, liked it so much!😅
@yunglee.
@yunglee. 4 ай бұрын
Would love a video about using OAuth to login with a custom backend and how to refresh a session with your own issued JWT!
@thepromisebenard
@thepromisebenard 4 ай бұрын
Nice Video. I think authentication and authorization is not really talked about often. It will be nice to see more indepth videos on rolling out custom auth and using libraries like auth js and clerk.
@aberba
@aberba 4 ай бұрын
That's what I expected. Next-auth is already well documented
@night23412
@night23412 4 ай бұрын
@@aberba lol
@ferdinandhriadel8100
@ferdinandhriadel8100 29 күн бұрын
God bless you. I couldn't figure out just how to do exactly that
@shilohgeorge1078
@shilohgeorge1078 Ай бұрын
Thanks a lot Lee, This was really helpfull
@Daddyjs
@Daddyjs 4 ай бұрын
What a awesome tutorial
@27sosite73
@27sosite73 3 ай бұрын
thank you for the repo, mate
@John-hz8si
@John-hz8si 2 ай бұрын
finally an official guide!!!
@pistolplasma
@pistolplasma 4 ай бұрын
Lee great job. If you could create a video on how Next.JS handles “authentication” -vs- “authorization” that would be awesome. Thanks Lee….😁👍🏿✨
@maddogshwa
@maddogshwa 4 ай бұрын
I have to say the canonical approach (authorization middleware) seems a little too basic but if it works it works.
@raphauy
@raphauy 3 ай бұрын
Thank you Lee, very helpfull!
@nilskaspersson
@nilskaspersson 4 ай бұрын
This is great, but what I hoped to get is information about was how authN and authZ intersects with the caches of server components and other server-side functions, as well as how and when to authorize server actions
@saadowain3511
@saadowain3511 4 ай бұрын
Yes we would like to talk more . Authorization
@AhmadAwais
@AhmadAwais 4 ай бұрын
Worth its weight in gold!! Nice one, Lee!!
@setasan
@setasan 4 ай бұрын
Nice, server components are great for websites or when you don't have much interactivity in a component in a application. Having the option of using them is really helpful althogh I much prefered if they didn't had put them as default in next.js.
@user-ld8cf3ed9j
@user-ld8cf3ed9j 4 ай бұрын
I would like to see a video on the topic of authorization/authentication for cases when a third-party server(node/nest/etc...) is used and also how it should work for "use client" components/pages
@poulticegeist
@poulticegeist 3 ай бұрын
I've been able to run the nextauth version just fine in dev mode, but when building in standalone mode, I'm having all sorts of issues. It would be awesome if a version of that in standalone mode were released. thank you, Lee!
@guilhermedavid3232
@guilhermedavid3232 4 ай бұрын
Lee, I have two questions about authentication in Next js. Can I use node js apis in middleware? Or do middleware necessarily run on the edge? The next-auth auth function is not memoized on the server, can I use the react cache function to memo the auth function?
@Chillihero
@Chillihero 4 ай бұрын
Nextjs & discord auth would be awesome, extending the Session, modify the callbacks etc
@kevinbatdorf
@kevinbatdorf 4 ай бұрын
Great video. Someone asked me why 10 seconds, so figured I'd comment here. Correct me if I have something wrong. Setting it to 10 seconds was just to demo it (although maybe some app has a need for it). Typically you would have a longer (7 days, 30 days, forever) "refresh token" as the cookie that continuously updates a short-lived "access token" that you would put in memory. That token could be 10 seconds, although I think 5 minutes is more common. Depends on the application though. But to anyone reading this, just use NextAuth! Don't play with fire.
@aw4534324
@aw4534324 3 ай бұрын
Hey there, what you've described is how I'm currently handling my auth process. However, I'm looking to move toward server components and I'm not sure how to store a token in memory, it turns up undefined. Can you shed some light on that? Or, do both tokens have to be stored in cookies/storage when using RSC?
@diegoalairelibre
@diegoalairelibre 2 ай бұрын
As I see it, when you use JWT you can’t invalidate token. So if you change user roles or simply disable user in database, user session will keep alive as long as JWT is valid. Giving a 10 seconds expiration time you can be sure to keep user data in token allways in sync
@HigherStudyAspirant
@HigherStudyAspirant 4 ай бұрын
Perfect. I always look for the custom way as next-auth is very abstract and I don't know what is going behind. Now my question is do we need to check getSession in each page, if yes, then will it work for Server and Client in the same way? and second query is how to handle callback url, I know if I check the session in each page, then I need to redirect and put the callback of each page, but if there is any way where I can define these in central/one place instead of checking in all pages. P.S: I am not using library (auth.js, clerk etc.,)
@Pierrot24382
@Pierrot24382 4 ай бұрын
Thank you, Lee, this is very helpful! I started working on an app using Auth.js but seeing how easy it it to implement authentication with Next.js, I'm wondering if it is worth using Auth.js. What's the added value compared to just implementing our own auth with Next.js?
@marktaylor865
@marktaylor865 Ай бұрын
a month ago i was entirely new to node.js react.js tailwind next.js and typescript and i have been enjoying it so much until i got to authentication. it has been a horrible experience. this video outlines a more direct approach to authentication and session management and I'm hoping it can help me implement properly because I'm pretty close to quitting next.js.
@marktaylor865
@marktaylor865 Ай бұрын
seems the first method has gotten me 90% of the way there. 3 days i flapped at this.
@RuslanGonzalez-ox6vz
@RuslanGonzalez-ox6vz 4 ай бұрын
Great Video!
@DVSK1
@DVSK1 4 ай бұрын
Lovely & smooth explanation, auth its a excellent topic, could you bring a video about next-auth with user information management updating a profile like profile image using server actions?
@thetechnocrack
@thetechnocrack 3 күн бұрын
Would really love a minimal example on authorization with all the next components in mind!
@Adrian_Galilea
@Adrian_Galilea 4 ай бұрын
Lee, can you do one about GDPR? I was worried when I learned that google fonts was not compliant, and then gladly surprised when I learned that next/font does self-hosting automatically. I would love to know more on how small projects can comply without it being a headache.
@eulersson
@eulersson 4 ай бұрын
Very useful video :D One minor point: shouldn't the encrypt/decrypt really be `encode_and_sign` and `decode_and_verify_signature`? I don't believe you are encrypting unless I am missing something.
@david.thomas.108
@david.thomas.108 4 ай бұрын
You are correct. The JWT isn’t encrypted. It is signed and the signature is verified. The JWT payload is base64 encoded and visible after decode. JWT has a different spec for encryption, known as JWE.
@SteveDotNet
@SteveDotNet 4 ай бұрын
Great walkthrough as always. And for those who noticed the null email, I see someone opened a PR on the linked repo to fix it.
@leerob
@leerob 4 ай бұрын
Thank you! Smol bug :)
@bartesler5262
@bartesler5262 4 ай бұрын
Thanks for this awesome video! Assuming you have a server that is returning a JWT in an API call, and you’re using the pages router, whhat is the right place for the login, logout functions? Using Next api handlers? Also, in this scenario would you still set the JWT in cookies in the same way?
@paramaggarwal
@paramaggarwal 4 ай бұрын
On the input element name="email" was missed, hence the formData read was failing and hence email was getting set as null in the JWT cookie.
@gaming_zen_99
@gaming_zen_99 Ай бұрын
Thanks man!
@Edgar-pu1lc
@Edgar-pu1lc 4 ай бұрын
Authentication in next js is the only reason why I don't use next, but if I have implemented authentication and authorization process on backend(Nest Js), what should I do in that case ? Where to save access token after successful authentication ? How to send access token along with header on each request ? In normal react this process is done very easily with axios or state manager(redux).
@farhan-app
@farhan-app 4 ай бұрын
Great video! Can you do some on design? I love the Vercel design ethos!
@rayhanislam7518
@rayhanislam7518 4 ай бұрын
Can I access the token or session from client side component. because I think httponly cookie don't access from client side.
@madhusudhansubedi
@madhusudhansubedi 4 ай бұрын
Very informative, Can you please make a more detail article on how to implement client side and server side validations on the server components during login process?
@mystickago
@mystickago 4 ай бұрын
can someone help why do we get null value in the email area
@JohnWeland
@JohnWeland 4 ай бұрын
I would love to see a deep dive on this. Admittedly I need handholding or a follow along a few time before I get things. I'd like to do something where there is a Username/Email and Password OR Google OR Github. Smurf the actual user lookup initially but then be able to plugin a database or something like AWS cognito, after the fact.
@TenzDelek
@TenzDelek Ай бұрын
on point
@chrisfaux3769
@chrisfaux3769 2 ай бұрын
For those getting user { email: null } in the session output, remember to add a name attribute `name='email'` in the email input in the `page.tsx` file
@KristianTheDesigner
@KristianTheDesigner 4 ай бұрын
More of this you ask? Yes, please 🧠👍
@nmdpa3
@nmdpa3 2 ай бұрын
In the authjs overview toward the end, one of the added security checks shown via the devtools / applications display was a CSRF token. Is the "role your own" auth in this video (using JWT and the session cookie) also in need of a CSRF token, and if so, any examples of how that would be added to the existing code example?
@Edwinil
@Edwinil 3 ай бұрын
I tryed next-auth, authjs, but this satisfied what I was looking for. thanks
@carloschavezlucero9142
@carloschavezlucero9142 4 ай бұрын
Best video ever
@joshuasingh854
@joshuasingh854 4 ай бұрын
Can this be done as easily on the pages router? Does anyone have a video or example of this? Thanks
@splodys
@splodys 4 ай бұрын
Great video as always! Are you able to explain why do we need auth.config.ts and auth.ts in nextjs new tutorial? One spreads the other, coulnd't we just have one?
@jrnxf
@jrnxf 4 ай бұрын
This is a really well done video, Lee! However, as I've mentioned before, the issues here is not reading the user data off the session, but the inability to use the updated session user to set client-side state immediately (without relying on a useEffect or a hard refresh) due to the router cache. Any update on when this can be fixed?
@alexiglesias5166
@alexiglesias5166 2 ай бұрын
I'm having the same problem ... Did you fix it? Fucking nextjs cache, it's a pain in the ass and is incredible how difficult is to deal with it, there is no information about it, not even in the documentation
@avidcaster
@avidcaster Ай бұрын
Vercel Auth would be great to complete with clerk, auth0. Something built-in and easy to use / deploy.
@DanielAzubuine
@DanielAzubuine 4 ай бұрын
Nice one.
@milesrykerodazie171
@milesrykerodazie171 2 ай бұрын
This is great.Please can you do a video on how to use nextjs and an external cookie based authentcation api (e.g nodejs express) and how to persist the cookies in deployed nextjs.Once again thank you
@GLINCKERO
@GLINCKERO 4 ай бұрын
Love the video, Can you make more video on integration for app router with next auth showing server side loading user data on page , and server side check for authenticated pages. it will be really helpfull, couldn't find any resources out there.
@MrJettann
@MrJettann 4 ай бұрын
Alright, so you set the session on frontend, but what to do if i should do it on backend with refresh and access tokens? Can you make a video about server authentication with these tokens? I can't find any valuable info about this :(
@Sus-nn8sv
@Sus-nn8sv 4 ай бұрын
Small question, suppose I login and wait for more than 10 seconds, and suppose I have a button that console.log the session variable, would it still console.log or do I need to make a hook using useState and useEffect instead?
@GhulamRasool-kr4ld
@GhulamRasool-kr4ld 4 ай бұрын
Can you create a video on how to build and deploy a production-ready application on AWS, including how to export output data? I've been struggling with this for the past few days. #nextjs #production #build
@UmairSadaqat
@UmairSadaqat 4 ай бұрын
00:01 Basic session authentication in Next.js without additional libraries. 01:15 Authentication process involves login and session creation 02:49 Asynchronous function for encrypting JWT based on payload and algorithm. 04:16 Updating session expiration and cookies for user authentication. 05:51 Implementing minimal session-based authentication in Next.js app. 07:20 Using middleware for refreshing page data 08:51 Authentication and session handling with Next.js App Router 10:15 The Next.js app router authentication is deeply complex.
@ARUNRAWAT-rh5ks
@ARUNRAWAT-rh5ks 4 ай бұрын
Pls make a video on session update when we update user details in database
@EvHaus
@EvHaus 4 ай бұрын
Thanks for the video. Biggest issue I've run into with this flow is that it seems nearly impossible to kick users out when using JWTs. Would love a video on that. ie. A bad actor signs up for my app and I need to kick them out immediately (faster than the JWT expiry date). How do I do that?
@traezeeofor
@traezeeofor 22 күн бұрын
In video, under session in , "email" returns null. This is an error/oversight on your part. Please see corrected code below: name property missing from input element. 😋. You're welcome.
@ravendelarama426
@ravendelarama426 4 ай бұрын
I have concerns, how do you authorize dynamic routes do you use regex how do you impelemtn it in middleware? Just starting out
@palockocz
@palockocz 2 ай бұрын
Can you please make a page transition video using next.js v14 with app dir and framer motion? Including exit animation?
@user-uy6vc7de2n
@user-uy6vc7de2n 4 ай бұрын
Love u dude
@wuffgang5333
@wuffgang5333 4 ай бұрын
with rsc there is only cookies possible? or would it just be more work to send auth header explicitly and verify in middleware but possible?
Next-Auth on App Router - Solid Auth, Super Fast
17:20
Jack Herrington
Рет қаралды 103 М.
She ruined my dominos! 😭 Cool train tool helps me #gadget
00:40
Go Gizmo!
Рет қаралды 59 МЛН
МАМА И STANDOFF 2 😳 !FAKE GUN! #shorts
00:34
INNA SERG
Рет қаралды 3,6 МЛН
Popular Technologies that Won't be Around Much Longer...
14:36
Sideprojects
Рет қаралды 144 М.
Why is JWT popular?
5:14
ByteByteGo
Рет қаралды 283 М.
Next.js App Router Caching: Explained!
25:22
Vercel
Рет қаралды 83 М.
The ARCH LINUX Experience
34:30
Bog
Рет қаралды 85 М.
Cool Tools I’ve Been Using Lately
23:11
Theo - t3․gg
Рет қаралды 185 М.
10 common mistakes with the Next.js App Router
20:37
Vercel
Рет қаралды 187 М.
💅🏻Айфон vs Андроид🤮
0:20
Бутылочка
Рет қаралды 736 М.
Lid hologram 3d
0:32
LEDG
Рет қаралды 8 МЛН
YOTAPHONE 2 - СПУСТЯ 10 ЛЕТ
15:13
ЗЕ МАККЕРС
Рет қаралды 97 М.
Samsung Galaxy 🔥 #shorts  #trending #youtubeshorts  #shortvideo ujjawal4u
0:10
Ujjawal4u. 120k Views . 4 hours ago
Рет қаралды 11 МЛН
Cadiz smart lock official account unlocks the aesthetics of returning home
0:30