This project implements an authentication system using Express.js, Mongoose, Passport.js, and JWT. It includes local registration and login, as well as social logins with Facebook, Google, and Apple. The system also supports OTP-based email verification and password reset.
-
Clone the repository:
git clone https://github.com/your-username/otp-api.git cd otp-api -
Install the dependencies:
npm install
Create a .env file in the root directory and add the following environment variables:
MONGO_URI=your_mongodb_connection_string
JWT_SECRET=your_jwt_secret
PORT=3000
EMAIL_SERVICE=gmail
EMAIL_USER=your_email@gmail.com
EMAIL_PASS=your_email_password
FACEBOOK_APP_ID=your_facebook_app_id
FACEBOOK_APP_SECRET=your_facebook_app_secret
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
APPLE_CLIENT_ID=your_apple_client_id
APPLE_TEAM_ID=your_apple_team_id
APPLE_KEY_ID=your_apple_key_id
APPLE_PRIVATE_KEY=your_apple_private_key
SESSION_SECRET=your_session_secretEnsure all the environment variables are correctly set, especially the credentials for email and social logins.
Start the server:
npm startThe server should be running on http://localhost:3000.
- POST /api/auth/register
-
Registers a new user and sends an OTP to their email for verification.
-
Body parameters:
email,password,confirmPassword -
Example request:
curl -X POST http://localhost:3000/api/auth/register -H "Content-Type: application/json" -d '{"email":"test@example.com","password":"password123","confirmPassword":"password123"}'
-
- POST /api/auth/verify
-
Verifies the OTP sent to the user's email.
-
Body parameters:
email,otp -
Example request:
curl -X POST http://localhost:3000/api/auth/verify -H "Content-Type: application/json" -d '{"email":"test@example.com","otp":"123456"}'
-
- POST /api/auth/signin
-
Authenticates a user with their email and password.
-
Body parameters:
email,password -
Example request:
curl -X POST http://localhost:3000/api/auth/signin -H "Content-Type: application/json" -d '{"email":"test@example.com","password":"password123"}'
-
-
POST /api/auth/request-password-reset
-
Requests an OTP for password reset.
-
Body parameters:
email -
Example request:
curl -X POST http://localhost:3000/api/auth/request-password-reset -H "Content-Type: application/json" -d '{"email":"test@example.com"}'
-
-
POST /api/auth/reset-password
-
Resets the user's password using the OTP.
-
Body parameters:
email,otp,newPassword,confirmNewPassword -
Example request:
curl -X POST http://localhost:3000/api/auth/reset-password -H "Content-Type: application/json" -d '{"email":"test@example.com","otp":"123456","newPassword":"newpassword123","confirmNewPassword":"newpassword123"}'
-
-
GET /auth/facebook
- Redirects the user to Facebook for authentication.
-
GET /auth/facebook/callback
-
Facebook redirects to this endpoint after authentication.
-
Example URL:
http://localhost:3000/auth/facebook/callback
-
-
GET /auth/google
- Redirects the user to Google for authentication.
-
GET /auth/google/callback
-
Google redirects to this endpoint after authentication.
-
Example URL:
http://localhost:3000/auth/google/callback
-
-
GET /auth/apple
- Redirects the user to Apple for authentication.
-
POST /auth/apple/callback
-
Apple redirects to this endpoint after authentication.
-
Example URL:
http://localhost:3000/auth/apple/callback
-
-
Register a new user: Send a POST request to
/api/auth/registerwith the user's email and password. An OTP will be sent to the user's email. -
Verify email: Send a POST request to
/api/auth/verifywith the email and OTP to verify the user's email address. -
Login: Send a POST request to
/api/auth/signinwith the email and password to log in. -
Request password reset: Send a POST request to
/api/auth/request-password-resetwith the email to receive an OTP for password reset. -
Reset password: Send a POST request to
/api/auth/reset-passwordwith the email, OTP, and new password to reset the password. -
Social login: Use the provided endpoints to log in with Facebook, Google, or Apple.
This documentation provides a comprehensive guide for setting up and using the OTP-Api project, including environment setup, running the server, and using the various API endpoints. Make sure to replace placeholder values in the .env file with actual credentials.