This API provides endpoints for user authentication and organisation management, built with Express.js. It uses JWT (JSON Web Tokens) for secure authentication.
- Node.js
- npm
- Postgres (or any other database you are using)
- Express.js
- JWT
-
Clone the repository:
git clone https://github.com/trevorjob/user-authentication-api.git
-
Navigate to the project directory:
cd authentication-api
-
Install dependencies:
npm install
-
Set up environment variables. Create a .env file in the root directory and add the following:
JWT_SECRET=<your_jwt_secret> DB_ADDR=<your_db_uri>
- Start the server:
npm run start
- The API will be accessible at http://localhost:3000.
- Register a new user
- URL:
/auth/register
- Method: POST
- Body:
{ "firstName": "example", "lastName": "example" "phone": "009987866" "password": "example" "email": "[email protected]" }
- Response:
- 201 Created: User registered successfully
- 400 Bad Request: Invalid input
- URL:
- Login a user
- URL:
/auth/login
- Method: POST
- Body:
{ "email": "exampleEmail", "password": "examplePassword" }
- Response:
- 200 OK: Successful login, returns JWT token
- 401 Unauthorized: Invalid credentials
- URL:
- Get all organisations
- URL:
/organisations
- Method: GET
- Headers:
Authorization: Bearer <JWT_TOKEN>
- Response:
- 200 OK: Returns a list of organisations
- 401 Unauthorized: Invalid or missing token
- URL:
- Get an organisation by ID
- URL:
organisations/<orgId>
- Method: GET
- Headers:
Authorization: Bearer <JWT_TOKEN>
- Response:
- 200 OK: Returns organisation details
- 401 Unauthorized: Invalid or missing token
- 404 Not Found: Organisation not found
- URL:
- Create a new organisation
- URL:
/organisations
- Method: POST
- Headers:
Authorization: Bearer <JWT_TOKEN>
- Body:
{ "name": "Example Organisation", "description": " example description" }
- Response:
- 201 Created: Organisation created successfully
- 401 Unauthorized: Invalid or missing token
- URL:
-
Get a user by ID
- URL:
users/<id>
- Method: GET
- Headers:
Authorization: Bearer <JWT_TOKEN>
- Response:
- 200 OK: Returns user details
- 401 Unauthorized: Invalid or missing token
- 404 Not Found: User not found
- URL:
-
Add a user to an organisation
- URL:
/:orgId/users
- Method: POST
- Headers:
Authorization: Bearer <JWT_TOKEN>
- Body:
{ "userId": "exampleUserId" }
- Response:
- 200 OK: User added to organisation successfully
- 401 Unauthorized: Invalid or missing token
- URL:
Contributions are welcome! Please open an issue or submit a pull request for any changes.
License This project is licensed under the MIT License.