This repository demonstrates a simple implementation of basic authentication using Node.js and Express.
- Secure user authentication with username and password
- Middleware for protecting routes
- Simple and clean structure for learning purposes
Ensure you have the following installed:
- Clone the repository:
git clone https://github.com/hasanbakhtiar/basic-auth.git
- Navigate to the project directory:
cd basic-auth
- Install dependencies:
or
npm install
bun install
Start the server with the following command:
npm start
or
bun start
The application will run on http://localhost:3000
(or the configured port). You can test the API using a browser or tools like Postman.
Method | Endpoint | Description | Authentication Required |
---|---|---|---|
POST | /login |
Authenticate user | No |
POST | /register |
Register user | No |
GET | /data |
Access protected resources | Yes |
For Admin | |||
GET | /users |
Access protected resources | Yes |
GET | /:id |
Access protected resources | Yes |
- To access protected routes, include a valid
Authorization
header with the formatBasic <base64-encoded-credentials>
.
basic-auth/
├── package.json # Project dependencies
├── server.js # Main server file
├── middleware/ # Authentication middleware
└── routes/ # Application routes
- Users send their credentials (username and password) in the HTTP headers.
- Credentials are validated against predefined or dynamically configured user data.
- If validation succeeds, users can access protected resources.
A middleware function is used to verify credentials for protected routes. Invalid or missing credentials result in an error response.
Contributions are welcome! Feel free to fork the repository, open an issue, or submit a pull request.
This project is licensed under the MIT License. See the LICENSE
file for more information.
For any questions or feedback, please reach out to:
- Name: Hasan Bakhtiar
- GitHub: @hasanbakhtiar