MERN Stack - Server

MongoDB - Express - React - Redux - React Native - NodeJS

Source Code

CI Testing code style: prettier license

mern-logo

API

General

Health Check

  • Method: GET
  • Content-Type: application/json
  • Endpoint
/api/alive
  • Response payload
Property Name Type Description
status string Service status

Sample response

{
  "status": "pass"
}

Authentication

Sign Up

  • Method: POST
  • Content-Type: application/json
  • Endpoint
/api/auth/signup
  • Resquest Body Payload
Property Name Type Required Description
username string Yes Username
email string Yes Email
password string Yes Password
firstName string No First name
lastName string No Last name

Sample request body payload

{
  "username": "user",
  "email": "user@tdev.app",
  "password": "password",
  "firstName": "User",
  "lastName": "Account"
}
  • Response Payload
Property Name Type Description
message string Message

Sample response

{
  "message": "Your account has been created successfully"
}

Sign In With Email

  • Method: POST
  • Content-Type: application/json
  • Endpoint
/api/auth/signin
  • Resquest Body Payload
Property Name Type Required Description
username string Yes Either username or email is required
email string Yes Either username or email is required
password string Yes Password

Sample request body payload

{
  "email": "user@tdev.app",
  "password": "password"
}
  • Response Payload
Property Name Type Description
token string JWT token
expiresAt number Expires at time (seconds)
user object User info

Sample response

{
  "token": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1ZTQ3YTk2MDBjYWM4NTE3OTBmMjk2NjMiLCJ1c2VySWQiOiI1ZTQ3YTk2MDBjYWM4NTE3OTBmMjk2NjIiLCJpYXQiOjE1ODE4MDk2ODYsImV4cCI6MTU4Njk5MzY4Nn0.6rjee9HpGVP-IsKfGBAiqU8Y6FHuuBN25odKZExig7liOhJd2lq_eUn8JUOtz7QpRX5RvGYzbzlxdRED0boNVA",
  "expiresAt": 1586993686,
  "signedInWith": "local",
  "user": {
    "id": "5e47a9600cac851790f29662",
    "username": "user",
    "email": "user@tdev.app",
    "status": "active",
    "firstName": "User",
    "lastName": "Account",
    "role": "user",
    "permissions": {
      "userInsert": false,
      "userModify": false,
      "userRead": false,
      "postInsert": false,
      "postModify": false,
      "postRead": true
    },
    "provider": {
      "local": {
        "userId": "5e47a9600cac851790f29662"
      }
    },
    "createdAt": "2020-02-15T08:18:40.429Z",
    "updatedAt": "2020-02-15T08:18:40.429Z"
  }
}

Sign In With Facebook

  • Method: POST
  • Content-Type: application/json
  • Endpoint
/api/auth/facebook
  • Resquest Body Payload
Property Name Type Required Description
accessToken string Yes The Facebook access token
refreshToken string No The Facebook refresh token

Sample request body payload

{
  "accessToken": "EAAx0076n7rwBAE76vGbLS0y5kK01uZB7urxtWC1eh30NIZBO4G0XH1gA2CSRGtNFxaZBKiUlT0nZAPk8AzyiK1DGg47HOaWnkfaG4FyZCPhiEQZByPbP9dWB6JZBw6GiIXPGmnSdkIAzuT5MEK5slyAEs8jZCazvs4wziZBdx2eIsWeSN5Hhxy9RRrZCSHrWGm8hvI9DrxZCkeR4BWVxLN6YItOuZC1A80VctLAZD"
}
  • Response Payload
Property Name Type Description
token string JWT token
expiresAt number Expires at time (seconds)
signedInWith string The auth provider that the user used to sign in with
user object User info

Sample response

{
  "token": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1ZTQ3YTk2MDBjYWM4NTE3OTBmMjk2NjMiLCJ1c2VySWQiOiI1ZTQ3YTk2MDBjYWM4NTE3OTBmMjk2NjIiLCJpYXQiOjE1ODE4MDk2ODYsImV4cCI6MTU4Njk5MzY4Nn0.6rjee9HpGVP-IsKfGBAiqU8Y6FHuuBN25odKZExig7liOhJd2lq_eUn8JUOtz7QpRX5RvGYzbzlxdRED0boNVA",
  "expiresAt": 1586993686,
  "signedInWith": "facebook",
  "user": {
    "id": "5e47a9600cac851790f29662",
    "username": "user",
    "email": "user@tdev.app",
    "status": "active",
    "firstName": "User",
    "lastName": "Account",
    "role": "user",
    "permissions": {
      "userInsert": false,
      "userModify": false,
      "userRead": false,
      "postInsert": false,
      "postModify": false,
      "postRead": true
    },
    "provider": {
      "facebook": {
        "userId": "197154714957030",
        "picture": "picture-url.jpg"
      }
    },
    "createdAt": "2020-02-15T08:18:40.429Z",
    "updatedAt": "2020-02-15T08:18:40.429Z"
  }
}

Sign In With Google

  • Method: POST
  • Content-Type: application/json
  • Endpoint
/api/auth/google
  • Resquest Body Payload
Property Name Type Required Description
accessToken string Yes The Google access token
refreshToken string No The Google refresh token

Sample request body payload

{
  "accessToken": "ya29.ImC_B0mUYTJfjnd7fCgZvIc6WWjj3GxMC0LwiOyhqDIQBJyAJN9eSRCLNDcr6bXCtHn5oSNOsB-Es2AIIOso-ZMBe0w31K2Ud85dpq3ep4tZI6uftTq_Vjjnzf4esuVO73c"
}
  • Response Payload
Property Name Type Description
token string JWT token
expiresAt number Expires at time (seconds)
signedInWith string The auth provider that the user used to sign in with
user object User info

Sample response

{
  "token": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1ZTQ3YTk2MDBjYWM4NTE3OTBmMjk2NjMiLCJ1c2VySWQiOiI1ZTQ3YTk2MDBjYWM4NTE3OTBmMjk2NjIiLCJpYXQiOjE1ODE4MDk2ODYsImV4cCI6MTU4Njk5MzY4Nn0.6rjee9HpGVP-IsKfGBAiqU8Y6FHuuBN25odKZExig7liOhJd2lq_eUn8JUOtz7QpRX5RvGYzbzlxdRED0boNVA",
  "expiresAt": 1586993686,
  "signedInWith": "google",
  "user": {
    "id": "5e47a9600cac851790f29662",
    "username": "user",
    "email": "user@tdev.app",
    "status": "active",
    "firstName": "User",
    "lastName": "Account",
    "role": "user",
    "permissions": {
      "userInsert": false,
      "userModify": false,
      "userRead": false,
      "postInsert": false,
      "postModify": false,
      "postRead": true
    },
    "provider": {
      "google": {
        "userId": "114383861774342288272",
        "picture": "picture-url.jpg"
      }
    },
    "createdAt": "2020-02-15T08:18:40.429Z",
    "updatedAt": "2020-02-15T08:18:40.429Z"
  }
}

Send Verification Email

  • Method: POST
  • Content-Type: application/json
  • Endpoint
/api/auth/send-token
  • Resquest Body Payload
Property Name Type Required Description
email string Yes Email
tokenPurpose string Yes Token purpose. It must be verify-email

Sample request body payload

{
  "email": "user@tdev.app",
  "tokenPurpose": "verify-email",
}
  • Response Payload
Property Name Type Description
message string Message

Sample response

{
  "message": "A verification email has been sent to your email"
}

Verify Email Code

  • Method: POST
  • Content-Type: application/json
  • Endpoint
/api/auth/verify-email/:token
  • Response Payload
Property Name Type Description
message string Message

Sample response

{
  "message": "Email verified"
}

Send Password Reset Email

  • Method: POST
  • Content-Type: application/json
  • Endpoint
/api/auth/send-token
  • Resquest Body Payload
Property Name Type Required Description
email string Yes Email
tokenPurpose string Yes Token purpose. It must be reset-password

Sample request body payload

{
  "email": "user@tdev.app",
  "tokenPurpose": "reset-password",
}
  • Response Payload
Property Name Type Description
message string Message

Sample response

{
  {
    "message": "A password-reset email has been sent to your email"
  }
}

Confirm Password Reset

  • Method: POST
  • Content-Type: application/json
  • Endpoint
/api/auth/reset-password/:token
  • Resquest Body Payload
Property Name Type Required Description
email string Yes Email
password string Yes New password

Sample request body payload

{
  "email": "user@tdev.app",
  "password: "new-password"
}
  • Response Payload
Property Name Type Description
message string Message

Sample response

{
  "message": "Password reset"
}

Verify JWT Token

  • Method: POST
  • Content-Type: application/json
  • Authentication Header
Authorization: Bearer {JWT Token}
  • Endpoint
/api/auth/verify-token
  • Resquest Body Payload
Property Name Type Required Description
refreshToken boolean No If true, a new JWT token will be included in the response

Sample request body payload

{
  "refreshToken": true,
}
  • Response payload
Property Name Type Description
status string The verification status
token string New JWT token
expiresAt number Expires at time (seconds)

Sample response

{
  "status": "pass",
  "token": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1ZTI3ZTQ4OTY2OGEyYjQxZWUxNmY3NDIiLCJ1c2VySWQiOiI1ZTI0ZjhkM2M1ZGZmZjFmYzk1NDQ3ZDUiLCJpYXQiOjE1Nzk2NzY1OTgsImV4cCI6MTU4NDg2MDU5OH0.7DjINccJtzowF0Nf2DnMoBtKpWEzRKLqcpzzIByHuwnqXRHKduYHGfOgf1ak9t2qLHQzPwMw-FxOGtZGVvAucA",
  "expiresAt": 1584860598,
}

Profile

Get User Profile

  • Method: GET
  • Content-Type: application/json
  • Authentication Header
Authorization: Bearer {JWT Token}
  • Endpoint
/api/profiles
  • Response payload
Property Name Type Description
profile object User profile JSON object

Sample response

{
  "profile": {
    "id": "5e47a9600cac851790f29662",
    "username": "user",
    "email": "user@tdev.app",
    "status": "active",
    "firstName": "User",
    "lastName": "Account",
    "role": "user",
    "permissions": {
      "userInsert": false,
      "userModify": false,
      "userRead": false,
      "postInsert": false,
      "postModify": false,
      "postRead": true
    },
    "provider": {
      "local": {
        "userId": "5e47a9600cac851790f29662"
      }
    },
    "createdAt": "2020-02-15T08:18:40.429Z",
    "updatedAt": "2020-02-15T08:18:40.429Z"
  }
}

Get User Public Profile

  • Method: GET
  • Content-Type: application/json

  • Endpoint
/api/profiles/:userId
  • Response payload
Property Name Type Description
profile object User profile JSON object

Sample response

{
  "profile": {
    "id": "5e24d42cf7dddf012cd496b2",
    "username": "sarah",
    "firstName": "Sarah",
    "lastName": "Connor",
    "createdAt": "2020-01-19T22:11:56.779Z"
  }
}

Update User Profile

  • Method: PUT
  • Content-Type: application/json
  • Authentication Header
Authorization: Bearer {JWT Token}
  • Endpoint
/api/profiles
  • Resquest Body Payload
Property Name Type Required Description
password string No New password
firstName string No New first name
lastName string No New last name

Sample request body payload

{
  "password: "new-password",
  "firstName: "Sarah"
}
  • Response payload
Property Name Type Description
updatedFields string array Fields has been updated

Sample response

{
  "updatedFields": [
    "password",
    "firstName"
  ]
}

Users

Get Users

  • Method: GET
  • Content-Type: application/json
  • Authentication Header
Authorization: Bearer {JWT Token}
  • Endpoint
/api/users
  • Resquest Query
Property Name Type Required Description
limit number No Limit number (Default: 30)
skip number No Offset number (Default: 0)
sort string No Sort criteria (example: “createdAt” or “-createdAt”)
username string No Username
email string No Email
firstName string No First name
lastName string No Last name
status string No Status. It could be “active”, “disabled” or “unverified-email”]
role string No User role. It could be “root”, “admin” or “user”
permissions string No User permissions. (example: ‘readPosts’ or ‘editPosts’).

Note: When permissions is specified (without role), it will include admin and root users in the response.

  • Response Payload
Property Name Type Description
users object array A list of users
usersCount number A total number of users

Sample response

{
  "users": [
    {
      "id": "5e24d42cf7dddf012cd496b2",
      "username": "tester",
      "email": "test@test.com",
      "status": "active",
      "firstName": "Sarah",
      "lastName": "Connor",
      "role": "root",
      "permissions": {
        "userInsert": false,
        "userModify": false,
        "userRead": false,
        "postInsert": false,
        "postModify": false,
        "postRead": true
      },
      "provider": {
        "local": {
          "userId": "5e24d42cf7dddf012cd496b2"
        }
      },
      "createdAt": "2020-01-19T22:11:56.779Z",
      "updatedAt": "2020-01-19T23:18:47.897Z"
    },
    {
      "id": "5e24db1d560ba309f0b0b5a8",
      "username": "tester2",
      "email": "test2@test.com",
      "status": "active",
      "firstName": "John",
      "lastName": "Connor",
      "role": "user",
      "permissions": {
        "userInsert": false,
        "userModify": false,
        "userRead": false,
        "postInsert": false,
        "postModify": false,
        "postRead": true
      },
      "provider": {
        "local": {
          "userId": "5e24db1d560ba309f0b0b5a8"
        }
      },
      "createdAt": "2020-01-20T20:44:44.634Z",
      "updatedAt": "2020-01-22T01:28:03.783Z"
    }
  ],
  "usersCount": 2
}

Get User

  • Method: GET
  • Content-Type: application/json
  • Authentication Header
Authorization: Bearer {JWT Token}
  • Endpoint
/api/users/:userId
  • Response Payload
Property Name Type Description
user object User info

Sample response

{
  "user": {
    "id": "5e24d42cf7dddf012cd496b2",
    "username": "tester",
    "email": "test@test.com",
    "status": "active",
    "firstName": "Sarah",
    "lastName": "Connor",
    "role": "root",
    "permissions": {
      "userInsert": false,
      "userModify": false,
      "userRead": false,
      "postInsert": false,
      "postModify": false,
      "postRead": true
    },
    "provider": {
      "local": {
        "userId": "5e24d42cf7dddf012cd496b2"
      }
    },
    "createdAt": "2020-01-19T22:11:56.779Z",
    "updatedAt": "2020-01-19T23:18:47.897Z"
  }
}

Update User

  • Method: PUT
  • Content-Type: application/json
  • Authentication Header
Authorization: Bearer {JWT Token}
  • Endpoint
/api/users/:userId
  • Resquest Body Payload
Property Name Type Required Description
role string No New role. It could be “admin” or “user”. NOTE: Only root users can update user role.
status string No New status. It could be “active”, “disabled” or “unverified-email”
permissions object No Permissions object

Sample request body payload

{
  "role": "admin",
  "permissions": {
    "userInsert": true,
    "userModify": true,
    "userRead": true,
    "postInsert": false,
    "postModify": false,
    "postRead": true
  }
}
  • Response Payload
Property Name Type Description
updatedFields string array Fields has been updated

Sample response

{
  "updatedFields": [
    "role",
    "permissions"
  ]
}

Delete User

  • Method: DELETE
  • Content-Type: application/json
  • Authentication Header
Authorization: Bearer {JWT Token}
  • Endpoint
/api/users/:userId
  • Response Payload
Property Name Type Description
message string Message

Sample response

{
  "message": "User deleted."
}