This Express application provides a robust user authentication system, utilizing JWTs (JSON Web Tokens) for secure access and session management. From registration to login, and even features like logging out of individual or all sessions, this app is a complete authentication solution.
-
Clone the repository:
git clone https://github.com/lucasodra/express_starter cd express_starter
-
Install dependencies:
npm install
-
Set up your environment:
Copy
env.example
to a new file named.env
:cp env.example .env
Ensure you update
.env
with the required environment variables. Ensure you have MongoDB installed and running locally. -
Start the server:
npm test
By default, the server runs on
http://localhost:3000
.
Base URL: http://localhost:3000/api
-
Register a new user
Endpoint:
/users/register
Method:
POST
Body:
{ "username": "<username>", "email": "<email>", "password": "<password>" }
-
Login an existing user
Endpoint:
/users/login
Method:
POST
Body:
{ "email": "<email>", "password": "<password>" }
-
Get profile of authenticated user
Endpoint:
/users/me
Method:
GET
Headers:
{ "Authorization": "Bearer <Your-Token>" }
-
Logout user from current device
Endpoint:
/users/logout
Method:
POST
Headers:
{ "Authorization": "Bearer <Your-Token>" }
-
Logout user from all devices
Endpoint:
/users/logoutAll
Method:
POST
Headers:
{ "Authorization": "Bearer <Your-Token>" }
Here's an overview of the main directories and files:
.
├── LICENSE
├── README.md
├── app.js # Main application entry point
├── env.example # Example environment file
├── expressController.js # Controller functions for routes
├── expressRoute.js # Route definitions
├── generate.key.js # Key generator utility
├── index.js # Server initialization
├── middleware/
│ └── auth.js # Authentication middleware
├── models/
│ └── User.js # User Mongoose model
└── test/
└── user.test.js # User-related tests using Axios
To test user-related functionalities, use the user.test.js
script. This script, built with Axios, tests the following:
- Registering a new user.
- Logging in an existing user.
- Retrieving the authenticated user's profile.
- Logging out the user from the current device.
- Logging out the user from all devices.
To run the test:
node test/user.test.js