https://github.com/scotch-io/node-token-authentication
Code for the scotch.io tutorial: Authenticate a Node API Using JSON Web Tokens
https://github.com/scotch-io/node-token-authentication
Last synced: about 2 months ago
JSON representation
Code for the scotch.io tutorial: Authenticate a Node API Using JSON Web Tokens
- Host: GitHub
- URL: https://github.com/scotch-io/node-token-authentication
- Owner: scotch-io
- Created: 2014-10-23T22:31:38.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2017-12-29T12:33:11.000Z (over 7 years ago)
- Last Synced: 2025-03-29T22:11:18.003Z (about 2 months ago)
- Language: JavaScript
- Homepage: https://scotch.io/tutorials/authenticate-a-node-js-api-with-json-web-tokens
- Size: 8.79 KB
- Stars: 364
- Watchers: 19
- Forks: 199
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Node Token Authentication
This repo uses JSON Web Tokens and the [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken) package to implement token based authentication on a simple Node.js API.
This is a starting point to demonstrate the method of authentication by verifying a token using Express route middleware.
## Requirements
- node and npm
## Usage
1. Clone the repo: `git clone [email protected]:scotch-io/node-token-authentication`
2. Install dependencies: `npm install`
3. Change SECRET in `config.js`
4. Add your own MongoDB database to `config.js`
5. Start the server: `node server.js`
6. Create sample user by visiting: `http://localhost:8080/setup`Once everything is set up, we can begin to use our app by creating and verifying tokens.
### Getting a Token
Send a `POST` request to `http://localhost:8080/api/authenticate` with test user parameters as `x-www-form-urlencoded`.
```
{
name: 'Nick Cerminara',
password: 'password'
}
```### Verifying a Token and Listing Users
Send a `GET` request to `http://localhost:8080/api/users` with a header parameter of `x-access-token` and the token.
You can also send the token as a URL parameter: `http://localhost:8080/api/users?token=YOUR_TOKEN_HERE`
Or you can send the token as a POST parameter of `token`.