Skip to content

API Users

Neuco edited this page Feb 15, 2018 · 4 revisions

General comments

Headers

All request must be send with the following headers:

Content-Type: application/json

Some routes are protected with JWT Authentication. To access them, you must obtain a token by login in with a valid user account. More on that, later. Once obtained the token, include it in the following header:

Authorization: Bearer $token

If a token is not passed or invalid, you will get a 401 Unauthorized response.

Dev mode

On dev mode, the default URL is http://localhost:3000/api. To start your local server, you can type npm start in the console. The nodemon included package will execute.

User

Default URL: http://localhost:3000/api/user

GET /

Retrieve a collection of Users.

Attributes

  • name
  • surname
  • email
  • library

Success: 200, Error: 500


GET /{id}

Retrive a single User.

Attributes

  • name
  • surname
  • email
  • library
  • dateOfBirth
  • gender

Success: 200, Error: 500


POST /signup

Create a new user.

Body (example)

{ 
  email: [email protected]
  password: secret
  name: Test
  surname: Test
  dateOfBirth: 2017-02-02
  gender: M or F
}

Response

  • _id
  • email
  • message

Success: 201, Duplicated email: 409, Error: 500


POST /login

Login with an account.

Body (example)

{ 
   email: [email protected]
   password: secret
}

Response

  • token
  • message

Success: 201, Fail auth: 401, Error: 500


PATCH /{id} - PROTECTED

Update some attributes of an User. Available for the time being: name, surname, dateOfBirth.

Body (example)

{
  name: John,
  surname: Doe,
  dateOfBirth: 1993-12-31
}

Response

  • number of attributes modified
  • status
  • message

Success: 200, Error: 500


POST /{id}/add/{bookid} - PROTECTED

Add a book to the Author list of books.

Response

  • message

Success: 201, Error: 500


POST /{id}/remove/{bookid} - PROTECTED

Remove a book from the Author list of books.

Response

  • message

Success: 201, Error: 500


PATCH /change-password/{id} - PROTECTED

Change password of the user, if oldpassword is valid.

Body (example)

{
  oldpassword: secret,
  newpassword: supersecret
}

Response

  • message

Success: 200, Error: 500

If the password is incorrect, you will get a 500 status with a message.


POST delete/{id} - PROTECTED

Delete an user, if the password is valid.

Body (example)

{
  password: secret
}

Response

  • message

Success: 200, Error: 500

If the password is incorrect, you will get a 500 status with a message.