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.
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.
big thanks to you! Very helped me
shout out to you, this worked for me too, big up
Thanks for this video , also I want to know what all extra videos are there in your udemy course on nest js?
There are more details and a complete API.
The 20:45 login route should have the POST method.
please make more video comprising of relational databases with authentication too........Thanks.....Nice Playlist
do you have an updated version of your course ?
The fourth one is great too!
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 ?
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.
Thankyou! works perfectly
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?
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";
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.
Have you setup JwtModule.registerAsync correctly? Recheck your code once.
@@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 ?
@@darkreverie3748 same problem
@@codingwithabbas
write in your arr.module.ts:
imports: [
ConfigModule.forRoot({
envFilePath: '.env',
isGlobal: true,
}), ....
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' ]
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' ]
simply add imports: [ConfigModule], inside JWTModule.RegisterAsync
app.module ma app ky Model { controllers, providers } ma default AuthService or AuthController import hu raha ha us ko remove kary
@@SoftMindSolutionsDeveloper bhut jldi btaa diya
@@SoftMindSolutionsDeveloper in english it'd be nice
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
Restart your server once.
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?
Check the code where we are saving the user in the database.
@@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
@@ahmetkeles5034 I have the same issue, have you found the problem?
@@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();
my dto is not working, its doesn't validate the email and password
Thank you sir
I don't have .env file in my project . Is there any alternate solution
You can simply create one. Create in root of the project.
how to make a swagger api for login and singup and in it use nestjs and mongodb please help me
I will try to add a video on it as well.
@@codingwithabbas Thank you
thank you
why have you created a get api for login it should be post
You can make it post. We are posting any data so GET will work fine.
secretOrPrivateKey must have a value i am getting this error please help to resolve this
It seems like you are passing undefined to secretOrPrivateKey, make sure you are passing the correct value.
@@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
Is this information relevant and the best? The dock says otherwise
Yes, this information is accurate and relevant.
sir,
latest version of mongoose is giving errors
Can you share that error?
@@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
@@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
@@spizer98 maybe it's a local problem, api is working correctly here
@@spizer98 right click the file and open in integrated terminal
throwing error
Share the error message.
sir please can u tell me where are u import Jwt Service in auth.service.ts
from nestjs/jwt, like this:
import { JwtService } from '@nestjs/jwt';
thanks you so much