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

Поділитися
Вставка
  • Опубліковано 26 січ 2023
  • Final Code:
    github.com/ghulamabbas2/libra...
    Complete In-Depth NestJs UDEMY Courses:
    www.udemy.com/course/nestjs-b...
    ⚙️ 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.

КОМЕНТАРІ • 52

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

    The fourth one is great too!

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

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

  • @user-js2dn2bt1m
    @user-js2dn2bt1m 19 днів тому

    Thankyou! works perfectly

  • @ashishkumari-yg4gn
    @ashishkumari-yg4gn Рік тому +1

    Thank you sir

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

    thanks you so much

  • @muhammadshahzad2446
    @muhammadshahzad2446 21 день тому

    thank you

  • @aliyanashraf6999
    @aliyanashraf6999 9 місяців тому +4

    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 7 місяців тому

      big thanks to you! Very helped me

    • @nanonkay5669
      @nanonkay5669 2 місяці тому +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?

  • @phantazzor
    @phantazzor 16 днів тому

    do you have an updated version of your course ?

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

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

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

    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 7 місяців тому

      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' ]

  • @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.

  • @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 9 місяців тому

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

    • @user-lz5rq1vq3x
      @user-lz5rq1vq3x 7 місяців тому

      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 7 місяців тому

      @@user-lz5rq1vq3x bhut jldi btaa diya

    • @phantazzor
      @phantazzor 15 днів тому

      @@user-lz5rq1vq3x in english it'd be nice

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

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

  • @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 9 місяців тому

      ​@@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();

  • @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

  • @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

  • @user-ux9xe2ij4f
    @user-ux9xe2ij4f Рік тому

    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';