rinekri / json-server-jwt

Light JSON API server with JWT authentication

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JSON API server with JWT authentication

A light weight, ready to run json rest api for front-end developers who need a quick back-end for prototyping and mocking .

banner

Build and start server

git clone

cd json-server-jwt

docker build -f .docker/Dockerfile -t json-jwt:api .

docker run -dti --name="json-api" -p "3000:3000" json-jwt:api

The server will be running in port 3000.

To view the server running, open http://localhost:3000

Demo user : admin / admin

Authentication

Request:

curl -XPOST -H "Content-Type: application/json" -d '{"email": "admin@example.com", "password": "admin"}' http://localhost:3000/api-token-auth 

Response:

{
    "auth": true,
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNTMwMjU5MzE3LCJleHAiOjE1MzAzNDU3MTd9.z_mknTvzY_hl4ulxNSEvn6MqHLrlcyALvsyih1oJGyA",
    "user": {
        "id": 1,
        "username": "admin",
        "password": "admin",
        "email": "admin@example.com"
    }
}

Token refresh

Request:

curl -XGET -H "Content-Type: application/json"  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNTMwMjU5MzE3LCJleHAiOjE1MzAzNDU3MTd9.z_mknTvzY_hl4ulxNSEvn6MqHLrlcyALvsyih1oJGyA" http://localhost:3000/api-token-refresh 

Response:

{
    "auth": true,
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNTMwMjU5ODEzLCJleHAiOjE1MzAzNDYyMTN9.vL_WvjzLDT2rCmIWJJh6lQGSyhPfEFuaqQeG6s31zzY",
    "user": {
        "id": 1,
        "username": "admin",
        "password": "admin",
        "email": "admin@example.com"
    }
}

Password reset

Request:

curl -XPOST -H "Content-Type: application/json" -d '{"email": "admin@example.com"}' http://localhost:3000/api-password-reset

Response:

{
    "token": "gCYjYn1Fy2VtflPPM9dY"
}

Password reset verify

Request:

curl -XPOST -H "Content-Type: application/json" -d '{"token": "gCYjYn1Fy2VtflPPM9dY"}' http://localhost:3000/api-password-reset-verify

Response:

OK

Password reset confirm

Request:

curl -XPOST -H "Content-Type: application/json" -d '{"token": "gCYjYn1Fy2VtflPPM9dY", "new_password": "admin123"}' http://localhost:3000/api-password-reset-confirm

Response:

Password reset success

Change password

Request:

curl -XPOST -H "Content-Type: application/json"  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNTMwMjU5MzE3LCJleHAiOjE1MzAzNDU3MTd9.z_mknTvzY_hl4ulxNSEvn6MqHLrlcyALvsyih1oJGyA" -d '{"password": "admin123", "new_password": "admin"}' http://localhost:3000/users/change-password 

Response:

Password changed

Access routes

Request:

curl -XGET -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNTMwMjU5MzE3LCJleHAiOjE1MzAzNDU3MTd9.z_mknTvzY_hl4ulxNSEvn6MqHLrlcyALvsyih1oJGyA" http://localhost:3000/users

Response:

[
  {
    "id": 1,
    "username": "admin",
    "password": "admin",
    "email": "admin@example.com"
  }
]

Source

https://github.com/typicode/json-server

About

Light JSON API server with JWT authentication


Languages

Language:JavaScript 48.8%Language:CSS 41.4%Language:HTML 9.3%Language:Dockerfile 0.5%