NextJS Authentication with Server Actions - Learn to build from scratch!

Поделиться
HTML-код
  • Опубликовано: 12 сен 2024
  • In this video we create an entire JWT authentication system using NextJS server actions.
    Source Code - github.com/Cod...
    NextJS Install - nextjs.org/doc...
    NextJS Route Handlers - nextjs.org/doc...
    NextJS Server Actions - nextjs.org/doc...
    NextJS Middleware - nextjs.org/doc...
    Prisma JS Getting started - www.prisma.io/...
    Jose - www.npmjs.com/...
    Bcrypt JS - www.npmjs.com/...
  • НаукаНаука

Комментарии • 74

  • @codingwithrobby
    @codingwithrobby  5 месяцев назад +3

    Do everything in server actions! ❤

    • @junaid-alyyy
      @junaid-alyyy 4 месяца назад

      Hi youtube.com/@codingwithrobby . How to remove cookies if the user session is expired. In my use case, I want to remove cookies instead of redirecting the user. Need your help, if you can reply. Thanks

    • @codingwithrobby
      @codingwithrobby  4 месяца назад

      @@junaid-alyyy cookies().delete('Authorization') This should work fine in middleware or a server action

    • @junaid-alyyy
      @junaid-alyyy 4 месяца назад

      @@codingwithrobby - Thanks for the quick response. It works with a server action but I want to validate the user session using middleware and if the session is expired or invalid then the cookie should be deleted but this deletion isn't possible `cookies().delete('Authorization')` in the middleware.

    • @user-xg5sf4pf6o
      @user-xg5sf4pf6o 3 месяца назад

      @@junaid-alyyy I also encountered this problem, what should I do?

    • @user-xg5sf4pf6o
      @user-xg5sf4pf6o 3 месяца назад

      import { cookies } from "next/headers"; belongs to the server-side API, but logout is a client-side operation

  • @summersnow2288
    @summersnow2288 3 месяца назад +10

    I don't understand why do you use api within the action, the whole point of action is to get rid of api?

  • @user-qe5oe8jz4y
    @user-qe5oe8jz4y 6 месяцев назад +8

    Straight to the point. Thanks so much

  • @stefanlemke7858
    @stefanlemke7858 4 месяца назад +2

    Absolutely love the way you are explaining things! Please keep up the great work.

  • @RobertBojor
    @RobertBojor 5 месяцев назад +6

    Thanks for this! But I do have a question... Wouldn't be easier to just use the server action to do all the work instead of having it offload the work to the API route? Based on my understanding of server actions, when you submit the form and the server action is called, under the hood there's a POST request being made to a hidden route (the server action). Daisy chaining the reuests like this might introduce more lag since two calls are being made, not to mention you have to account for errors in both methods.

    • @macccu
      @macccu 4 месяца назад +4

      Yeah this tutorial makes no sense. Just a proof that you should not trust any random youtuber for programming knowledge. Soon avg TS code tutorial it's going to be worse than PHP 20 years ago. Type safety is not going to help anyone if people are just going to do stupid things.

    • @newborn7348
      @newborn7348 2 месяца назад +1

      Yeah the whole idea of using a server action is so that you wouldn't need to create an api route for things like this.

  • @AndrulisTravel
    @AndrulisTravel 6 месяцев назад +1

    Very nice :) So if you have time, the next interesting things are 'Forgot password', sending a verification link when a new user registers, to prevent false registrations, and a Google login option :)

  • @desmondcoffie65
    @desmondcoffie65 2 месяца назад

    awesome video, straight to the point . love the way you signed out with peace

  • @dzseroldfabian4574
    @dzseroldfabian4574 5 месяцев назад +1

    You are an honest, true hero. Keep it up Chief

  • @Keifeh.P
    @Keifeh.P 5 месяцев назад +2

    Exactly what I was looking for, thank you!

  • @deanelie7775
    @deanelie7775 6 месяцев назад +1

    Glad to see this video on my youtube homepage, thank you for this video. ☺

  • @Lovedeep555
    @Lovedeep555 Месяц назад

    Its very straight forward, thanks

  • @marztianpapi3419
    @marztianpapi3419 Месяц назад

    Great video bro!

  • @eternl_sunshine22
    @eternl_sunshine22 3 месяца назад

    fantastic, i managed to follow you throughout the tutorial even if it did take me a while since i had to figure out using drizzle orm but everything was great! will try to implement jwt refresh tokens now

    • @divinecharles4102
      @divinecharles4102 2 месяца назад

      Hey man, have you figured out the refresh token part?

  • @kpeople96
    @kpeople96 День назад

    Love the sound of your keyboard. Can you share a keyboard name and link please?

  • @2pacrapempire866
    @2pacrapempire866 5 месяцев назад

    Man thanks a lot. was looking for this. Got it at the right time

  • @MehranRoham
    @MehranRoham 2 месяца назад

    Thanks so much Robby, this video helped me a lot

  • @tshepokhumako1403
    @tshepokhumako1403 5 месяцев назад +1

    You so real. Thanks

  • @newbiedevtons
    @newbiedevtons 3 месяца назад

    Very helpful and simple to understand

  • @alexvoid8717
    @alexvoid8717 5 месяцев назад +1

    I'm new to server actions and still trying to understand them. Aren't they supposed to be independent of APIs/routes and be able to perform database operations directly within themselves?

    • @TomislavKimovski
      @TomislavKimovski 5 месяцев назад

      I was thinking the same when I saw that part. No need for the fetch call to the api endpoint because you are already on the server, just do a db lookup.

  • @dzseroldfabian4574
    @dzseroldfabian4574 5 месяцев назад +1

    How to Log out? and how to read user data from cookies?

  • @w.arajinda2968
    @w.arajinda2968 6 месяцев назад

    thank to you i can continue my projects,keep it up
    👍👍👍👍👍

  • @Leonavancini
    @Leonavancini 2 месяца назад

    Thanks for this explanation, I have only one question, how can I add refresh token with this approach? can you give me a hint? thanks in advance

    • @iteratus
      @iteratus Месяц назад

      Another useful google output: www.google.com/gasearch?q=nextjs%20server%20actions%20bearer%20token%20authorization&source=sh/x/gs/m2/5#ip=1 even though the results are probably not what you're expecting, it points you in the right direction

  • @ItachiUchiha-w5h
    @ItachiUchiha-w5h 6 месяцев назад +1

    I'm getting a
    Error:p1001: can't reach database server at `localhost`:`5432`
    How do I solve it

    • @codingwithrobby
      @codingwithrobby  6 месяцев назад

      Do you have Postgres installed locally? You could also just get a database at www.elephantsql.com and use the connection string they give you

    • @ItachiUchiha-w5h
      @ItachiUchiha-w5h 6 месяцев назад

      @@codingwithrobby yea that was the problem I solved it thanks..

  • @iuliancarnaru954
    @iuliancarnaru954 6 месяцев назад

    nice one, helpful information

  • @muhammadijaz6042
    @muhammadijaz6042 28 дней назад

    Please, guide me if possible, How to add subscription based payment method that can be customised to any payment solution available all over the world. Take care.

  • @faxa22
    @faxa22 6 месяцев назад

    Hi! Really love your content. In intro to App dev you said that tried creating it with golang from scratch. Can you do a video on this topic?)

  • @cyphermediaschool
    @cyphermediaschool 5 месяцев назад

    How to protect api no one access , we can access only same origin 😢 in next js 14

  • @financialwonton
    @financialwonton 24 дня назад

    Can you do captcha with google?

  • @financialwonton
    @financialwonton Месяц назад

    Good job

  • @xiaomaitian
    @xiaomaitian 4 месяца назад

    With server actions, does it feel like you don't need route handlers

  • @the.money.mentor.825
    @the.money.mentor.825 2 месяца назад

    isnt it a bad idea to store the db connection string in an env variable? it can be accessed on the front end. I'm going to watch this video and put it all in the backend and secure it with a public api key and then only allow calls from a certain ip.

    • @iteratus
      @iteratus Месяц назад

      .env variables are not accessible by FE per default. So we're fine here

  •  Месяц назад

    I liked you video thanks

  • @mecobrabo5590
    @mecobrabo5590 5 месяцев назад

    awesome content. can you explain why you moved all the logic to an route handler when you can keep it in the server actions?

    • @agusjkdev
      @agusjkdev 5 месяцев назад

      I'd say he wanted to keep it simple, after all is not that bad

    • @mecobrabo5590
      @mecobrabo5590 5 месяцев назад

      @@agusjkdev dont think so, adding one more step (the router handler)

  • @victorchukwu6671
    @victorchukwu6671 Месяц назад

    Thanks so much

  • @alexdin1565
    @alexdin1565 Месяц назад

    thanks for this amazing content but the only problem is your server actions are not protected so any one can see because you export them

  • @BiteAByte-xy3ek
    @BiteAByte-xy3ek 3 месяца назад

    Can you implement "Refresh token" and "Token rotation" to this code? seems to be fun.

    • @divinecharles4102
      @divinecharles4102 2 месяца назад

      Hey man, have you figured this out now?

    • @BiteAByte-xy3ek
      @BiteAByte-xy3ek 2 месяца назад

      @@divinecharles4102 Yes, I may create a video about it

  • @MusaRehman-m9o
    @MusaRehman-m9o 6 месяцев назад

    thanks needed this

  • @iancarr3923
    @iancarr3923 5 месяцев назад

    Excellent!

  • @w3mw
    @w3mw 4 месяца назад

    Great video thanks, but if you have a user and want to display the information or store it somehow in a useContext to be able to use the users information around the app, will I just put cookies().get("Authentication") into a authContext then?

  • @smexiking8620
    @smexiking8620 6 месяцев назад

    Hey robby, which operating system you're using? I sense it's linux but which distro?
    Thanks

  • @user-xg5sf4pf6o
    @user-xg5sf4pf6o 3 месяца назад

    How to Log out?

  • @BoomOzi
    @BoomOzi 2 месяца назад

    how to logout?

  • @dimasandrianoh3811
    @dimasandrianoh3811 6 месяцев назад

    Thanks sir, but i have question. why u still create api route if only with server action can create user or login (logic in server action)?

    • @codingwithrobby
      @codingwithrobby  6 месяцев назад +2

      Yeah... I suppose that would work too 😬. I guess this just adds a little more flexibility if you wanted to open it up externally down the line.

    • @victor63666
      @victor63666 6 месяцев назад

      I believe that when utilizing server actions, you are essentially making a POST request to an endpoint representing the server action function. In this context, it seems like you are engaging in two fetch operations. I think you can invoke this function within both the signup route and the signup server action.
      @@codingwithrobby

    • @TomislavKimovski
      @TomislavKimovski 5 месяцев назад

      Not really needed, but it was good to see how to set up api endpoints if needed.

    • @TomislavKimovski
      @TomislavKimovski 5 месяцев назад

      Brilliant, loved it! Referencing the docs was a great idea, made everything look so simple.

  • @CarltonJosephLv
    @CarltonJosephLv 6 месяцев назад

    Thanks!

  • @prashlovessamosa
    @prashlovessamosa 6 месяцев назад

    Thanks

  • @macccu
    @macccu 4 месяца назад +1

    Why are you making server-side request in server action to your own api? This makes no sense. Please stop.

  • @ahmedmrsawy9215
    @ahmedmrsawy9215 5 месяцев назад +2

    Sorry but using server action to make a request to a route handler is so DUMP

  • @chulada03
    @chulada03 6 месяцев назад

    Thanks