AuthAPI is a Node.js application built with Express and Mongoose that provides a foundational backend for user authentication and management. It connects to a MongoDB Atlas database and offers RESTful API endpoints for user registration, login, and session handling, serving as a flexible starting point for more complex applications.
- User Registration: Allows new users to create an account.
- User Login: Authenticates users and manages sessions.
- Session Management: Uses cookies to handle user sessions securely.
- User Data Management: Endpoints to retrieve, update, and delete user data.
- Authorization: Ensures that users can modify only their own data.
api/
├── src/
│ ├── controllers/
│ │ ├── authentication.ts # Handles authentication logic (login/register)
│ │ └── users.ts # Handles user data management (CRUD operations)
│ ├── db/
│ │ └── users.ts # Database model and user-related functions
│ ├── helpers/
│ │ └── index.ts # Helper functions for authentication
│ ├── middlewares/
│ │ └── index.ts # Middleware for authentication and authorization checks
│ ├── router/
│ │ ├── authentication.ts # Routes for authentication
│ │ ├── users.ts # Routes for user management
│ │ └── index.ts # Main router file
│ ├── index.ts # Entry point of the application
├── package.json # Project metadata and dependencies
├── nodemon.json # Configuration for nodemon
└── tsconfig.json # TypeScript configuration
- Node.js (v14 or higher)
- MongoDB Atlas account
- TypeScript (optional, for development)
-
Clone the Repository:
git clone https://github.com/ARH-MNAJS/AuthAPI.git cd AuthAPI
-
Install Dependencies:
npm install
-
Configure MongoDB Connection:
- Update the
MONGO_URL
insrc/index.ts
with your MongoDB Atlas connection string.
- Update the
-
Run the Application:
npm start
The server will start on
http://localhost:8080
.
- POST /auth/register: Register a new user.
- POST /auth/#: Log in an existing user.
- GET /users: Retrieve all users (requires authentication).
- DELETE /users/:id: Delete a user by ID (requires authentication and ownership).
- PATCH /users/:id: Update a user's username by ID (requires authentication and ownership).
Endpoint: POST /auth/register
- Function:
register
- Description: Handles user registration by creating a new user in the database.
- Parameters: Expects
username
,email
, andpassword
in the request body. - Response: Returns the created user object or an error message.
Endpoint: POST /auth/#
- Function:
login
- Description: Authenticates a user by checking their email and password.
- Parameters: Expects
email
andpassword
in the request body. - Response: Returns the user object and sets a session cookie if successful.
- Middleware:
isAuthenticated
- Description: Verifies user authentication by checking the session token stored in cookies.
- Usage: Applied to routes that require user authentication.
Functions in src/db/users.ts
:
- getUsers: Retrieves all users from the database.
- getUserByEmail: Finds a user by their email address.
- getUserBySessionToken: Finds a user by their session token.
- getUserById: Retrieves a user by their ID.
- createUser: Creates a new user in the database.
- deleteUserById: Deletes a user by their ID.
- updateUserById: Updates user information by their ID.
Functions in src/controllers/users.ts
:
- getAllUsers: Retrieves all users and returns them in the response.
- deleteUser: Deletes a user by ID and returns the deleted user object.
- updateUser: Updates a user's username by ID and returns the updated user object.
Functions in src/middlewares/index.ts
:
- isOwner: Verifies that the current user is the owner of the resource they are modifying; returns a
403 Forbidden
status if not. - isAuthenticated: Checks if a user is authenticated and, if so, merges user information into the request object.
Functions in src/helpers/index.ts
:
- random: Generates a random string for use as a salt in password hashing.
- authentication: Creates a hashed password using HMAC with SHA-256.
AuthAPI is a versatile starting point for building applications that require secure authentication and user data management. It leverages modern technologies like Express and MongoDB, making it easy to extend and adapt as needed.