This Node.js Express server handles user authentication including registration, login, logout, and session validation.
- User Registration
- User Login
- User Logout
- Session Validation
Before you begin, ensure you have met the following requirements:
- Node.js and npm installed
- MongoDB running locally or remotely (update connection URI accordingly)
To install the necessary packages, run the following command:
npm install
Create a .env file in the root directory and update it with your MongoDB URI and any other configurations such as your secret key for JWT.
- Example .env file:
PORT=5000 URI=mongodb://localhost:27017/myauthdb SECRET_ACCESS_TOKEN=your_secret_key
To start the server, run:
npm start
The server will start running on http://localhost:5000.
-
URL:
/auth/register
-
Method:
POST
-
Body:
{ "first_name": "John", "last_name": "Doe", "email": "[email protected]", "password": "password123" }
-
Success Response:
201 Created
+{ "status": "success", "data": { "first_name": "John", "last_name": "Doe", "email": "[email protected]" }, "message": "Your account has been successfully created." }
-
Error Response:
400 Bad Request
+{ "error": { "status": "failed", "message": "It seems you already have an account, please log in instead." } }
-
URL:
/auth/login
-
Method:
POST
-
Body:
{ "email": "[email protected]", "password": "password123" }
-
Success Response:
200 OK
+ user data JWT token (set in HTTPOnly cookie) +{ "status": "success", "data": { "first_name": "John", "last_name": "Doe", "email": "[email protected]" }, "message": "You have successfully logged in." }
-
Error Response:
401 Unauthorized
+{ "error": { "status": "failed", "message": "Invalid email or password. Please try again with the correct credentials." } }
- URL:
/auth/logout
- Method:
GET
- Success Response:
200 OK
+{ "status": "success", "data": { "first_name": "John", "last_name": "Doe", "email": "[email protected]" }, "message": "You have successfully logged in." }
- Error Response:
401 Unauthorized
(if token is invalid or expired) +{ "error": { "status": "failed", "message": "Invalid email or password. Please try again with the correct credentials." } }
- URL:
/auth/verify
- Method:
GET
- Success Response:
200 OK
+{ "status": "success", "message": "You are authenticated." }
- Error Response:
401 Unauthorized
(if token is invalid or expired)
- Please read CONTRIBUTING.md for details on our code of conduct and the process for submitting pull requests.
- This project is licensed under the MIT License - see the LICENSE.md file for details.