This is a template for user authentication (SignIn and SignUp) usign the Nestjs framework, with prisma and docker integrations.
- Clone the project in your machine
- Create a .env file and set the following variables:
- DATABASE_URL: Prisma url for database connection
- JWT_SECRET: Secret for Json Web Token
- POSTGRES_USER: Postgres user for docker
- POSTGRES_PASSWORD: Postgres password for docker
- POSTGRES_DB: Postgres database for docker
- Install the node modules with
npm install
- Up the docker container wiht
npm run db:dev:up
- Create the prisma migrations if they do not exist with
npm run prisma:dev:migrate
- Deploy the migrations to the database with
npm run prisma:dev:deploy
- Start the application with
npm run start
The prisma schema is comming with a pre defined User model that can be refactored to suit your needs.
The current dto is set up only with email and password, that can altered to carry any atributes that you may need.
AuthModule
is a NestJS module responsible for handling user authentication within the application. It integrates JWT for secure token-based authentication and includes necessary services, controllers, and strategies.
- JwtModule: Configures the JWT module for the application.
- global: Indicates that this configuration applies globally across the application.
- secret: JWT secret key, retrieved from environment variables (
process.env.JWT_SECRET
). - signOptions: Options for signing the JWT, including the token expiration time (
expiresIn: '1d'
).
- AuthService: Service that contains the authentication logic such as user registration and login.
- JwtStrategy: Strategy for validating JWT tokens.
- AuthController: Controller that defines the authentication endpoints (
signUp
,login
,get_user
).
-
Environment Variables: Set up the necessary environment variables for the JWT secret.
Create a
.env
file in the root directory of your project (if it doesn't already exist) and add the following:JWT_SECRET=your_secret_key_here
-
JwtModule: The JWT module is configured with the secret and token expiration within the
AuthModule
.
AuthService
: Implements the core authentication logic.AuthController
: Defines the endpoints for authentication-related actions.JwtStrategy
: Validates and processes JWT tokens for secure access.
AuthController
is responsible for managing user authentication within the application. It provides endpoints for user registration, login, and retrieval of authenticated user information.
Registers a new user.
- Body: Should contain an object of type
SignUpDto
.
{
"email": "[email protected]",
"password": "examplePassword"
}
- 200 OK: Returns an object with information about the created user or a success message.
- 400 Bad Request: If the provided data is invalid.
Authenticates a user and returns a JWT token.
- Body: Should contain an object with
email
andpassword
fields.
{
"email": "[email protected]",
"password": "examplePassword"
}
- 200 OK: Returns an object containing the JWT token.
- 401 Unauthorized: If the credentials are invalid.
Retrieves information about the authenticated user based on the provided JWT token.
- Headers: Should contain the JWT token in the authorization header
Authorization: Bearer <token>
.
- 200 OK: Returns an object with information about the authenticated user.
- 401 Unauthorized: If the token is invalid or absent.
The application will be available and ready to receive requests at the documented endpoints.
AuthService
: Service responsible for authentication logic.SignUpDto
: Data Transfer Object for registering new users.AuthGuard
: JWT authentication guard provided by Passport.
AuthService
is a service in a NestJS application responsible for handling authentication logic. It includes methods for user registration, login, and JWT token generation, and interacts with a Prisma database to manage user data.
- PrismaService: Service for interacting with the Prisma ORM to manage database operations.
- JwtService: Service provided by
@nestjs/jwt
to handle JWT token creation and verification. - bcrypt: Library for hashing passwords.
Registers a new user by hashing the password, saving the user data to the database, and generating a JWT token.
-
Parameters:
dto
(SignUpDto): Data transfer object containing user registration details.
-
Returns:
- An object containing the user data (excluding the password) and a JWT token.
Authenticates a user by verifying the email and password, and generates a JWT token if the credentials are valid.
-
Parameters:
email
(string): The user's email.password
(string): The user's password.
-
Returns:
- An object containing the user data (excluding the password) and a JWT token.
-
Throws:
UnauthorizedException
if the credentials are invalid.
Retrieves a user from the database based on the email.
-
Parameters:
email
(string): The user's email.
-
Returns:
- An object containing the user data.
Generates a JWT token for the given user.
-
Parameters:
user
(any): An object containing the user data.
-
Returns:
- A string representing the JWT token.
JwtStrategy
is a strategy for handling JWT authentication in a NestJS application. It leverages Passport.js to validate JWT tokens and ensures secure access to protected routes by verifying user credentials against the database.
- PrismaService: Service for interacting with the Prisma ORM to manage database operations.
- PassportStrategy: Base class for creating custom strategies with Passport.js.
- ExtractJwt, Strategy: Components from
passport-jwt
used to extract and verify JWT tokens.
- jwtFromRequest: Specifies that the JWT token should be extracted from the Authorization header as a Bearer token.
- secretOrKey: JWT secret key, retrieved from environment variables (
process.env.JWT_SECRET
).
Validates the JWT token payload by checking if the user exists in the database.
-
Parameters:
payload
(any): The payload extracted from the JWT token, containing user information such as email.
-
Returns:
- The user object if the user is found.
-
Throws:
UnauthorizedException
if the user is not found.
The strategy extracts the JWT token from the Authorization header in the format Bearer <token>
.
- Extract Payload: The
validate
method extracts the email from the payload. - Database Lookup: It uses
PrismaService
to find the user in the database by email. - Return User: If the user exists, it returns the user object.
- Unauthorized Exception: If the user does not exist, it throws an
UnauthorizedException
.
This project is licensed under the terms of the MIT license. See the LICENSE file for more details.