Template repo for building a NestJS client of a Auth0 tenant.
- Fully functional auth module, includes 3 core routes:
- Auth session refresh on each request.
- Auth session validation guard and decorator (for protected endpoints)
- Integration with auth0 for password grant and refresh token grant flows.
- Cache support for serverless auth sessions.
- TypeORM configuration for DB interactions.
- Organized test files structures with centralized mocks.
- Dockerization of all dependencies.
Below you can find a simple reference to the base endpoints of the repo.
- Request URL: https://api_url/auth/login
- HTTP Method: POST
- Request body:
{
// application/json
"username": "email@domain", // email or username
"password": "secret-pass-123"
}
- Returns: a. Body with following structure
{
"user": {
"createdAt": "2024-05-01T00:00:00Z",
"updatedAt": "2024-05-01T00:00:00Z",
"userId": "auth0UserId",
"username": "a unique username",
"email": "[email protected]",
"avatar": "auth0 managed avatar",
"firstName": "John",
"lastName": "Doe"
},
"sessionId": "uuid-string-randomly-generated" // Raw session id
}
b. Signed cookie session_id
Deletes session cookie and session. The user must re-login after calling this endpoint
- Request URL: https://api_url/auth/logout
- HTTP Method: GET
- Returns:
{
"message": "User logged out."
}
Returns the currently logged user.
- Request URL: https://api_url/auth/me
- HTTP Method: GET
- Returns:
{
"createdAt": "2024-05-01T00:00:00Z",
"updatedAt": "2024-05-01T00:00:00Z",
"userId": "auth0UserId",
"username": "a unique username",
"email": "[email protected]",
"avatar": "auth0 managed avatar",
"firstName": "John",
"lastName": "Doe"
}
This project was developed with Node iron (20.12.2) and it is highly recommended to use the same version to run the project.
While not completely required. It is recommended to have a stable version of docker installed for easier setup. It is required to provision a DB server and a redis store instance. For details on the dockerization check the docker-compose.yml file.
Setup of the project includes the main NestJS app depedencies as well as dependant microservices.
$ docker compose up -d # Setups microservices
$ npm install
An example.env
file is attached to the root of the project. You should rename it / copy to .env
to store the required env vars.
$ cp example.env .env
The server will listen on the .env
specified port (default 3000) after running the following command.
# watch mode
$ npm run start:dev
# unit tests
$ npm run test
# test coverage
$ npm run test:cov