NestJs REST API with MongoDB #4 - Authentication, Login/Sign Up, assign JWT and more

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ต.ค. 2024
  • Final Code:
    github.com/ghu...
    Complete In-Depth NestJs UDEMY Courses:
    www.udemy.com/...
    ⚙️ Gadgets I use for Recording:
    💻 Laptop - Macbook PRO M1 - 16/512
    amzn.to/4eUqrSe
    🎤 MIC - Samson Technologies Q2U USB/XLR Dynamic Microphone
    amzn.to/3W9Z4wl
    🖱️Mouse - Logitech G402 Gaming Mouse Hyperion Fury USB, Black
    amzn.to/3RY4xUy
    ⌨️ Keyboard - Logitech K380 Multi-Device Bluetooth Keyboard
    amzn.to/3RY4Fn0
    🖥️ Monitor - UltraSharp 24-Inch Screen Led-Lit Monitor (U2419H)
    amzn.to/4bxidN6
    In this video, we will learn how we can add authentication to our NestJS API. In my previous video, I created a complete CRUD Library API. And now we will create our sign-up and login routes.
    In this video, we will continue with that project and authentication and integrate passport and assign JWT tokens to the users.
    If you like this video make sure to subscribe and share, and stay tuned for more coding videos.

ความคิดเห็น • 60

  • @aliyanashraf6999
    @aliyanashraf6999 ปีที่แล้ว +5

    For those getting error:
    nest can't resolve dependencies of the JWT_MODULE_OPTIONS (?). Please make sure that the argument ConfigService at index [0] is available in the JwtModule context.
    solution:
    add
    imports: [ConfigModule], inside JWTModule.RegisterAsync
    Thank you.

    • @volatilnost
      @volatilnost 11 หลายเดือนก่อน

      big thanks to you! Very helped me

    • @nanonkay5669
      @nanonkay5669 6 หลายเดือนก่อน +1

      shout out to you, this worked for me too, big up

  • @shashankkamath2171
    @shashankkamath2171 ปีที่แล้ว +1

    Thanks for this video , also I want to know what all extra videos are there in your udemy course on nest js?

    • @codingwithabbas
      @codingwithabbas  ปีที่แล้ว

      There are more details and a complete API.

  • @wahid-eng
    @wahid-eng 17 วันที่ผ่านมา

    The 20:45 login route should have the POST method.

  • @loveshdutta9207
    @loveshdutta9207 9 หลายเดือนก่อน

    please make more video comprising of relational databases with authentication too........Thanks.....Nice Playlist

  • @phantazzor
    @phantazzor 3 หลายเดือนก่อน

    do you have an updated version of your course ?

  • @awaraamin6850
    @awaraamin6850 ปีที่แล้ว

    The fourth one is great too!

  • @khathi6484
    @khathi6484 2 หลายเดือนก่อน

    My errors: nest can't resolve dependencies of the AuthService(?, JwtService). Please make sure that the argument UserModule at index [0] is available in the AppModule context(But I didn't have or Create UserModule). So I decided to create a new User Folder for CRUD and in the UserModule( Providers, I added JwtService) so it work to me ?

    • @vivekabhale1335
      @vivekabhale1335 2 หลายเดือนก่อน

      The issue you're encountering is due to the incorrect usage of the @InjectModel decorator in your AuthService. Specifically, you are passing Model.name instead of the name of the schema you defined (User). The correct usage should be the name of the schema (for eg. User), which you registered in your AuthModule.

  • @ນ.ຄູນໄພລິນເເກ້ວວົງກົດ
    @ນ.ຄູນໄພລິນເເກ້ວວົງກົດ 3 หลายเดือนก่อน

    Thankyou! works perfectly

  • @arthuriski
    @arthuriski ปีที่แล้ว

    Thanks Sir, you save my life,
    But any of my class-validator is not working, I use class-validator in dto as you did
    Is there is something that I miss?

    • @Aziz.Ahmed95
      @Aziz.Ahmed95 10 หลายเดือนก่อน

      have you imported the class-validator from @nestjs/common package try importing and then see like this below
      import { IsOptional, IsString, IsNumber } from "class-validator";

  • @vaib-dev
    @vaib-dev ปีที่แล้ว +1

    I'm getting error:
    Error: Nest can't resolve dependencies of the JWT_MODULE_OPTIONS (?). Please make sure that the argument ConfigService at index [0] is available in the JwtModule context.

    • @codingwithabbas
      @codingwithabbas  ปีที่แล้ว

      Have you setup JwtModule.registerAsync correctly? Recheck your code once.

    • @darkreverie3748
      @darkreverie3748 ปีที่แล้ว +1

      @@codingwithabbas I have the same problem, I even copied the auth module code from your repository, and the error still persists. What could be the problem ?

    • @HariPrakashrrm6
      @HariPrakashrrm6 ปีที่แล้ว +1

      @@darkreverie3748 same problem

    • @Amelin1207
      @Amelin1207 ปีที่แล้ว

      @@codingwithabbas
      write in your arr.module.ts:
      imports: [
      ConfigModule.forRoot({
      envFilePath: '.env',
      isGlobal: true,
      }), ....

    • @Guptafamily_official
      @Guptafamily_official ปีที่แล้ว

      i got error
      Error: Nest can't resolve dependencies of the JWT_MODULE_OPTIONS (?). Please make sure that the argument ConfigService at index [0] is available in the JwtModule context.
      Potential solutions:
      - Is JwtModule a valid NestJS module?
      - If ConfigService is a provider, is it part of the current JwtModule?
      - If ConfigService is exported from a separate @Module, is that module imported within JwtModule?
      @Module({
      imports: [ /* the Module containing ConfigService */ ]
      })
      at Injector.lookupComponentInParentModules (E:\bytescrum
      eact\backend
      ode_modules\@nestjs\core\injector\injector.js:248:19)
      at Injector.resolveComponentInstance (E:\bytescrum
      eact\backend
      ode_modules\@nestjs\core\injector\injector.js:202:33)
      at resolveParam (E:\bytescrum
      eact\backend
      ode_modules\@nestjs\core\injector\injector.js:123:38)
      at async Promise.all (index 0)
      at Injector.resolveConstructorParams (E:\bytescrum
      eact\backend
      ode_modules\@nestjs\core\injector\injector.js:138:27)
      at Injector.loadInstance (E:\bytescrum
      eact\backend
      ode_modules\@nestjs\core\injector\injector.js:64:13)
      at Injector.loadProvider (E:\bytescrum
      eact\backend
      ode_modules\@nestjs\core\injector\injector.js:91:9)
      at E:\bytescrum
      eact\backend
      ode_modules\@nestjs\core\injector\instance-loader.js:56:13
      at async Promise.all (index 4)
      at InstanceLoader.createInstancesOfProviders (E:\bytescrum
      eact\backend
      ode_modules\@nestjs\core\injector\instance-loader.js:55:9)
      PS E:\bytescrum
      eact\backend> $ npm install --save @nestjs/jwt
      $ : The term '$' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path
      was included, verify that the path is correct and try again.
      At line:1 char:1
      + $ npm install --save @nestjs/jwt
      + ~
      + CategoryInfo : ObjectNotFound: ($:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException

      PS E:\bytescrum
      eact\backend> npm install --save @nestjs/jwt
      npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
      [##################] | reify:fsevents: sill reify mark deleted [ 'E:\\bytescrum\
      eact\\backend\
      ode_modules\\fsevents' ]

  • @Guptafamily_official
    @Guptafamily_official ปีที่แล้ว

    Error: Nest can't resolve dependencies of the JWT_MODULE_OPTIONS (?). Please make sure that the argument ConfigService at index [0] is available in the JwtModule context.
    Potential solutions:
    - Is JwtModule a valid NestJS module?
    - If ConfigService is a provider, is it part of the current JwtModule?
    - If ConfigService is exported from a separate @Module, is that module imported within JwtModule?
    @Module({
    imports: [ /* the Module containing ConfigService */ ]
    })
    at Injector.lookupComponentInParentModules (E:\bytescrum
    eact\backend
    ode_modules\@nestjs\core\injector\injector.js:248:19)
    at Injector.resolveComponentInstance (E:\bytescrum
    eact\backend
    ode_modules\@nestjs\core\injector\injector.js:202:33)
    at resolveParam (E:\bytescrum
    eact\backend
    ode_modules\@nestjs\core\injector\injector.js:123:38)
    at async Promise.all (index 0)
    at Injector.resolveConstructorParams (E:\bytescrum
    eact\backend
    ode_modules\@nestjs\core\injector\injector.js:138:27)
    at Injector.loadInstance (E:\bytescrum
    eact\backend
    ode_modules\@nestjs\core\injector\injector.js:64:13)
    at Injector.loadProvider (E:\bytescrum
    eact\backend
    ode_modules\@nestjs\core\injector\injector.js:91:9)
    at E:\bytescrum
    eact\backend
    ode_modules\@nestjs\core\injector\instance-loader.js:56:13
    at async Promise.all (index 4)
    at InstanceLoader.createInstancesOfProviders (E:\bytescrum
    eact\backend
    ode_modules\@nestjs\core\injector\instance-loader.js:55:9)
    PS E:\bytescrum
    eact\backend> $ npm install --save @nestjs/jwt
    $ : The term '$' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path
    was included, verify that the path is correct and try again.
    At line:1 char:1
    + $ npm install --save @nestjs/jwt
    + ~
    + CategoryInfo : ObjectNotFound: ($:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    PS E:\bytescrum
    eact\backend> npm install --save @nestjs/jwt
    npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
    [##################] | reify:fsevents: sill reify mark deleted [ 'E:\\bytescrum\
    eact\\backend\
    ode_modules\\fsevents' ]

    • @aliyanashraf6999
      @aliyanashraf6999 ปีที่แล้ว

      simply add imports: [ConfigModule], inside JWTModule.RegisterAsync

    • @SoftMindSolutionsDeveloper
      @SoftMindSolutionsDeveloper 10 หลายเดือนก่อน

      app.module ma app ky Model { controllers, providers } ma default AuthService or AuthController import hu raha ha us ko remove kary

    • @Guptafamily_official
      @Guptafamily_official 10 หลายเดือนก่อน

      @@SoftMindSolutionsDeveloper bhut jldi btaa diya

    • @phantazzor
      @phantazzor 3 หลายเดือนก่อน

      @@SoftMindSolutionsDeveloper in english it'd be nice

  • @ExtraTurtle
    @ExtraTurtle หลายเดือนก่อน

    when I try to use bcrypt.compare for password and user.password, it tells me password doesn't exist on user even though I made the scema exacrtly like yours

  • @ahmetkeles5034
    @ahmetkeles5034 ปีที่แล้ว +1

    It doesnt give me any errors if i write mail or password incorrect just instantly gives me token even if i send the inputs empty, and i cant see the users that created in mongo can you help me please?

    • @codingwithabbas
      @codingwithabbas  ปีที่แล้ว

      Check the code where we are saving the user in the database.

    • @ahmetkeles5034
      @ahmetkeles5034 ปีที่แล้ว +1

      @@codingwithabbas this is why it accepts incorrect mail and password types? it gives me token always no matter the input of the mail and password

    • @malcomwaltermaccoinnich3418
      @malcomwaltermaccoinnich3418 ปีที่แล้ว

      @@ahmetkeles5034 I have the same issue, have you found the problem?

    • @kishankalathia8218
      @kishankalathia8218 ปีที่แล้ว

      ​@@malcomwaltermaccoinnich3418
      import { ValidationPipe } from '@nestjs/common';
      async function bootstrap() {
      const app = await NestFactory.create(AppModule);
      app.useGlobalPipes(
      new ValidationPipe({
      transform: true, // Ensure this is set to true
      }),
      );
      await app.listen(3002);
      }
      bootstrap();

  • @sanjayms00
    @sanjayms00 10 หลายเดือนก่อน

    my dto is not working, its doesn't validate the email and password

  • @ashishkumari-yg4gn
    @ashishkumari-yg4gn ปีที่แล้ว +1

    Thank you sir

  • @aditimohan7892
    @aditimohan7892 ปีที่แล้ว

    I don't have .env file in my project . Is there any alternate solution

    • @codingwithabbas
      @codingwithabbas  ปีที่แล้ว

      You can simply create one. Create in root of the project.

  • @jimmyadams4375
    @jimmyadams4375 ปีที่แล้ว

    how to make a swagger api for login and singup and in it use nestjs and mongodb please help me

  • @muhammadshahzad2446
    @muhammadshahzad2446 3 หลายเดือนก่อน

    thank you

  • @girishpillai9390
    @girishpillai9390 ปีที่แล้ว

    why have you created a get api for login it should be post

    • @codingwithabbas
      @codingwithabbas  ปีที่แล้ว

      You can make it post. We are posting any data so GET will work fine.

  • @vvmanikantakantipudi9919
    @vvmanikantakantipudi9919 ปีที่แล้ว

    secretOrPrivateKey must have a value i am getting this error please help to resolve this

    • @codingwithabbas
      @codingwithabbas  ปีที่แล้ว

      It seems like you are passing undefined to secretOrPrivateKey, make sure you are passing the correct value.

    • @vvmanikantakantipudi9919
      @vvmanikantakantipudi9919 ปีที่แล้ว

      @@codingwithabbas i guess i am sending the correct one from .env file then also i am getting error when i hit the api user details has been creating and storing in db but the issue is i am not getting that jwt token

  • @НикитаМалышенко-щ3щ
    @НикитаМалышенко-щ3щ 21 วันที่ผ่านมา

    Is this information relevant and the best? The dock says otherwise

    • @codingwithabbas
      @codingwithabbas  18 วันที่ผ่านมา

      Yes, this information is accurate and relevant.

  • @ashishkumari-yg4gn
    @ashishkumari-yg4gn ปีที่แล้ว

    sir,
    latest version of mongoose is giving errors

    • @codingwithabbas
      @codingwithabbas  ปีที่แล้ว

      Can you share that error?

    • @ashishkumari-yg4gn
      @ashishkumari-yg4gn ปีที่แล้ว

      @@codingwithabbas npm ERR! Could not resolve dependency:
      npm ERR! peer mongoose@"^6.0.2" from anestjs/mongoose@9.2.1
      npm ERR! node_modules/@nestjs/mongoose
      npm ERR!
      anestjs/mongoose"^9.2.1" from the root project
      npm ERR!
      npm ERR! Conflicting peer dependency: mongoose 6.10.0
      npm ERR! node_modules/mongoose
      npm ERR!
      npm ERR!
      npm ERR!
      npm ERR!
      npm ERR! Fix the upstream dependency conflict, or retry
      npm ERR! this command with --force, or --legacy-peer-deps
      npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
      npm ERR!
      npm ERR! See C:\Users\PRINCE
      peer mongoose@"^6.0.2" from anestjs/mongoose@9.2.1
      node_modules/@nestjs/mongoose
      anestjs/mongoosen"^9.2.1" from the root project
      RAI\AppData\Local
      pm-cache\eresolve-report.txt for a full report.
      npm ERR! A complete log of this run can be found in:
      npm ERR!
      C:\Users\PRINCE RAI\AppData\Local
      pm-cache\_logs\2023-03-01T11_17_26_318Z-debug-0.log

    • @spizer98
      @spizer98 ปีที่แล้ว

      @@codingwithabbas
      npm ERR! code ERESOLVE
      npm ERR! ERESOLVE could not resolve
      npm ERR!
      npm ERR! While resolving: @nestjs/mongoose@9.2.1
      npm ERR! Found: mongoose@7.0.1
      npm ERR! node_modules/mongoose
      npm ERR! mongoose@"^7.0.1" from the root project
      npm ERR!
      npm ERR! Could not resolve dependency:
      npm ERR! peer mongoose@"^6.0.2" from @nestjs/mongoose@9.2.1
      npm ERR! node_modules/@nestjs/mongoose
      npm ERR! @nestjs/mongoose@"^9.2.1" from the root project
      npm ERR!
      npm ERR! Conflicting peer dependency: mongoose@6.10.3
      npm ERR! node_modules/mongoose
      npm ERR! peer mongoose@"^6.0.2" from @nestjs/mongoose@9.2.1
      npm ERR! node_modules/@nestjs/mongoose
      npm ERR! @nestjs/mongoose@"^9.2.1" from the root project

    • @kalone5569
      @kalone5569 ปีที่แล้ว +1

      @@spizer98 maybe it's a local problem, api is working correctly here

    • @jayaprakashjp077
      @jayaprakashjp077 ปีที่แล้ว +1

      @@spizer98 right click the file and open in integrated terminal

  • @KU10S
    @KU10S ปีที่แล้ว

    throwing error

  • @ParashUkheda
    @ParashUkheda ปีที่แล้ว

    sir please can u tell me where are u import Jwt Service in auth.service.ts

    • @codingwithabbas
      @codingwithabbas  ปีที่แล้ว

      from nestjs/jwt, like this:
      import { JwtService } from '@nestjs/jwt';

  • @dungangvan7147
    @dungangvan7147 ปีที่แล้ว

    thanks you so much