Role Based Authorization with Next Auth + Google Provider + Prisma in NEXT.JS

Поділитися
Вставка
  • Опубліковано 14 жов 2024
  • Join this channel to get access to perks:
    / @sakuradev
    Learn how to implement powerful role-based authorization in Next.js using Next Auth, Google Provider, and Prisma. Secure your app, control user access, and level up your Next.js skills with this tutorial! 🔒👩‍💻
    By the end of this tutorial, you'll have the skills to build an authentication and authorization system that fits your project's needs, from basic user roles to more advanced scenarios.
    👉 Don't forget to like, subscribe, and hit the notification bell to stay updated with our latest tutorials on web development, Next.js, and more!
    ✅ GitHub Repo (Don't forget the fallow and give a start in GitHub):
    github.com/vah...
    #NextJS #Authorization #WebDev

КОМЕНТАРІ • 86

  • @tsykin
    @tsykin 8 місяців тому +1

    That worked like a charm for me! Thank you for this tutorial
    In the future, please, make more content on nextjs + prisma + postgresql

  • @phil5293
    @phil5293 11 місяців тому +2

    Dude. This is amazing. I've been struggling with google provider authentication for a while. This fixed it. Just a note - to initialize Prisma using sqlite database instead of the default Postgresql use the command "npx prisma init --datasource-provider sqlite"

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

      Really happy that it was helpful for you. Also, thanks for your tip, so I don't need to manually change it to sql lite. I will use it in my upcomming videos.

  • @alexloquendero1000
    @alexloquendero1000 Рік тому +3

    Just what I needed man! Thank you!

  • @GuilhermeSiebert
    @GuilhermeSiebert 7 місяців тому +1

    AMAZING TUTORIAL! Thank you so much!

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

    Just wanted to write that your videos are very clear and good, thanks!

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

    I'm sorry if the question makes no sense but i'm quite new with nextjs, from my understanding in next all the children of a client component must be client component, so then by wrapping the entire app with provider, doesn't it defeat the purpose of nextjs server side rendering?
    thanks for the video!

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

      Hi, good question. The answer is no. You can watch this short video in which I have explained it: ua-cam.com/video/xHOC1DJgQA0/v-deo.html

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

    You made my day again !

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

      Thanks and you also made my day with your nice words 💖

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

    Thank you for this video, but I want to add custom roles (EMPLOYEE, EMPLOYER, ...) when sign in with Google Provider before create user in database, how can I do this?

  • @jamesyu6385
    @jamesyu6385 7 місяців тому

    Thank for this awesome tutorial!. question! when i build this project to deploy in vercel, Error: Unsafe assignment of an `any` value.. guessing from googleProvider using profile. the type shown is "any". how can I give the type for profile comming from Google?

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

    hey, thanks for sharing such an amazing, precise codebase with unbeleiveable clarity of things ( explanations ).
    Q. btw, which syntax theme you are using in vscode.(curious) arrow function and equality comparison with triple equal looked cool!

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

    Hey Sakura, why do you create the Session Table in your DB? For me is everything workin without Session and VerificationToken ?

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

      Here we are using the session strategy as "jwt", the session table if for the server side sessions (session.strategy="database") and the verificationToken table is for the email provider as long as you don't use those removing them works fine.

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

    Whats the better method between allowing access to the admin dashboard this way or simply hiding the admin dashboard link from the navbar by checking the user role in the frontend

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

      Meaning something like {user.role=='admin' ? Admin Dashboard: }
      Which is the better method?

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

      @@ichigokuro3986 definitely this way in the video.

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

      Thanks mate! keep up the good work!@@SakuraDev

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

    Hey, could you help me with a question about NextAuth providers and Next.js layouts? I'm using Route Groups in my app, so I don't have a single root layout. Instead, I have a (home) layout and an (auth) layout because I don't want to share my header and footer on both pages, just on the home page. So, where should I import the NextAuth provider? Should I import it in both layouts? Thank you!

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

    Thank you!
    Could you please make a tutorial on how to refresh token in nextauth?

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

      Hi, In this video I have shown how we can do that: ua-cam.com/video/khNwrFJ-Xqs/v-deo.html

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

    Here's a quick question how to you implement the 'bun:sqlite' instead of going the other route... Thank you in advance.

  • @Redona_
    @Redona_ 20 днів тому

    Can I use ExpressJS for auth logic while still having credential&google provider in NEXT.JS?

    • @SakuraDev
      @SakuraDev  20 днів тому +1

      In this case you should use passport.js for it. You need to handle the Google authentication on your expressjs and the just tell nextjs that the user is authenticated. I will upload a video with a stand alone backend. It uses nestjs in the backend, however.

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

    Hello, is it possible to setup google login with another Backend server using next and next auth and presist the user in the database?

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

      Hi, yes it is possible. I should create a video about it

  • @pauliusvasiliauskas2676
    @pauliusvasiliauskas2676 7 місяців тому

    i would like to have a video for configurint jwt session token attaching access-token for back end api, would be nice to have azure ad provider as a sample

  • @Jibs-iq1em
    @Jibs-iq1em Рік тому

    Wow, Nice video its really detailed and breaks down the complex part, may I ask which Icons you use in your vscode app?

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

      Thanks 🙏, it's material icons

    • @Jibs-iq1em
      @Jibs-iq1em Рік тому

      @@SakuraDev Thanks

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

      @@SakuraDev hey sakura, may you didnt see my question: but why session and verficitation token tables? It’s works without also? I just want to understand :)

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

      @@owl3379 hi, yes it works without them if you set the strategy to jwt. But if you set strategy to database you have to keep these tables

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

      @@SakuraDev whats the key difference? Its better to store in database?

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

    My problem is we have 3 roles. Advertiser, Publisher and Admin. I would like user to sign up as Advertiser or as Publisher. Now this data needs to comes from frontend. How to do that.

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

      You can handle it in the middleware. get your user data from the backend with role then check them in middleware

  • @codex1758
    @codex1758 5 місяців тому

    Can you make a tutorial on how to use the credentials provider in conjunction with google provider please?

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

    thx for tutorial..
    i have a question.. how to handle session timeout ?

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

      Thanks 👍, it is handled by next-auth

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

    I am trying to follow along but I cant get the role to be changed role is just null even with the callback in place. Seems like its not working. All the other fields so name etc works fine its just role that is not working.

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

      Can you share your code?

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

    I want my admin to be able to create other users(employees). Does this video cover it?

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

      In this video I have shown how we can protect pages based on the role of the user. A normal user can only see his profile, but an admin user can see the admin panel .

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

    How to make: credentials + google provider + nestjs (simultaneously)? Backend shema contains password, but google doesn't provide password. So password in schema should be optional? Thank you for tutorial

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

      I will create a short tutorial on that

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

      @@SakuraDev i'm looking for this too

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

    Amazing job!

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

      Thanks 🙏🙏

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

    thanks for sharing

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

      Thanks for your constant support ❤️🙏

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

    with this method is it possible to have more than 2 roles ?

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

      It's a little complicated but yes we can do that with this method

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

      @@SakuraDev thanks for the answer ❤️, i might just tried this out first

  • @user-bn3ej9go4p
    @user-bn3ej9go4p Місяць тому

    Can you please explain how I replace I'd with database id I don't, won't use sub I'd instead of this I want to use my databasge id

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

      Hi, we should use the next-auth callbacks and replace the database id with google id in your session. I think next-auth is really complicated for many developer so now i am actually moving from next-auth to session based auth in next.js which is really simple and yet powerful. I have recorded a full tutorial for that and it will uploaded next week.

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

    Nice video, but how to redirect user if role not admin?

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

      You can handle it in the middleware. Redirect or rewrite

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

    hi, I just want to ask , what if I had two signIn with google buttons, one to sign in as an admin and one to sign in as customer, how I tell what the role is ? I'm checking the provider in the signIn call back if it google then I save the user to the db in User table and in Account Table and if the user exist I just return it , but the user first sign in how I can differentiate if this user want to sign in as admin or as customer ?

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

      he is not answering this in this video

  • @thalysmatiascarrara2144
    @thalysmatiascarrara2144 7 місяців тому

    i dont know why im receiving this error when trying to use PrismaAdapter... Type 'import("/home/thalys/Documents/projects/linkyplayer-dashboard/node_modules/@auth/core/adapters").Adapter' is not assignable to type 'import("/home/thalys/Documents/projects/linkyplayer-dashboard/node_modules/next-auth/adapters").Adapter'.
    Types of property 'createUser' are incompatible.
    Type '((user: AdapterUser) => Awaitable) | undefined' is not assignable to type '((user: Omit) => Awaitable) | undefined'.
    Type '(user: AdapterUser) => Awaitable' is not assignable to type '(user: Omit) => Awaitable'.
    Types of parameters 'user' and 'user' are incompatible.
    Property 'id' is missing in type 'Omit' but required in type 'AdapterUser'.ts(2322)
    adapters.d.ts(174, 5): 'id' is declared here.
    types.d.ts(106, 5): The expected type comes from property 'adapter' which is declared here on type 'AuthOptions'
    (property) AuthOptions.adapter?: Adapter | undefined

    • @proniket
      @proniket 3 місяці тому

      Yeah... I'm also receiving the same error... don't know why...

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

    Thanks for stick and using next-auth
    As every tutorial using clerk which is easy but not good for pricing

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

    Could you please make one with nextauth 4 + firebase 10 adapter

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

    Make this with google auth next auth client only in next js and api save users data using nest js Rest Api role based

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

      Make tutorial on it

    • @ВячеславЛепский-п1э
      @ВячеславЛепский-п1э Рік тому

      That would be very cool, because I can’t find any information about this. I would like to see the implementation of oauth + next auth + nest with JWT and Refresh tokens

  • @satvik0099
    @satvik0099 5 місяців тому

    I was searching for the solution where we can customise some email domain snd can tell whether its an user or admin.
    But this is just changing the roles manually.
    Not that helpfull

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

    what if the user role is in another table? not in the user table? how would you get the role of user?

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

      we are using prisma so we just need to use "Include" api in the prisma query to get the user role from a related table. the rest is the same

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

      @@SakuraDev I was thinking of using either select or include with prisma for credentials log in, however, how about for Google Provider log in? Do u have an idea if you can also add a function to it? thanks, I really like your channel.

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

      @@sirhc6chris1984 well, first thanks for your nice words. when we want to implement role based authorization with google provider, as I have shown in the video, we can use " profile" function to get the role of the user.

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

    The tutorial is not reflecting real world scenarios. basically you have only one role (user) and change the role in the database to add more roles. In a real world as many users pointed out here in the comments you already have 2,3,4 roles and they need to register. That is the biggest riddle nextauth is very bad to ealborate. How to handle different signIn locations (for different roles) for example admin has his own Register Button viah Oauth.

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

    Theme and font of vs code

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

      Hi, Bearded arch theme and jetbrain font

  • @tuananhdo1870
    @tuananhdo1870 6 місяців тому

    6:37

  • @tuananhdo1870
    @tuananhdo1870 6 місяців тому

    18:30

  • @tuananhdo1870
    @tuananhdo1870 6 місяців тому

    11:20

  • @abujayed8999
    @abujayed8999 5 місяців тому

    if you used mongoDB in your every tutorial you would get more sub.