This is a sample project showcasing user authentication and authorization functionality for logging IN.
-
Clone the repository:
git clone <repository_url>
-
Install the dependencies:
npm install
-
Configure the server and database settings in the config folder.
-
Create a
.env
file and set these fields. PORT = 3002 JWT_KEY = krishna -
And, setup your database
-
{
"development": {
"username": "root",
"password": "<your data base password>",
"database": "AuthDatabase",
"host": "127.0.0.1",
"dialect": "mysql"
},
- Start the server.
-
MVC Architecture: The project follows a structured
MVC architecture
, separating concerns and promoting code modularity, maintainability, and scalability. -
Database Integration: The application seamlessly integrates with sequelize using
Mysql
, providing reliable data persistence and retrieval. -
User Registration: Users can create a new account by providing their email and password, which is securely stored in the database.
4 . User Authentication: Users can sign in to their account using their email and password, with password encryption for enhanced security
.
-
JSON Web Tokens (JWT): The application utilizes
JWTs
for user authentication, providing a secure and scalable authentication mechanism. -
Password Encryption: User passwords are securely hashed and encrypted using the bcrypt library, making them resistant to unauthorized access.
- Node.js
- Express.js
- sequelize
- Mysql
- Bcrypt
- JSON Web Tokens (JWT)
- Validator
The project follows an MVC (Model-View-Controller) architecture with the following folder structure:
src
: Contains the main project filesconfig
: Configuration files for server and databasecontrollers
: Handles the business logic for each routemodels
: Defines the data models using Mongoose schemasrepository
: Handles database operations using Mongoose modelsroutes
: Defines the API routes and routes requests to controllersservices
: Implements the business logic using repository and external libraries
index.js
: Entry point of the application
*Create a new user:
*Method: POST
*Endpoint: /api/v1/signUp
*Request body:
-Email (string, required): User's email address
-Password (string, required): User's password
*Response:
-Success: HTTP 201 Created with the created user data
-Error: HTTP 500 Internal Server Error with error message
*Sign in as a user:
*Method: POST
*Endpoint: /api/v1/signIn
*Request body:
-Email (string, required): User's email address
-Password (string, required): User's password
Response:
-Success: HTTP 200 OK with a success message and a JSON Web Token (JWT)
-Error: HTTP status code based on the error type (e.g., 400 Bad Request, 500 Internal Server Error)
*Check if a user is authenticated:
*Method: GET
*Endpoint: /api/v1/authenticated
*Request headers:
-x-access-token (string, required): JWT obtained during sign in
*Response:
-Success: HTTP 200 OK with a success message and user data
-Error: HTTP status code based on the error type (e.g., 401 Unauthorized, 500 Internal Server Error)