Custom Login Page in Next-Auth and Next.js 13 Authentication : Complete Step-by-Step Tutorial

Поділитися
Вставка
  • Опубліковано 20 лип 2024
  • In this tutorial video, you will learn how to create a custom login page using Next-Auth and Next.js 13 authentication and how to configure the Next-Auth to use the custom login page. We will cover the basics of setting up Next-Auth and integrating it with our Next.js application. Then, we will dive into creating a stylish and user-friendly login page that enhances the overall user experience. By the end of this video, you'll have the knowledge and skills needed to implement a custom login page in your own projects, providing a seamless and secure authentication process for your users.
    0:00 Intro
    0:16 What we are going to do
    3:57: What is the "signIn()" function in Next-Auth
    5:28 Start to create the custom login page
    10:58 Quick recap of what we have done
    12:09 Outro
    Github Repo: github.com/vahid-nejad/next-a...
    Subscribe to my channel / @sakuradev
    Detailed video about next-auth: • Authentication with Ne...
    Role-Based Authorization with next-auth: • Role Based Authorizati...
    How to Manage Backend JWT Access Tokens in Next Auth and Next.js 13: • How to Manage Backend ...
    my discord server: / discord

КОМЕНТАРІ • 125

  • @branislannjemec9050
    @branislannjemec9050 Рік тому +8

    Bro you are the best!

    • @SakuraDev
      @SakuraDev  Рік тому

      Thanks for your positive comment.🌷🌷🌷.

  • @sinan245
    @sinan245 Рік тому +2

    Most detailed explanation for the most important part (authentication) of the all applications. Thanks...

    • @SakuraDev
      @SakuraDev  Рік тому

      Thanks for your nice feedback 🌹🙏

  • @scalitics
    @scalitics Рік тому

    Thanks to your videos and the basics I had, I was able to start developing my application, thank you so much!

    • @SakuraDev
      @SakuraDev  Рік тому

      Hi, Thanks 🙏🙏, I am really happy that it was helpful for you 🌹😊

  • @aditraanggra4252
    @aditraanggra4252 11 місяців тому +1

    This video I looking for, thanks, Dude!

    • @SakuraDev
      @SakuraDev  11 місяців тому

      Glad I could help!🌷and welcome to my channel 🖐🏻

  • @saman6199
    @saman6199 Рік тому +2

    Thanks so much for detail walkthrough.

    • @SakuraDev
      @SakuraDev  Рік тому +1

      Thanks for your nice feedback

  • @gamescope2607
    @gamescope2607 8 місяців тому

    Hello, thanks for the video. I have a question about how to implement multiple custom logins for different user roles, such as clients and administrators. Can you provide some guidance on this?

  • @benberkayozdemir
    @benberkayozdemir 10 місяців тому +1

    really clear explanation. thanks!

    • @SakuraDev
      @SakuraDev  10 місяців тому

      Glad you enjoyed it!

  • @Monstermash355
    @Monstermash355 10 місяців тому

    How can i make a toast appear after success login?
    If i invoke the toast function in the onsubmit it gets lost because the component unmount

  • @gosnooky
    @gosnooky 9 місяців тому +1

    Thank you. NextAuth is by far the most overly complicated library I've used in 20 years. It seems that with the entire Next ecosystem, if you need to do something custom, it's a nightmare. I guess if you're not using OAuth or deploying to Vercel they don't wanna know ya.

    • @SakuraDev
      @SakuraDev  9 місяців тому +1

      Yes, I am agree, specially the docs is really confusing. what stack do you use for your authentication?

  • @ICOReviewtoken
    @ICOReviewtoken Рік тому +1

    Custom login like this, there are many video tutorials, now I want the login form to be a popup when I click on a button on the header, what should I do?

    • @SakuraDev
      @SakuraDev  Рік тому +1

      Hi, well that's easy. You should use a modal component for login. But you can't use the signIn() function to evoke the login component. You have to evoke the login component manually however you should use signIn() func with its argumemt inside the login button of login component.

  • @huynhanhkietk17hcm15
    @huynhanhkietk17hcm15 10 місяців тому

    I see when log in using your code then there is a moment that there is still the login text. How can I SSR the useSession first so that it only show either the login text or the user in the app directory as getServerSideProps is not supported so I couldn't pass the session. That would help a lot.

    • @SakuraDev
      @SakuraDev  10 місяців тому

      Hi, In server components, to get the session you should use the getServerSession from next-auth. You can see my latest video for more insights.

  • @randomforest_dev
    @randomforest_dev Рік тому

    First of all, thank you very much for this awesome tutorial 🙏. By the way, do you know how to customize the error messages ? For example, I want to show password incorrect message or server error message or email already used, etc,. on login page, not on a separate error page, without a redirect etc? I been checking their docs and other places the whole day but without any proper solution yet .

    • @SakuraDev
      @SakuraDev  Рік тому +1

      Hi, Thanks for your feedback. for custom messages in login page you can watch this ua-cam.com/video/xJ6efexXJ7I/v-deo.html
      @ 13:48 I have explain that.

  • @jessdhoctor7834
    @jessdhoctor7834 Рік тому +2

    that's cool bro, but you should add validation i like your video about NextAuth but sometimes you missed the important thing, which is the validation. I hope you will continue creating video about Nextjs 13 and NextAuth

    • @SakuraDev
      @SakuraDev  Рік тому +1

      Thank you for your feedback and support! I'm glad that you found my NextAuth tutorial helpful. Your suggestion about adding validation is well-received, and I'll make sure to cover it in my future videos. I appreciate your support and hope to continue creating content that meets your needs and interests.

    • @jessdhoctor7834
      @jessdhoctor7834 Рік тому

      ​@@SakuraDev sure bro, im new to NextAuth and since that server side i don't see what is happening behind that logic i can't run console log on [...nextauth] and you explained it well i hope you will create 3 auth provider that use only 1 email like my email that i use to credentials is same i use with google and facebook that with back-end api like on your other video.

  • @freesyrian6732
    @freesyrian6732 Рік тому +1

    nice man
    But what if i wanna 2 login pages, one for normal user, the other one for only portal admin login, how can i implement it with next-auth ?

    • @SakuraDev
      @SakuraDev  Рік тому

      I will take a look and see how can i do that... if i find a solution I let you know.

  • @cyrusrohail9243
    @cyrusrohail9243 9 місяців тому +1

    What happens when the credentials are invalid? Does it redirect to some other page? I am using formik for form validation and I need to show login failed error on the same page, is there any way to do that?

    • @SakuraDev
      @SakuraDev  9 місяців тому

      It will be back to sign-in page, with error message in url, you can grab the error from url

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

    I followed exact same steps. However in my custom login page if i login. It does not create a session token. But if i login on the page generated by next auth at route api/auth/signin it works perfectly fine. What could be the issue? Ps i am on next js latest version and using app directory.

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

      Hi, do you call await signIn ()?

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

      @@SakuraDev yeah. I fixed it. Turns out you need to provide csrf token while calling the signIn method.

  • @saman6199
    @saman6199 Рік тому +1

    Would it be possible to tell me, how can I redirect dynamically after successful signIn? Lets say user click the protected route and we redirect the user to login page. How can I redirect the user to the page that was protected before signIn, it needs to be dynamic, because we don't know which route user coming from. I would really appreciate you help

    • @SakuraDev
      @SakuraDev  Рік тому

      In login form we can access to callbackUrl as a search param in then redirect to user to it.

    • @saman6199
      @saman6199 Рік тому +1

      @@SakuraDev thanks mate, your the best. I have a feeling that you are Iranian, I just assumed from your last name. If you are, cheshe maei, otherwise thanks for you help mate.

    • @SakuraDev
      @SakuraDev  Рік тому +1

      chese maiee 😁

  • @yvanjuleskana1350
    @yvanjuleskana1350 Рік тому

    Tank You!🙏
    I wanted to see how you handle form errors! Because by default nextauth redirects to a page dedicated to errors. It would have been easier to display them on the same page

    • @ahmedelhelw3147
      @ahmedelhelw3147 Рік тому

      If you know please tell me 😢

    • @hvilelaeng
      @hvilelaeng Рік тому

      it seems, on pages: key at [...nextauth]/route.tsx, you can add error: '/auth/error' for example, to deal with errors in a dedicated page.

  • @antoniors
    @antoniors Рік тому

    Guys, by wrapping all components with the Providers component, doesn't our application become completely client side application?

    • @joematkin2554
      @joematkin2554 Рік тому

      nope, server components stay server components

    • @mediacreatif
      @mediacreatif Рік тому

      @@joematkin2554 that's why it's pretty cool man :)

  • @vidhanshuborade5977
    @vidhanshuborade5977 11 місяців тому

    Thank you bro 💖💖

    • @SakuraDev
      @SakuraDev  11 місяців тому +1

      Thank you too! 🌷

  • @bozzhik
    @bozzhik 22 дні тому

    is this app router tutorial or pages router? i can not understand, because u have both folders

    • @SakuraDev
      @SakuraDev  22 дні тому

      This video was for the time when the app dir was not yet stable so we couldn't put the API dir in app dir. Please watch the latest version of this video through this link 🔗: ua-cam.com/video/g6S-XZxq9Ug/v-deo.html

  • @arielduarte719
    @arielduarte719 11 місяців тому

    Thanks a lot pal

    • @SakuraDev
      @SakuraDev  11 місяців тому

      You're welcome! If you have any more questions or need further assistance, feel free to ask. Happy to help! 😊👍

  • @tunaralyarzada
    @tunaralyarzada Рік тому +1

    Hey brother, I have an issue with getServerSession() function when i call it in api route handler, it returns null. So i cant get session info in my api handler files. However in server side and client side components it successfully return session object. Only problem is in api route files. Btw, i am using last next js 13 version which has api folder in app dir. Do u know the reason why it happens?

    • @SakuraDev
      @SakuraDev  Рік тому +1

      Hi, I am currently looking for a solution and as soon as find one, I will let you know. 🌷🌷

    • @tunaralyarzada
      @tunaralyarzada Рік тому +1

      @@SakuraDev allright man, i'll be waiting for ur feedback. Thanks!

    • @internetdrew
      @internetdrew Рік тому

      Have you tried doing a post request to your API route and pushing the session from SSR in the body of the POST?

    • @tunaralyarzada
      @tunaralyarzada Рік тому

      I need to get session on get request handler and yeah it tried to send session from ssr through get request headers but again in route handler i got undefined

    • @tunaralyarzada
      @tunaralyarzada Рік тому

      The weird side is that, when i send get request through chrome browser like with search bar, i got session object in console successfully. The problem is, when i send it with axios or fetch it return null

  • @hamed4451
    @hamed4451 11 місяців тому

    callbackUrl cause reload and redirect to the custom url .. is it ok to reload whole the app??

    • @SakuraDev
      @SakuraDev  11 місяців тому +1

      It is just way next auth redirect. In order to prevent refreshing the page, set redirect:false and then use redirect function of next.js router to redirect to callback url

    • @hamed4451
      @hamed4451 11 місяців тому

      @@SakuraDev I've set redirect false and use callbackurl in Next's redirect function but its not working and redirect again and again

    • @SakuraDev
      @SakuraDev  11 місяців тому

      @@hamed4451 ok. We should useRouter.
      In the github repo of this video : ua-cam.com/video/g6S-XZxq9Ug/v-deo.html
      I fixed it with useRouter.
      Got to Login component inside signIn page.

  • @feliperetamal7023
    @feliperetamal7023 11 місяців тому +1

    if i use subdomain when i signin the subdomain disappear

    • @SakuraDev
      @SakuraDev  11 місяців тому

      You need to set your domian in .env file.

  • @xanim_iva98
    @xanim_iva98 Рік тому +1

    thats all great, i create the same, but when i build projcet via docker, it gives me an secret error, i added this secret everywhere, tried all things but there is no solution. cant build it.

    • @SakuraDev
      @SakuraDev  Рік тому

      Have you create NEXTAUTH_SECRET in env file?

    • @xanim_iva98
      @xanim_iva98 Рік тому +1

      @@SakuraDev yes! i fixed the problem, its because i need to create it also in my middleware. love your videos❤️

    • @SakuraDev
      @SakuraDev  Рік тому

      @@xanim_iva98 I'm glad it was helpful.🌹

    • @xanim_iva98
      @xanim_iva98 Рік тому +1

      @SakuraDev can u please provide information how to make dynamic nextauth url, so that it can work for production staging and local env. also i have such problem in signout, as it always return to localhost in staging, but i make callback url as that staging url, it isnot working anyway. i redirect to localhost

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

    what pw did u enter to get logged in?

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

      It was just a simple one

  • @alperdemir8657
    @alperdemir8657 Рік тому +1

    How can i implement this using login modal instead of specific login page ?

  • @rizalfindwiky-ml5jy
    @rizalfindwiky-ml5jy Рік тому

    hai , how if backed using sanctum laravel?

    • @SakuraDev
      @SakuraDev  Рік тому

      Hi, it dosent matter what is your backend. It just has to offer a rest api for login that takes username and password inside the body of a POST request and checks if the user exists with the usernane and password. If so, then it returns a user json object that contains the user data as well as jwt tokens. For more info see the detailed video about authentication: ua-cam.com/video/cDWytA0V2kI/v-deo.html

  • @mediacreatif
    @mediacreatif Рік тому +1

    You should update this video with the next JS 13 APP DIRECTORY to avoid confusions :)

    • @SakuraDev
      @SakuraDev  Рік тому

      Yeah, actually I have created a video with next auth and app dir.

    • @SakuraDev
      @SakuraDev  Рік тому

      Thanks for your feedback 😉🌹

    • @mediacreatif
      @mediacreatif Рік тому

      @@SakuraDev With a custom login page ?

  • @mediacreatif
    @mediacreatif Рік тому

    How do you custom page with Google Provider ?

    • @SakuraDev
      @SakuraDev  Рік тому

      You can create a custom login page and put a custom link in that page which call the "signIn("google")". but you cant change the google page in which you select your google account, because it's rendered by the google.

  • @AayanAhmad-mw3fq
    @AayanAhmad-mw3fq 3 місяці тому

    Nice

  • @dotdot9007
    @dotdot9007 Рік тому

    But what happens if user enter wrong password and redirect is true?

    • @SakuraDev
      @SakuraDev  Рік тому +1

      it redirect user again to login page

  • @cheetu2257
    @cheetu2257 Рік тому +1

    hi bro, please in page login how to hide navbar ?!

    • @SakuraDev
      @SakuraDev  Рік тому

      hi, to that, you should use route seperation

    • @cheetu2257
      @cheetu2257 Рік тому +1

      @@SakuraDev there is a an video explained how ?! thnaks in advence keep going

  • @saikrishnamuntha62
    @saikrishnamuntha62 Рік тому +1

    nextjs 13v is in beta version so we an continue with 12thversion bro ?????

    • @SakuraDev
      @SakuraDev  Рік тому

      I think it will be stable very soon. I am using the next.js 13 in our enterprise project and we haven't encounter any bug yet. By the way I think if your project is going to be a long one it is a good idea to go with version 13 becuase if you start with version 12, very soon you have to refacror it with version 13.

    • @saikrishnamuntha62
      @saikrishnamuntha62 Рік тому

      @@SakuraDev at scratch level only started with 12th version

    • @saikrishnamuntha62
      @saikrishnamuntha62 Рік тому

      what is the main difference app directory folder page.tsx and normal page.tsx in 13th version both are same???

    • @Showmatic
      @Showmatic Рік тому +1

      Next.js 13 is production ready. It's only the app directory that's in beta

    • @saikrishnamuntha62
      @saikrishnamuntha62 Рік тому

      @@Showmatic So then we can go with 13th version except App directory

  • @user-mj9hm7vd4u
    @user-mj9hm7vd4u 10 місяців тому

    brother could you make admin portal for website prototype, if you can it very helpful for me as well as everyone of your
    subscriber

    • @SakuraDev
      @SakuraDev  10 місяців тому

      Yes I am planning to create a admin panel tutorial with next.js.

  • @mersharch7767
    @mersharch7767 11 місяців тому

    i have created a custom login page but it still goes to the one created by nextjs

    • @mersharch7767
      @mersharch7767 11 місяців тому

      I’m using the app router instead of pages

    • @SakuraDev
      @SakuraDev  11 місяців тому

      I have also a new version of this video which uses App dir. have you set the pages object in next-auth route.ts file?

    • @mersharch7767
      @mersharch7767 11 місяців тому +1

      @@SakuraDev yes it’s suddenly working now without making any change
      Will you be making a video on authenticating two roles using two different endpoints?

    • @mersharch7767
      @mersharch7767 11 місяців тому

      As in two different external apis

    • @mersharch7767
      @mersharch7767 11 місяців тому

      Thanks for the content though

  • @yousuf4you
    @yousuf4you 11 місяців тому

    super

  • @saikrishnamuntha62
    @saikrishnamuntha62 Рік тому +1

    how to set protectedroutes in nextjs I am confused how to do???

    • @SakuraDev
      @SakuraDev  Рік тому

      Hi, you can use it with middleware. I have described it in this video : ua-cam.com/video/cDWytA0V2kI/v-deo.html

    • @saikrishnamuntha62
      @saikrishnamuntha62 Рік тому

      @@SakuraDev without middleware can we do it manually ,creating hoc and wrapping in app.tsx file component page props wrapping I done like this but the UI is not displaying

    • @ICOReviewtoken
      @ICOReviewtoken Рік тому +1

      @@SakuraDev Can you do a protected route with multil langues (nextintl)? there's a lot to talk about when working with multi langues that also use midlware

    • @SakuraDev
      @SakuraDev  Рік тому

      @@ICOReviewtoken Hi, I will try that and let you know.

    • @chmod-tf7ei
      @chmod-tf7ei Рік тому +1

      next middleware is the least painful method

  • @nurseryrhymes.videos
    @nurseryrhymes.videos Рік тому

    Nextjs 13 App Level Directory this is broken (No more pages directory)

    • @SakuraDev
      @SakuraDev  Рік тому +1

      hi, you are right. but for the next-auth it is still recommanded to put the [...nextauth].ts file inside the pages dir. although as I did in this video, for pages it is better to use App dir.

    • @nurseryrhymes.videos
      @nurseryrhymes.videos Рік тому

      @@SakuraDev Hi can you do a tuts for the latest version of nextjs I mean the App level directory?

    • @SakuraDev
      @SakuraDev  Рік тому +1

      @@nurseryrhymes.videos this video is already in the App dir level. All the pages are in App dir. Just the nextauth file is still in the pages dir. If you want to place the nextauth file inside the app dir too, I have to make research about it and I will let you know soon.

    • @nurseryrhymes.videos
      @nurseryrhymes.videos Рік тому

      @@SakuraDev Thanks bro

  • @sumitwadhwa8823
    @sumitwadhwa8823 11 місяців тому

    csrf token??

    • @SakuraDev
      @SakuraDev  11 місяців тому

      Hi, in NextAuth.js, CSRF protection is present on all authentication routes.

  • @FGA-47
    @FGA-47 Рік тому +1

    Man you are the best, Thanks❤🫡

    • @SakuraDev
      @SakuraDev  Рік тому

      Thanks for your nice comment 🌷