This project is a simple implementation of two-factor authentication (2FA) in a Node.js application using OTP (One Time Password). The application uses Express.js as the web framework and Prisma as the ORM for interacting with the database.
- User Registration
- User Login
- Generate OTP
- Verify OTP
- Validate OTP
- Disable OTP
- Node.js
- Express.js
- Prisma
- PostgreSQL
- OTPAuth
- Clone the repository:
git clone https://github.com/AllanJone/2fa_node_js.git
cd 2fa_node_js
- Install the dependencies:
npm install
- Set up the database:
You need to have PostgreSQL installed on your machine or hosted in a cloud environment.
Update the DATABASE_URL
in the .env
file with your own PostgreSQL connection string.
Run the following command to apply the migrations and create the database schema:
npm run db:migrate
npm run db:push
- Start the server:
npm start
The server will start on port 8000.
POST /api/auth/register
: Register a new user.POST /api/auth/login
: Login a user.POST /api/auth/generate-otp
: Generate OTP for a user.POST /api/auth/verify-otp
: Verify the OTP of a user.POST /api/auth/validate-otp
: Validate the OTP of a user.POST /api/auth/disable-otp
: Disable the OTP of a user.
To register a user, make a POST
request to /api/auth/register
with the following body:
{
"name": "John Doe",
"email": "johndoe@gmail.com",
"password": "password123"
}
To login a user, make a POST
request to /api/auth/login
with the following body:
{
"email": "johndoe@gmail.com",
"password": "password123"
}
To generate an OTP for a user, make a POST
request to /api/auth/generate-otp
with the following body:
{
"user_id": "user_id"
}
To verify the OTP of a user, make a POST
request to /api/auth/verify-otp
with the following body:
{
"user_id": "user_id",
"token": "123456"
}
To validate the OTP of a user, make a POST
request to /api/auth/validate-otp
with the following body:
{
"user_id": "user_id",
"token": "123456"
}
To disable the OTP of a user, make a POST
request to /api/auth/disable-otp
with the following body:
{
"user_id": "user_id"
}