The User-Organisation App is a backend API application that allows users to register, login, and manage organisations. Each user can belong to multiple organisations, and each organisation can have multiple users. Upon registration, a default organisation is created for the user. The app provides endpoints for creating, retrieving, and managing organisations and users within them.
-
User Authentication:
- Register a new user.
- Login an existing user.
- Authentication using JWT tokens.
-
Organisation Management:
- Create organisations.
- Retrieve a single organisation by ID.
- Retrieve all organisations a user belongs to.
- Add users to an organisation.
-
Protected Routes:
- Ensure routes are protected and only accessible to authenticated users.
- Ensure users can only manage organisations they created or belong to.
-
Clone the repository.
-
Folder structure
hng_task02/
├── src/
│ ├── controllers/
│ │ ├── auth.js
│ │ └── organisation.js
│ ├── models/
│ │ ├── user.js
│ │ └── organisation.js
│ ├── helpers/
│ │ ├── auth.js
│ │ └── db.config.js
│ ├── middlewares/
│ │ └── auth.js
│ ├── models/
│ │ ├── organisation.js
│ │ └── user.js
│ ├── routes/
│ │ ├── auth.js
│ │ └── organisation.js
│ └── index.js
├── .env
├── package.json
├── README.md
├── initialSetup.js
└── tests/
├── auth.spec.js
└── organisation.spec.js
- Install dependencies using
npm install
. - Create a
.env
file with the following environment variables:PORT=8000 DATABASE_URL=your_database_url JWT_SECRET=your_jwt_secret
- Run the application using
npm start
.
- Endpoint:
[POST] /api/auth/register
- Request Body:
{ "firstName": "Didi", "lastName": "Roy", "email": "didi@email.com", "password": "password123" }
- Response:
{ "status": "success", "message": "Registration successful", "data": { "accessToken": "jwt_token", "user": { "userId": "user_id", "firstName": "Didi", "lastName": "Roy", "email": "didi@email.com", "phone": "1234567890" } } }
- Endpoint:
[POST] /api/auth/login
- Request Body:
{ "email": "didi@email.com", "password": "password123" }
- Response:
{ "status": "success", "message": "Login successful", "data": { "accessToken": "jwt_token", "user": { "userId": "user_id", "firstName": "Didi", "lastName": "Roy", "email": "didie@email.com", "phone": "1234567890" } } }
- Endpoint:
[POST] /api/organisations
- Request Body:
{ "name": "John's Organisation", "description": "This is John's default organisation." }
- Response:
{ "status": "success", "message": "Organisation created successfully", "data": { "orgId": "org_id", "name": "John's Organisation", "description": "This is John's default organisation." } }
- Endpoint:
[GET] /api/organisations/:orgId
- Response:
{ "status": "success", "message": "Organisation fetched successfully", "data": { "orgId": "org_id", "name": "John's Organisation", "description": "This is John's default organisation." } }
- Endpoint:
[GET] /api/organisations
- Response:
{ "status": "success", "message": "Organisations fetched successfully", "data": { "organisations": [ { "orgId": "org_id_1", "name": "John's Organisation", "description": "This is John's default organisation." }, { "orgId": "org_id_2", "name": "Another Organisation", "description": "This is another organisation." } ] } }
- Endpoint:
[POST] /api/organisations/:orgId/users
- Request Body:
{ "userId": "new_user_id" }
- Response:
{ "status": "success", "message": "User added to organisation successfully" }
Ensure you have PostgreSQL set up and running. Use the provided db.config.js
file to configure the database connection.
import { Pool } from 'pg';
import dotenv from 'dotenv';
dotenv.config();
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});
const connectDb = async () => {
try {
await pool.connect();
console.log("DB Connected successfully.");
} catch (error) {
console.error("Unable to connect to the database:", error);
}
};
connectDb();
export default pool;
Make sure to run the initial setup to create necessary tables:
import { createUserTable } from "./src/models/user.js";
import { createOrganisationTable, createUserOrganisationTable } from "./src/models/organisation.js";
(async () => {
await createUserTable();
await createOrganisationTable();
await createUserOrganisationTable();
})();
Use the following command to start the application:
npm start
The application should now be running on the specified port, and you can start interacting with the API endpoints.