Skip to content

bariyer/express-bearer-auth-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@bariyer/express
bearer authentication example

bariyer sub brand express logo

Report Bug  •  Request Feature

Installation

1. Clone the repository

git clone https://github.com/bariyer/express-bearer-auth-example.git

2. Install dependencies

npm i
yarn

3. Rename .env.example to .env and fill the values

mv .env.example .env

Example:

# –––------------ JSON Web Token  –––------------
# Your JWT Secret Key. This key is unique to you. Please don't share with anyone.
# Its could be anything. Ex. example_secret_key
JWT_SECRET_KEY=9a20452f-dcc1-5b2f-95b0-8a6d167e178a
# format being https://github.com/vercel/ms
JWT_EXPIRE=1d
# format being https://github.com/vercel/ms
JWT_COOKIE_EXPIRE=2d

3. Run the example server

yarn start
npm start

4. Open Postman or similar tool to test the API

For Postman users

  1. Click import button
  2. Select Link tab
  3. Paste this link https://www.getpostman.com/collections/db123cb580f6e775853e
  4. Run the bearer auth requests

For non-Postman users

Method Endpoint Headers Body
GET http://localhost:3001/ Authorization: Bearer <access_token> {}

Response Code: 200 OK Response Body:

[
    {
        "id": 1,
        "name": "Milk"
    },
    {
        "id": 2,
        "name": "Cheese"
    }
]



Method Endpoint Headers Body
POST http://localhost:3001/login Authorization: Bearer <access_token> x-www-form-urlencoded {username: john, password: 123456}

user is registered

Response Code: 200 OK Response Body:

{
  "access_token": "<your_access_token>"
}

user is not registered or wrong username or password

Response Code: 401 Unauthorized Response Body:

{
    "message": "username or password incorrect"
}



Method Endpoint Headers Body
POST http://localhost:3001/register Authorization: Bearer <access_token> x-www-form-urlencoded {username: john, password: 1234567}

user not registered

Response Code: 200 OK Response Body:

{
    "id": "eka031pkt6e9p43nzv50yntp",
    "username": "john",
    "password": "$2b$10$.jLwU8c0v8ICv16.zCFhce1A9rggQOsBlENjBuH0VxUKeay3q9xBy"
}

user already registered

Response Code: 401 Unauthorized Response Body:

{
    "message": "user already registered, go to login"
}

Contributing

Contributions are what make the open source community such an amazing place to be inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feat/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feat/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

About

@bariyer/express bearer authentication example

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published