https://github.com/supermamon/node-restify-jwt-sample
A sample restify api service with JWT authentication
https://github.com/supermamon/node-restify-jwt-sample
api jwt node restify
Last synced: 5 months ago
JSON representation
A sample restify api service with JWT authentication
- Host: GitHub
- URL: https://github.com/supermamon/node-restify-jwt-sample
- Owner: supermamon
- License: mit
- Created: 2017-09-14T15:50:39.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-12-11T00:02:04.000Z (over 1 year ago)
- Last Synced: 2024-12-11T00:29:41.412Z (over 1 year ago)
- Topics: api, jwt, node, restify
- Language: JavaScript
- Size: 1.06 MB
- Stars: 17
- Watchers: 2
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# node-restify-jwt-sample
A sample api service with JWT authentication
## What's Inside
* route path prefix
* versioned routes
* protected/unprotected routes
* registration
* admin/non-admin routes
* logging (using winston)
* unit tests
## Configure
See `/config/index.js`
## Examples
Use `api/register` to generate tokens.
```sh
$ curl -X POST \
> -H "Content-Type: application/json" \
> --data '{ "name": "Johnny Appleseed", "role": "test", "password":"some-hashed-password" }' \
http://localhost:8080/api/register
{"name":"Johnny Appleseed","role":"test","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiSm9obm55IEFwcGxlc2VlZCIsInJvbGUiOiJ0ZXN0IiwiaWF0IjoxNTA5MDc2MTEwfQ.EsRsidT33amgeDX8u6SlE6LwWUs2jpyblogOvLaJ1Y8"}
$ curl -X POST \
> -H "Content-Type: application/json" \
> --data '{ "name": "Tim Cook", "role": "admin", "password":"some-hashed-password" }' \
http://localhost:8080/api/register
{"name":"Tim Cook","role":"admin","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiVGltIENvb2siLCJyb2xlIjoiYWRtaW4iLCJpYXQiOjE1MDkwNzY0NzB9.f5_v9HfOAiOS4IiiQ5Pj0IxLOMJGWUhHQ57Zd9opqwE"}
```
Route protection
```sh
$ curl localhost:8080/api/ping
{"ping":"OK"}
$ curl localhost:8080/api/home
{"code":"InvalidCredentials","message":"No authorization token was found"}
```
Protected route
```sh
$ curl -H "Authorization: Bearer your-token" localhost:8080/api/home
{"welcome":"Johnny Appleseed"}
```
Admin route
```sh
$ curl -H "Authorization: Bearer admin-token" localhost:8080/api/admin
{"action":"completed"}
$ curl -H "Authorization: Bearer user-token" localhost:8080/api/admin
{"code":"Forbidden","message":"You don't have sufficient priviledges."}
```