Skip to content

Latest commit

 

History

History
227 lines (180 loc) · 7.71 KB

README.md

File metadata and controls

227 lines (180 loc) · 7.71 KB

Table of Contents

Nodejs-AuthApp

A Node.js app that implements APIs to emulate basic User authentication.

Host Domain
Heroku https://nodejs-authapp.herokuapp.com/

Postman collection link

Go to Postman > Import > Link (Tab) > Paste the link and Continue
https://www.getpostman.com/collections/c319720e2fc33dd89af7

Technologies/Packages Used!

Project structure

src is the root of the project tree.

  • middlewares holds files that are essential for APIs where user authentication is a must.
  • models holds all schemas that will be used to model mongoose documents.
  • routes holds all the files that implement certain APIs to the app. Each file is organized based on the APIs it holds.
  • scripts holds files that act like scripts, i.e, they initiate services. These scripts are executed only once (inside the index.js).
  • utils provides utilities that provide functionality to certain areas of the app. For example, twilio.sms.js provides a class with method sendSms to send an SMS.

Middlewares

All the middlewares focus on validating the user's authenticity by obtaining required fields from the request.

Method - POST
Query params -

  • id

Headers -

  • Authorization

The key difference between login_token.auth and login_token.unverified.auth is that this middleware ignores the state of user's email or phone's verification.

Method - POST
Query params -

  • id

Headers -

  • Authorization

This middleware validates user's authenticity by validating user's two-factor authentication security questions. This is the only authentication that does not rely on the login token.

Method - POST
Query params -

  • id

Body - application/json
Body params -

  • first_question_answer
  • second_question_answer
  • third_question_answer

Routes

The routing can be simplified by splitting the routes into three parts.

I. UserAuth

1. /user/auth/signin

Use this api to sign in as a User

Method - POST
Body - application/json
Body params -

  • email (Not required if phone is provided)
  • phone (Not required if email is provided)
  • password

2. /user/auth/signup

Use this api to sign up as a User

Method - POST
Body - application/json
Body params -

  • data {
    • full_name
    • email
    • phone
    • password
      }

3. /user/auth/resetPassword

Use this api to reset password (while signed in)

Method - POST
Body - application/json
Body params -

  • password (New password)

II. UserInfo

1. /user

Use this api to fetch user's details

Method - POST
Middlewares - login_token.auth

2. /user/update

Use this api to update user fields

Method - POST
Middlewares - login_token.auth
Body params -

  • data {
    • full_name?
    • email?
    • phone?
      }

3. /user/avatar

Use this api to set a new avatar

Method - POST
Middlewares - login_token.auth
Body params -

  • avatar (File)

Use this api to delete an avatar

Method - DELETE
Middlewares - login_token.auth

4. /user/verify

Not meant to be used. This api will be used to confirm verification (either for emails, phones, or password resets)

Method - GET
Query params -

  • token

5. /user/verify/requestEmail

Use this api to request an email confirmation (timeout. 120s)

Method - POST
Middlewares - login_token.unverified.auth
Body params -

  • email? (Provide email to update old unverified email with new one)

6. /user/verify/requestPhone

Use this api to request a phone confirmation (timeout. 120s)

Method - POST
Middlewares - login_token.unverified.auth
Body params -

  • phone? (Provide phone to update old unverified phone with new one)

III. User2FA

1. /user/2fa

Use this api to fetch user's 2fa details

Method - POST
Middlewares - login_token.auth

2. /user/2fa/enable

Use this api to enable and/or update user's 2fa details

Method - POST
Middlewares - login_token.auth
Body params -

  • data {
    • first_question? {
      • question
      • answer
        }
    • second_question? {
      • question
      • answer
        }
    • third_question? {
      • question
      • answer
        }

3. /user/2fa/disable

Use this api to disable user's 2fa

Method - PATCH
Middlewares - login_token.auth

Use this api to disable and clear user's 2fa details

Method - DELETE
Middlewares - login_token.auth

4. /user/2fa/resetPassword

Use this api to reset password (while signed out)

Method - POST
Middlewares - 2fa.auth
Body params -

  • password (New password)