- npm install @prisma/client
- npm i prisma -D
- npm i -D class-validator class-transformer
- npm i argon2 --hash password--
- npm i @nestjs/passport passport passport-local ---login----
- npm i -D @types/passport-local
- nest g gu auth/guards/local-auth ---guards----
- npm i @nestjs/jwt @nestjs/config
- npm i passport-jwt
- npm i -D @types/passport-jwt
- npm i passport-google-oauth20
- npm i -D @types/passport-google-oauth20
Nest framework TypeScript starter repository.
$ npm install# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov- A POST request is made to the
/auth/signupendpoint with the user’s email and password.
- The registerUser method in the AuthController receives the request.
- It extracts the data from the request body, validates it using the ValidationPipe, and forwards it to AuthService.registerUser().
- The AuthService checks if a user with the same email exists by calling UserService.findByEmail().
- If no user exists, it creates the user using UserService.create().
- The new user is now registered.
- The user receives a success response confirming the registration.
- The client sends a POST request to
/auth/loginwith an email and password in the request body.
- The loginUser() method in AuthController is protected by the LocalAuthGuard.
- When the request hits
/auth/login, LocalAuthGuard.canActivate() is executed first to validate the user before the controller handles the request.
- The guard extracts the email and password from the request.
- It calls AuthService.validateLocalUser() to validate the user’s credentials.
- In validateLocalUser(), the service:
- Finds the user by email using UserService.findByEmail().
- If found, it verifies the password using argon2.verify().
- If credentials are valid, it returns the user’s ID and email.
- If the user is valid, LocalAuthGuard attaches the user object to the request (via request.user = user).
- The request now contains the authenticated user’s data, and canActivate() returns true, allowing the request to proceed to the controller.
- Now that the guard has validated the user, the AuthController.loginUser() method is called.
- It receives the authenticated user from the request object.
- Inside loginUser(), the AuthService.generateTokens() method creates a JWT token with the user’s ID (sub field).
- The response includes the JWT token, user ID, and email.
- The client receives a response with the user’s ID, email, and access token.
Client → Sends POST /auth/login (with email & password) LocalAuthGuard → Triggers canActivate() AuthService → validateLocalUser(email, password) Verifies user’s existence and password. LocalAuthGuard → Attaches user to request.user. AuthController → Calls loginUser() method. AuthService → Generates JWT token. AuthController → Sends response (user ID, email, access token).
Nest is MIT licensed.