Authentication Flow in Next.js (Complete Tutorial)

Поділитися
Вставка
  • Опубліковано 10 січ 2025

КОМЕНТАРІ • 91

  • @rog0079
    @rog0079 2 місяці тому +58

    Finally, someone not using 3rd party libraries to implement authentication. Thanks for this!

    • @karacan2469
      @karacan2469 2 місяці тому +5

      You've made an excellent point! The reliance on third-party libraries has become so common that I've developed a bit of an aversion to using many of them myself. This creator is a skilled developer, and the video is incredibly useful. I hope they continue making content like this!

  • @yonathanhailemelekot5238
    @yonathanhailemelekot5238 2 місяці тому +12

    The most underrated React/Next.js YT channel. you deserve more recogition Sir.

  • @hikmetmelik
    @hikmetmelik 2 місяці тому +7

    Sir, you are the best instructor ever. I started a brand new project with Next.js and you uploaded a fresh new video right on topic. Thank you! (I commented before watching it, I'm sure it's great anyway ) 🔥

  • @stacksagar
    @stacksagar 2 місяці тому +2

    as a developer, for me one of the best nextjs auth concept tutorial with clean and clear

  • @kunnykk6468
    @kunnykk6468 2 місяці тому +2

    Finally, the Next.js course is up! I knew you’d come through, dude🔥

  • @ShaynPutnam
    @ShaynPutnam 2 місяці тому +1

    Literally just found this dude yesterday, and today he puts out a video of EXACTLY what I was looking for!!!

  • @insensibility
    @insensibility 2 місяці тому

    I was just watching one of your shorts, wishing you could do NextJS. You're awesome, man. I keep on recommending you to people wanting to learn React and Tanstack. You're the GOAT!

  • @denisshvets7768
    @denisshvets7768 2 місяці тому +1

    Just in time. I was researching the topic and this video came out. What a luck. Thanks.

  • @victoreleanya4375
    @victoreleanya4375 Місяць тому +1

    Thanks for sharing this. I am just in the middle of implementing my own custom auth

  • @mohamedsalimbensalem6118
    @mohamedsalimbensalem6118 2 місяці тому

    you're the best instructor I've ever found! I've been watching your tutorials since you had 1K subscribers. I'd really appreciate it if you could cover more about React Native.

  • @BalaMathias
    @BalaMathias 2 місяці тому

    You just got yourself a subscriber!
    This is dope! I've always wanted something like this!
    Thank you so much. 👏

  • @MrDerterminator1992
    @MrDerterminator1992 2 місяці тому

    Thank you so much for showing the bare minimum to really understand the required steps in auth.
    FYI, you get the pending state as the third argument directly from useActionState hook.
    const [state, formAction, isPending] = useActionState(fn, initialState)

  • @farzany
    @farzany Місяць тому

    Fantastic video! Thank you for clearly explaining every step!

  • @jaecheokkim99
    @jaecheokkim99 Місяць тому

    Very clear and useful really appreciate it!

  • @merlinmeraldz
    @merlinmeraldz 2 місяці тому

    I really like your videos 🎉
    Hopefully we get more NextJs videos. Thanks a lot for a well explained content.

  • @shakil-the-coding-monster
    @shakil-the-coding-monster 2 місяці тому

    very rare video on youtube. Thanks a lot!!

  • @WallQ
    @WallQ 2 місяці тому

    I make the compliments of everyone my own, thank you for this

  • @Amitkumar-ps1dt
    @Amitkumar-ps1dt 2 місяці тому +1

    I am from India I am big fan of you and your tutorial specific on React, and Next
    Kindly make some videos on node, express and Mongo also
    Your videos helps a lot
    Thanks
    Love from India

  • @serlismaldonado
    @serlismaldonado 2 місяці тому

    This a great tutorial. Thanks!

  • @Q-Productions23
    @Q-Productions23 2 місяці тому +11

    Hey, Bro Could you Please make this but in the scenario where Nodejs is the backend and Nextjs Frontend?

  • @gagancg1433
    @gagancg1433 2 місяці тому +1

    Great Video finally ! It would be nice to have auto refresh token implementation in nextjs

  • @jayelemartermulo8574
    @jayelemartermulo8574 2 місяці тому

    thank you so much. you make it such that i can make a nextjs app with a separate backend without depending in 3rd party library

  • @jujudev7
    @jujudev7 2 місяці тому

    Someone knows how to see details on hover (14:36) in VS Code ? The name of the extension please ? 🙏
    Thank U for the Tuto 🙌

  • @balduin_b4334
    @balduin_b4334 2 місяці тому +1

    did you verify that the middleware is working on a vercel deployment of nextjs?
    i dont think it works because jose isnt edge runtime compatible.

  • @bharatdubey83
    @bharatdubey83 2 місяці тому +2

    WHEN WILL THE NEXT JS COURSE COME

  • @idrisalparslan2312
    @idrisalparslan2312 2 дні тому

    Thanyou mr! u are a life saver

  • @kennethlau8108
    @kennethlau8108 2 місяці тому +1

    can this tutorial to be redone in nextjs 14 without useActionState as nextjs 15 is still quite buggy for production.

  • @Vicsory
    @Vicsory 22 дні тому +1

    Finally 😮🎉🎉🎉

  • @ntone.s
    @ntone.s 2 місяці тому +1

    Thanks for another great tutorial! Tried to run the project but received an error in terminal "Failed to verify session" after adding middleware.ts therefore login didn`t work.
    Also in session.ts two functions create and delete wants cookies to be awaited. Could you help please?

  • @Farruh_13
    @Farruh_13 2 місяці тому +1

    Do you plan to make a video about Front-end architecture?

  • @sonuinnervoyage44
    @sonuinnervoyage44 Місяць тому

    Great video about the vanilla next authenttication! it would have been more better if you could've added how to store the user details and consume it in the components whenever required.

  • @codesius
    @codesius 2 місяці тому +1

    I believe useActionState also now returns isPending state (the old useFormState did not), so I don't think you need to rely on useFormStatus just for pending anymore. I could be wrong though. It's been some time since I read the docs on it.

    • @LehmannMr
      @LehmannMr 2 місяці тому

      You should take a look at next-safe-action. This takes it even a step further.

  • @dimitriss3635
    @dimitriss3635 Місяць тому

    So if then you wanted to get the user data you would get his is from the token and then fetch his data using the id right ?

  • @LegSpinDrift
    @LegSpinDrift 26 днів тому

    how can you implement the refresh token functionality in this? when i create a session i get 2 tokens "access token" and "refresh token" .. when the access token expires i need the refresh token to make an api call and get the new access token and refresh token

  • @_andrewpeacock
    @_andrewpeacock 2 місяці тому

    Would *LOVE* to see some more content on Remix or the new Tanstack Start

  • @doomguy2135
    @doomguy2135 2 місяці тому +1

    How to use session in client components??

  • @joelcontreras4629
    @joelcontreras4629 2 місяці тому

    Thank you! This is just what I need but how do you resolve the hydrated warning?

  • @muhammadobaidullahkhan5887
    @muhammadobaidullahkhan5887 2 місяці тому

    Thanks a lot buddy! I was just trying to add auth in my on going project and found ur tutorial, Thanks again <

  • @halfmoonvibes
    @halfmoonvibes 28 днів тому

    how the client components are supposed to get access to the data that is unique to a user for example a database if they don't have access to cookies?

  • @maxkabechani
    @maxkabechani 15 днів тому

    Trying to do this but with separate backend. On login, I’m receiving the set-header but the server action doesn’t set the header in the client

  • @thatcoldboy
    @thatcoldboy 21 день тому

    Really nice tutorial, please can you do this with an external backend written in django, nodejs, etc. and also show how refresh tokens will be implemented

  • @rgagankumar5191
    @rgagankumar5191 2 місяці тому

    Great tutorial.
    Confusing part is how do i extend the session expiry time, when they are logged in
    Need help on this.. 🙏🏻🙏🏻

  • @novianto2346
    @novianto2346 2 місяці тому

    How about using react hook form for form handling? Can we combine the server action with react hook form?

  • @tomasburian6550
    @tomasburian6550 Місяць тому

    The source code already has the finished code in it. Is there no repo with the starter code?

  • @juansimonmerlo6854
    @juansimonmerlo6854 2 місяці тому

    Excuse me i'm having a problem with useActionState and useFormStatus. I've already installed the same dependencies you had but I still get this error: "The 'react' module doesn't have any 'useActionState' exported.
    Same error with react-dom and useFormStatus.
    Thanks!

  • @modelzero5463
    @modelzero5463 Місяць тому +1

    where is project start code?

  • @harou22
    @harou22 2 місяці тому

    Can you not crash the server by spoofing the formData requests and sending e.g. null instead? Object.fromEntries(null) will throw with type error.

  • @patrickamaechi1881
    @patrickamaechi1881 2 місяці тому

    would be great to also maybe include refresh token rotation just to extend the feature already implemented

  • @niklausmikaelson7332
    @niklausmikaelson7332 2 місяці тому +1

    Refresh token ?

  • @MrR4P70R
    @MrR4P70R 2 місяці тому

    I'm surely missing something but, how do you call/categorize "jwt" when you say "without any 3rd party library" ?

    • @himankverma4617
      @himankverma4617 16 днів тому

      Without libraries like nextauth,Oauth,or clerk

  • @sidalisaadi1961
    @sidalisaadi1961 2 місяці тому

    Great video!
    I would like to know how auth solutions like clerk work, do you have to make a request for their api each time you need to verify the auth?
    how does the hooks to get user work..?

    • @cosdensolutions
      @cosdensolutions  2 місяці тому

      You use their API as a backend, and then they also store cookies that the hooks use. It's basically what we did but they manage all for you and expose some hooks and components to use

  • @mounir101
    @mounir101 Місяць тому

    Thanks bro, you're great and love the explanation. Please, could you do the same process using NextAuth lib?

  • @Sardorbek-Safarov
    @Sardorbek-Safarov 2 місяці тому

    How can we get the token if we need to use it on client side?

  • @nunoalmeida1970
    @nunoalmeida1970 Місяць тому

    Very cool

  • @StarkElessar
    @StarkElessar 2 місяці тому

    Thanks, but you didn't show getme - how do you know I'm logged in, you didn't show token update, interceptor on 401

  • @skia515
    @skia515 Місяць тому

    thanks for this video
    how can I check if user is logged in in home page? like if user logged in ? print his email other wise show login button

  • @고민영-n3h
    @고민영-n3h 23 дні тому

    how to access the token in client component?

  • @skia515
    @skia515 Місяць тому +1

    Seriously, I can't believe a big framework like NextJs don't have own authentication & authorization in-Built, programmer should make own auth system from scratch or using 3rd party libraries.

    • @youarethecssformyhtml
      @youarethecssformyhtml Місяць тому

      Not only Next but also other frameworks like Nuxt and Remix and so many JS frameworks

  • @cb73
    @cb73 2 місяці тому

    Good stuff… just a little side note on login validation. There is no reason to validate passwords when logging in.

  • @datle2863
    @datle2863 2 місяці тому

    Is the middleware file actually used here or it is just for server simulation? I dont see you use it anywhere so im curious

    • @cosdensolutions
      @cosdensolutions  2 місяці тому

      Next.js automatically uses it and runs it before every request

  • @void-deus
    @void-deus 2 місяці тому +1

    wow thanks

  • @vikashmaurya9824
    @vikashmaurya9824 2 місяці тому

    How I can implement csrf token in nextjs with authentication?

  • @HariNarayan-c4w
    @HariNarayan-c4w 9 днів тому

    But how can I get details of logged In user from session using auth provider?

    • @HariNarayan-c4w
      @HariNarayan-c4w 9 днів тому

      And where can I store my bearer token which I received from the API?

  • @hieunguyen0801
    @hieunguyen0801 2 місяці тому

    Can you make a video tutorial on custom fetch of nextjs like axios in a real project?

  • @waleedsharif618
    @waleedsharif618 2 місяці тому

    Same steps with NextJS 15 ? Which released today

  • @mohibahmed5098
    @mohibahmed5098 10 днів тому

    how do we get session?

  • @mehmethanifierenler3612
    @mehmethanifierenler3612 2 місяці тому

    very very thanks

  • @tonyvito5062
    @tonyvito5062 2 місяці тому

    Not secure tho and could have a lot of issues since you're not refreshing that jwt token

  • @dasarimanoj3086
    @dasarimanoj3086 2 місяці тому

    Are we actually getting Next.js tutorials on this channel?

    • @cosdensolutions
      @cosdensolutions  2 місяці тому

      What do you mean?

    • @dhanrajshetty645
      @dhanrajshetty645 2 місяці тому

      ​@@cosdensolutions Since you post react videos that's why.

    • @cosdensolutions
      @cosdensolutions  2 місяці тому

      Yeah sure, but that comment felt like it was a bad thing and I just wanted to clarify and understand

    • @dasarimanoj3086
      @dasarimanoj3086 2 місяці тому

      @@cosdensolutions Noo 🥲sorry if I have framed the question in that way. I was asking can we expect more of nextjs content on this channel further going is what I was requesting you 😅

  • @seagull2345
    @seagull2345 2 місяці тому

    Same thing for React Native, please.

  • @kinggrey2511
    @kinggrey2511 2 місяці тому

    Can we have more interviews like proper interview I got asked questions like event loop, event bubling difference between http server and express etc stuff I know it was a kinda big company but iam a 3rd students like who ask those questions. I prefer these kind of interview questions please also thanks for your hooks playlist.❤❤❤❤

  • @vira5995
    @vira5995 2 місяці тому

    next video on nextauth Please🙏

  • @albaprogrammer2710
    @albaprogrammer2710 17 годин тому

    leh ugha

  • @EmilWall
    @EmilWall 2 місяці тому +1

    passwrong 😂😂

  • @abuzain859
    @abuzain859 2 місяці тому +1

    Your solution is solid, but the logic in the middleware isn’t fully accurate in a few production cases. I’d submit a pull request, but since you’ve combined all the video code into a single standalone repo, it’s not feasible.
    Here’s the issue with your middleware code:
    typescript
    Copy code
    if (isPublicRoute && session?.userId) {
    return NextResponse.redirect(new URL("/dashboard", req.nextUrl));
    }
    Currently, this line redirects the user to the dashboard if they’re logged in and trying to access a public route. However, consider that some pages-like marketing or landing pages-should be accessible to all users, regardless of login status. This code would redirect logged-in users to the dashboard, which isn’t always correct.
    A better solution would be to categorize pages into three main types: protected, public, and neutral. Then, if the user is trying to access a public route and has a session ID, redirect them.

  • @lorena4293
    @lorena4293 Місяць тому

    has anyone else had this error:
    Property 'set' does not exist on type 'Promise'.ts(2339)
    session.tsx?

  • @m7mdnho154
    @m7mdnho154 2 місяці тому

    NextResponse.redirect("/login") throws an error. I solved it by doing NextResponse.redirect(new URL("/login", res.url))