Next.js Authentication - AuthJS / NextAuth for Role-Based Security

Sdílet
Vložit
  • čas přidán 1. 06. 2024
  • Learn how to use NextAuth, soon to be called AuthJS. Learn how to use this tool to add authentication to Next.js 13.5. Dive into the world of role-based authentication and learn how to implement authentication on both server-rendered and client-rendered pages. Discover how to leverage out-of-the-box OAuth providers like Google and GitHub, and explore the process of creating your custom authentication provider using MongoDB. Stay ahead in the world of authentication with this comprehensive guide!
    💻 Code: github.com/ClarityCoders/Next...
    ✏️ Course developed by @ClarityCoders
    ⭐️ Contents ⭐️
    ⌨️ (0:00:00) Introduction
    ⌨️ (0:01:41) Environment Setup / Extension Review
    ⌨️ (0:02:24) Project Creation
    ⌨️ (0:03:28) Install NextAuth (Auth.js) / Create Project Structure
    ⌨️ (0:15:36) Create NextAuth (Auth.js) API - Options / Routes
    ⌨️ (0:25:39) Setup built-in OAuth Provider - (Google / GitHub)
    ⌨️ (0:29:47) Login / Logout Funtionality
    ⌨️ (0:37:45) Protecting Pages - Server Side Page
    ⌨️ (0:40:10) Protecting Pages - Client Side Page
    ⌨️ (0:46:12) Protecting Pages - Middleware
    ⌨️ (0:53:58) Creating Custom Provider (Creating a Database of Users)
    ⌨️ (0:55:16) MongoDB Atlas Signup / Setup
    ⌨️ (0:58:04) Create User Models (Mongoose Schema)
    ⌨️ (1:00:37) User Form to Create Users
    ⌨️ (1:10:30) Create Users API
    ⌨️ (1:18:45) Setup Custom Provider
    🎉 Thanks to our Champion and Sponsor supporters:
    👾 davthecoder
    👾 jedi-or-sith
    👾 南宮千影
    👾 Agustín Kussrow
    👾 Nattira Maneerat
    👾 Heather Wcislo
    👾 Serhiy Kalinets
    👾 Justin Hual
    👾 Otis Morgan
    👾 Oscar Rahnama
    --
    Learn to code for free and get a developer job: www.freecodecamp.org
    Read hundreds of articles on programming: freecodecamp.org/news

Komentáře • 102

  • @ClarityCoders
    @ClarityCoders Před 6 měsíci +19

    Thanks again for the invite to the channel! These videos come out on my channel first along with more videos please check out my channel if you haven't already. Any questions or future projects you would like to see let me know. Keep Coding.

    • @NewDaricha
      @NewDaricha Před 5 měsíci

      😍

    • @nmdpa3
      @nmdpa3 Před měsícem

      Thanks. Any chance you'll do same with Next.js 14 and Auth.js 5?

  • @xanthe69
    @xanthe69 Před měsícem +5

    Quick tip: If you want to create a folder and a file inside it, instead of creating the folder first and then creating the file, you can create a file named "folder name/filename" to do the same. An example would be create a file called Member/page.jsx which would create the page.jsx file in the Member folder

  • @user-wk9rj7ci4c
    @user-wk9rj7ci4c Před 5 měsíci +2

    Amazing video. Very well explained without being superfluous. Tutorial videos like this are such a rarity.

  • @user-ni4xk8pg7r
    @user-ni4xk8pg7r Před 6 měsíci +8

    I just completed the previous project, the Ticketing App, from the same instructor, thanks for this one👍

    • @ClarityCoders
      @ClarityCoders Před 6 měsíci +1

      Great to hear thanks for watching. Let me know if you have any other tutorials you need!

  • @CivilianJointTaskForce
    @CivilianJointTaskForce Před 6 měsíci +4

    You are a great Programmer, thanks for the lecture.

  • @A03L
    @A03L Před 5 měsíci +2

    I saw this course on udemy, I'm glad you provide this for free!!!

  • @MedAliBeel
    @MedAliBeel Před 4 měsíci

    thanks FCC and ClarityCode, it really help me understand the basics of nextauth and to implement it correctly in one of my test projects keep up

  • @okch4m
    @okch4m Před 6 měsíci +1

    Really needed this. Saved for future use 👍

  • @jirayuvijjakajohn295
    @jirayuvijjakajohn295 Před 6 měsíci

    I just completed it. Good one keep it up FCC

  • @BlueHat1
    @BlueHat1 Před 12 dny +1

    What a great tutorial! Very helpful and clear.

  • @ayushtiwari1504
    @ayushtiwari1504 Před 5 měsíci +3

    Doubt 🚨 How do I authorise my api routes to ensure security.
    Btw best video on Next Auth i found on CZcams uptill now.

  • @omc345
    @omc345 Před 6 měsíci +3

    I like the fact that the instructor defines environmental variables at 25:57 🫡

    • @symoleon97
      @symoleon97 Před 5 měsíci

      They know what they did. They probably deleted the keys after making the video

  • @promtpking
    @promtpking Před 6 měsíci

    you guys always putting out stuff like you reading our minds lol! ... request could you please do a tutorial on MQL5 as in metatrader's language for building trading bots

  • @DeveloperLeon
    @DeveloperLeon Před 3 měsíci

    Great video, thanks! I wonder, what if you want more roles than just Admin and User? What if you want the Admin to be able to create new roles and select which permissions they have?

  • @user-yf2qv6gq1q
    @user-yf2qv6gq1q Před 6 měsíci

    Thanks for this tutorial great explanation

  • @yanferaraque1051
    @yanferaraque1051 Před 6 měsíci

    thank you, it was a great tutotrial, would be great if you can show us how to deploy it in Netlify too.

  • @ondersabahat101
    @ondersabahat101 Před 6 měsíci +1

    Thank you for this beautiful lesson. I'm starting to watch other videos on the channel. Why did you write folders like "components" and "models" in parentheses? Can you explain please?

    • @thinpiglin
      @thinpiglin Před 6 měsíci

      Thanks for the great tutorial!
      I'm curious with the parentheses too. Is this just a naming convention, or there are functions in Next I'm missing? 🙋

    • @parspy694
      @parspy694 Před 6 měsíci +6

      you put parenthesis into the folder's name if you want Next.js ignore that folder and not consider as a routing page

  • @warrenb2856
    @warrenb2856 Před 6 měsíci +1

    28:15 Maybe the interface has changed, but make sure you go to API & Services.

  • @user-kq5vl8ii8w
    @user-kq5vl8ii8w Před 4 měsíci

    Really Great Tutorial Video and Project

  • @NewDaricha
    @NewDaricha Před 5 měsíci

    Thanks for such a great tutorial video.

  • @arumals3995
    @arumals3995 Před 3 měsíci

    Thanks for this really useful tutorial.

  • @seancoonce9845
    @seancoonce9845 Před 3 měsíci

    I’m curious (and new to NextJS) why do you say, “you should try to shy away from client side components when you can”?

  • @beinspiredwithquotes
    @beinspiredwithquotes Před 5 měsíci

    thanks for the lecture

  • @BenjiFriedman
    @BenjiFriedman Před 5 měsíci +1

    Great video, thanks. Question-- how could I conditionally show a link in the navigation based on whether the user has the admin role or not?

    • @a.b.x.543
      @a.b.x.543 Před 3 měsíci +1

      maybe try this session?.user?.role === admin

    • @BenjiFriedman
      @BenjiFriedman Před 3 měsíci

      @@a.b.x.543 totally, thanks

  • @herozero777
    @herozero777 Před 3 dny

    A great tutorial indeed. Thanks for this, but sadly after 6 months, it's already outdated as NextAuth v5 uses very different function names.

  • @user-fc9cj1fp6b
    @user-fc9cj1fp6b Před 3 měsíci

    Should add a session:{stratigy:"jwt"} in the options config, or there would be no 'role' property in the server side session.

  • @joshuafoxworth6972
    @joshuafoxworth6972 Před 3 měsíci

    Thanks for the video. It seems like a bad implementation to put admin privileges hard coded in the code. That means that anyone with github access can see who has admin on your site and it means that you need a code change to add or remove privileges? Is there any other way of handling this?

  • @CodingValk
    @CodingValk Před 6 měsíci +9

    Hello, I’m new to the coding community, but wouldn’t this video be out dated because nextAuth.js is changing to auth.js and the step for the new documentation are different

    • @symoleon97
      @symoleon97 Před 5 měsíci

      It is changing for about 1 year now, so I think the video wouldn't be outdated very soon 😅

  • @collinsk8754
    @collinsk8754 Před měsícem

    Great tutorial. Thanks.

  • @ThanakitChokbunsuwan
    @ThanakitChokbunsuwan Před 16 dny

    A big thanks. This saved my life

  • @bizimsiti
    @bizimsiti Před 18 dny

    when i create clientMember page, i recieved this error "sync/await is not yet supported in Client Components". but in video, everything is okey. I didnt understand why

  • @TheDoguBati
    @TheDoguBati Před 5 měsíci

    Im having hard time creating credentials login because i want user id as well and when i try to add user id to session callback it gives undefined .id is undefined error. How do i go about this one solve the issue ?

  • @patty181188
    @patty181188 Před 2 měsíci

    Thank you so much for creating this tutorial, it's so easy to follow.

  • @loo6942
    @loo6942 Před 4 měsíci

    at 47:08, what if I want to use my own login in my middleware before using NextAuth?

  • @ZainRamzan-pt8cm
    @ZainRamzan-pt8cm Před 5 měsíci

    I have a quick question. I've implemented cookies and JWT authentication in my NestJS backend. Now I'm working on the Next.js frontend, and I have a server component that sends a request to the backend (Nest) to get protected data. However, I'm receiving an "unauthorized" message from the backend in the server component.
    Note: I'm using HTTPS-only cookies and ngrok for HTTPS in development.
    How to solve this? means how i can configure next js
    How i can forwards the secure cookies from Server compoents to the nest js backend that are comming from the browser

    • @miguel8yuri
      @miguel8yuri Před 2 měsíci

      Try having the request http instead of https

  • @azamattugelbay
    @azamattugelbay Před 6 měsíci

    Hello why you doesn't use sever actions instead

  • @BaoNguyen-yy3vw
    @BaoNguyen-yy3vw Před 4 měsíci

    Great video, thanks pro🥳🥳

  • @JaiShreeRam5964
    @JaiShreeRam5964 Před 4 měsíci +1

    Using next 14 and I am not able to use the default (sigin and other) pages of next auth???

  • @leonzeng4139
    @leonzeng4139 Před 4 měsíci

    when github oath, what's the application name and Authorization callback URL,it's not clean to do?
    Is there a document for the video. some words are not clean to follow.

  • @user-tz3bc3dl7g
    @user-tz3bc3dl7g Před 4 měsíci

    HI! Thx for the video. Have you tried to enable App Check for Firebase? Official docs say to do it this way:
    const appCheck = initializeAppCheck(app, {
    provider: new ReCaptchaEnterpriseProvider(/* reCAPTCHA Enterprise site key */),
    isTokenAutoRefreshEnabled: true // Set to true to allow auto-refresh.
    });
    But it works only for the client side, but signInWithEmailAndPassword we call on the server, so it does not use the app check token and I got: An error (auth/firebase-app-check-token-is-invalid.)
    Do you know how to handle it? Thx in advance

  • @deivymorales1
    @deivymorales1 Před 6 měsíci

    al prinicipio me daba error con userRole no is defined, portanto me toco definirlo en el alcance global

  • @Samoniel2910
    @Samoniel2910 Před 5 měsíci +1

    const options = {...} export default options
    worked for me, otherwise it was 500 error

  • @MuhammadUmair-gy5le
    @MuhammadUmair-gy5le Před 6 měsíci +1

    Hey I have errors
    I'm stuck in `middleware` section. When I click on `CreateUser` link without having a `login` session. It shows me `Only Admins` text. It should redirect me to login.

    • @bringmemore2621
      @bringmemore2621 Před 5 měsíci +1

      In last next.js version you have to put middleware.ts in the same level with the pages or app folder. If the pages/app folder is in root add it in root level. If the pages/app folder is inside src you have to add it inside src folder. Worked like a charm !!

    • @ukkashrecords7046
      @ukkashrecords7046 Před 3 měsíci

      thnx love u saved my day@@bringmemore2621

  • @opickyayat3132
    @opickyayat3132 Před 4 měsíci

    Can NextAuth use for api auth with another domain access it

  • @reactdeveloper2368
    @reactdeveloper2368 Před 23 dny

    If I'm implementing Google Oauth only what will be value of NEXTAUTH_URL?

  • @Victor-dd7el
    @Victor-dd7el Před 6 měsíci

    but how can I use a similar library in a go app?

  • @joeyreyes9666
    @joeyreyes9666 Před 2 měsíci

    with secret keys i’m not able to commit to github unless i get rid of the secret keys… what’s the workaround? i’m assuming there’s a way to encrypt it or something?

  • @Samner42
    @Samner42 Před 6 měsíci

    bro help me connect this with your tickets app I wrote it all, but I would like every user to see only their created tickets

  • @razlar2080
    @razlar2080 Před 5 měsíci

    Thank You

  • @waitwhat-jh7vr
    @waitwhat-jh7vr Před 4 měsíci

    is this also applicable on `pages`?

  • @leonzeng4139
    @leonzeng4139 Před 4 měsíci

    what's the file name to keep secret, env.local? It's not clean in the video.

  • @amelianceskymusic
    @amelianceskymusic Před 6 měsíci

    Is Ctrl+Shift+L necessary?

  • @JohnJohn-tn3lc
    @JohnJohn-tn3lc Před měsícem

    How to place scopes?

  • @alexanderkomanov4151
    @alexanderkomanov4151 Před 6 měsíci

    GREAT!!!

  • @muhammadyafizhambatubara461

    done

  • @user-mh1km3mv7z
    @user-mh1km3mv7z Před 4 měsíci

    do something to put google auth in here your thing is already setup

  • @roko7378
    @roko7378 Před 6 měsíci

    why am I getting "missing script dev" logged onto my terminal at 14:13??

    • @0xN1nja
      @0xN1nja Před 6 měsíci

      run npm install

    • @roko7378
      @roko7378 Před 6 měsíci

      @@0xN1nja tried it but I’m still having the same problem :(

    • @roko7378
      @roko7378 Před 6 měsíci

      Basically I had a folder containing all of my code inside another folder… so the fix was to cd into that folder containing the code and running npm run dev inside of it 🤦🏻‍♂️🤦🏻‍♂️

  • @ukkashrecords7046
    @ukkashrecords7046 Před 3 měsíci

    middleware is not working for me in next 14

  • @gregoryyang6747
    @gregoryyang6747 Před 2 měsíci

    So this is still about the old NextAuth 4 right, please change the title, which is misleading as AuthJS 5 makes significant changes

  • @sguitas
    @sguitas Před 5 měsíci

    This works with next.js 14?

    • @drbv27
      @drbv27 Před 8 dny

      yes works perfect

  • @abdullahmehar2931
    @abdullahmehar2931 Před 5 měsíci

    some how this is not working for me now

  • @roko7378
    @roko7378 Před 6 měsíci

    why is my .env.local file not working??

    • @Lucious_LT
      @Lucious_LT Před 17 dny

      Mine too, having client_id error

  • @itexpert4193
    @itexpert4193 Před 6 měsíci

    great

  • @user-cn6lz8nn1r
    @user-cn6lz8nn1r Před 3 měsíci

    Use typescript

  • @devrano48
    @devrano48 Před 2 měsíci

    middleware makes the routing slower...

  • @user-mh1km3mv7z
    @user-mh1km3mv7z Před 4 měsíci

    yeah broken tutorial
    sorry :/
    please do a write up with this it would have been helpful

  • @kelvinebima9571
    @kelvinebima9571 Před 6 měsíci

    Lovely video but it’s not differentiating roles.

  • @booi_mangang
    @booi_mangang Před 25 dny

    No ts is a deal breaker

  • @AntNZ
    @AntNZ Před 4 měsíci

    Is it just me or is Next-Auth v4 and beta all now completely unusable?

  • @Tanner-cz4bd
    @Tanner-cz4bd Před 6 měsíci

    Finally

  • @nadyashaymardanova6000
    @nadyashaymardanova6000 Před 6 měsíci

    🙅

  • @frenchmike
    @frenchmike Před 2 měsíci

    26:00

  • @worseize
    @worseize Před 6 měsíci +1

    They make so much css staff to make it seems messy and hard , but if you need working example you need 5 mins and few lines of code but if you show that then you can’t be payed well😂

  • @AntNZ
    @AntNZ Před 4 měsíci

    What the hell happened - it was such a good project and is now dead.

  • @user-mh1km3mv7z
    @user-mh1km3mv7z Před 4 měsíci

    can you start with configuring the console.cloud.google?
    at 28:26 your google cloud is all set up with other things but mine asks for "Configure Consent Screen" but it sends me to See Setting up OAuth 2.0 and i obviously have no idea what to do since im at this tutorial

    • @user-pf4ij5he7t
      @user-pf4ij5he7t Před 27 dny

      did you solve the problem man? i'm at the same issue borther

  • @techiesakar
    @techiesakar Před 4 měsíci

    I want NextAuth to call signOut event when refresh token expire.
    Is there any way to do it ?