HNG-User-org is a backend API project for user authentication and organisation management. It allows users to register, log in, and manage organisations they belong to or have created. This project is built using Node.js, Express, and PostgreSQL with Prisma ORM.
The project adheres to the following criteria:
-
User Model:
userId
(string, unique uuid)firstName
(string, required)lastName
(string, required)email
(string, unique, required)password
(string, required)phone
(string)
-
Validation:
- All fields are validated.
- On validation error, returns status code 422 with an appropriate error message.
-
User Authentication:
- Registration: Endpoint for user registration with password hashing.
- Login: Endpoint for user login using JWT for protected endpoints.
-
Organisation Management:
- A user can belong to multiple organizations.
- An organisation can contain multiple users.
- On registration, a default organisation is created with the user’s first name.
-
Organisation Model:
orgId
(string, unique uuid)name
(string, required)description
(string)
-
POST /auth/register: Registers a user and creates a default organisation.
- Request Body:
{ "firstName": "string", "lastName": "string", "email": "string", "password": "string", "phone": "string" }
- Successful Response:
{ "status": "success", "message": "Registration successful", "data": { "accessToken": "jwt", "user": { "userId": "string", "firstName": "string", "lastName": "string", "email": "string", "phone": "string" } } }
- Request Body:
-
POST /auth/login: Logs in a user.
- Request Body:
{ "email": "string", "password": "string" }
- Successful Response:
{ "status": "success", "message": "Login successful", "data": { "accessToken": "jwt", "user": { "userId": "string", "firstName": "string", "lastName": "string", "email": "string", "phone": "string" } } }
- Request Body:
- GET /api/users/:id: Retrieves a user’s record (protected).
-
GET /api/organisations: Retrieves all organizations the user belongs to or created (protected).
-
GET /api/organisations/:orgId: Retrieves a single organization record (protected).
-
POST /api/organisations: Creates a new organization (protected).
- Request Body:
{ "name": "string", "description": "string" }
- Successful Response:
{ "status": "success", "message": "Organisation created successfully", "data": { "orgId": "string", "name": "string", "description": "string" } }
- Request Body:
-
POST /api/organisations/:orgId/users: Adds a user to a specific organization.
- Request Body:
{ "userId": "string" }
- Request Body:
- Clone the repository:
git clone https://[email protected]/yourusername/HNG-User-org.git cd HNG-User-Org
- Install dependencies
npm install
- Set up environmental variables Create a .env file and add your PostgreSQL database configuration and JWT secret.
- Prisma setup
- Initialize prisma in your project
npx prisma init
- Migrate your database
npx prisma migrate dev --name init
- Generate the prisma client
npx prisma generate
- Initialize prisma in your project
To run the unit and end-to-end tests, use
npm test
- Node.js
- Express
- PostgreSQL
- Prisma ORM
- JWT from jsonwebtoken for authentication
Thanks to the HNG team for this task