Skip to content
Paul Cham edited this page Sep 13, 2019 · 20 revisions

image This example comes with a complete REST API to handle Authentication and CRUD features on Users and their corresponding Profile. You may view the complete access points in this document.

Routes


Auth

1. Get Authenticated User

Get authenticated user given the token, must provide x-auth-token with the token generated on successful registration and login.

Endpoint:

Method: GET
Type: RAW
URL: http://localhost:5000/api/auth

Headers:

Key Value Description
x-auth-token {{token}} Provide valid token

Responses:

Status: Success Response | Code: 200

{
  "_id": "5cd6831f1acb4f0b691c4ef6",
  "email": "[email protected]",
  "avatar": "//www.gravatar.com/avatar/cb440f309ad5be39a03b7e7c0ba9d4d6?s=200&r=pg&d=mm",
  "date": "2019-05-11T08:09:03.479Z",
  "__v": 0
}

Status: Validation Error | Code: 401

{
  "msg": "Token is not valid"
}

2. Login User

Login user with email and password, returns a token on successful login.

Endpoint:

Method: POST
Type: RAW
URL: http://localhost:5000/api/auth

Headers:

Key Value Description
Content-Type application/json content-type must be application/json

Body:

{
  "email": "[email protected]",
  "password": "password"
}

Responses:

Status: Success Response | Code: 200

{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI1Y2Q2ODMxZjFhY2I0ZjBiNjkxYzRlZjYiLCJpYXQiOjE1NTc1NzExOTEsImV4cCI6MTU1NzkzMTE5MX0.678z6gMvFPLkqjepxahfb_NochizvZbu5neaqUL5LvE"
}

Status: Validation Error | Code: 400

{
  "errors": [
    {
      "location": "body",
      "param": "email",
      "msg": "Please include a valid email"
    },
    {
      "location": "body",
      "param": "password",
      "msg": "Password is required"
    }
  ]
}

Profile

1. Get Current Profile

Get authenticated user's profile given the token, must provide x-auth-token with the token generated on successful registration and login.

Endpoint:

Method: GET
Type: RAW
URL: http://localhost:5000/api/profile/me

Headers:

Key Value Description
x-auth-token {{token}} Provide valid token

Responses:

Status: Success Response | Code: 200

{
  "_id": "5cd6834d1acb4f0b691c4ef7",
  "user": {
    "_id": "5cd6831f1acb4f0b691c4ef6",
    "email": "[email protected]",
    "avatar": "//www.gravatar.com/avatar/cb440f309ad5be39a03b7e7c0ba9d4d6?s=200&r=pg&d=mm"
  },
  "firstName": "John",
  "lastName": "Doe",
  "username": "john.doe",
  "date": "2019-05-11T08:09:49.181Z",
  "__v": 0
}

Status: Validation Error | Code: 401

{
  "msg": "Token is not valid"
}

2. Create Update Profile

Create or update the user's profile, must provide x-auth-token with the token generated on successful registration and login.

Endpoint:

Method: POST
Type: RAW
URL: http://localhost:5000/api/profile

Headers:

Key Value Description
x-auth-token {{token}} Provide valid token
Content-Type application/json content-type must be application/json

Body:

{
  "firstName": "John", 
  "lastName": "Doe", 
  "username": "john.doe"
}

Responses:

Status: Success Response | Code: 200

{
  "_id": "5cd6834d1acb4f0b691c4ef7",
  "user": "5cd6831f1acb4f0b691c4ef6",
  "firstName": "John",
  "lastName": "Doe",
  "username": "john.doe",
  "date": "2019-05-11T08:09:49.181Z",
  "__v": 0
}

Status: Validation Error | Code: 400

{
  "errors": [
    {
      "location": "body",
      "param": "firstName",
      "msg": "First Name is required"
    },
    {
      "location": "body",
      "param": "lastName",
      "msg": "Last Name is required"
    },
    {
      "location": "body",
      "param": "username",
      "msg": "Username is required"
    }
  ]
}

Status: Validation Error | Code: 401

{
  "msg": "Token is not valid"
}

3. Get All Profiles

Get all profiles.

Endpoint:

Method: GET
Type: RAW
URL: http://localhost:5000/api/profile

Responses:

Status: Success Response | Code: 200

[
  {
    "_id": "5cd6476507e48607c355df5b",
    "user": {
      "_id": "5cd6474c07e48607c355df5a",
      "email": "[email protected]",
      "avatar": "//www.gravatar.com/avatar/3e81e650b21be1b3ce771dce1c0c9653?s=200&r=pg&d=mm"
    },
    "firstName": "Paul",
    "lastName": "Cham",
    "username": "pol.cham",
    "date": "2019-05-11T03:54:13.600Z",
    "__v": 0
  },
  {
    "_id": "5cd6834d1acb4f0b691c4ef7",
    "user": {
      "_id": "5cd6831f1acb4f0b691c4ef6",
      "email": "[email protected]",
      "avatar": "//www.gravatar.com/avatar/cb440f309ad5be39a03b7e7c0ba9d4d6?s=200&r=pg&d=mm"
    },
    "firstName": "John",
    "lastName": "Doe",
    "username": "john.doe",
    "date": "2019-05-11T08:09:49.181Z",
    "__v": 0
  }
]

4. Get User's Profile

Get user's profile by their userId.

Endpoint:

Method: GET
Type: RAW
URL: http://localhost:5000/api/profile/user/{{userId}}

Parameters:

Key Value Description
userId 5cd6831f1acb4f0b691c4ef6 userId must be a valid user._id

Responses:

Status: Success Response | Code: 200

{
  "_id": "5cd6834d1acb4f0b691c4ef7",
  "user": {
    "_id": "5cd6831f1acb4f0b691c4ef6",
    "email": "[email protected]",
    "avatar": "//www.gravatar.com/avatar/cb440f309ad5be39a03b7e7c0ba9d4d6?s=200&r=pg&d=mm"
  },
  "firstName": "John",
  "lastName": "Doe",
  "username": "john.doe",
  "date": "2019-05-11T08:09:49.181Z",
  "__v": 0
}

Status: Validation Error | Code: 400

{
  "msg": "Profile not found"
}

5. Delete Profile

Delete current user and their corresponding profile, must provide x-auth-token with the token generated on successful registration and login.

Endpoint:

Method: DELETE
Type: RAW
URL: http://localhost:5000/api/profile

Headers:

Key Value Description
x-auth-token {{token}} Provide valid token

Responses:

Status: Success Response | Code: 200

{ 
  "msg": "User removed"
}

User

1. Register User

Register user given their email and password, returns the token upon successful registration.

Endpoint:

Method: POST
Type: RAW
URL: http://localhost:5000/api/user

Headers:

Key Value Description
Content-Type application/json content-type must be application/json

Body:

{
  "email": "[email protected]",
  "password": "password"
}

Responses:

Status: Success Response | Code: 200

{ 
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI1Y2Q2ODMxZjFhY2I0ZjBiNjkxYzRlZjYiLCJpYXQiOjE1NTc1NjIxNDMsImV4cCI6MTU1NzkyMjE0M30.BP7n27AVY9MKTz1ViHMJWOVqQGMktJmT8AJWrZuQoP0"
}

Status: Validation Error | Code: 400

{
  "msg": "User already exists"
}

Default

1. Test API

Test if the Base API is running.

Endpoint:

Method: GET
Type: RAW
URL: http://localhost:5000/

Status: Success Response | Code: 200

API Running