Code Monkey home page Code Monkey logo

test-express-jwt's Introduction

Testing JWT Auth and Express

Simple app where quotes are protected behind authentication using JWT.

Get Started (Development)

  1. git clone
  2. npm install to install dependencies
  3. npm start to start the server

Then, to get a new JWT, you'll need to login (using Postman or some other tool)

To Login

POST http://localhost:3000/login

  • username: admin
  • password: 54321

Example:

$ curl -X POST --data 'username=admin&password=54321' http://localhost:3000/login

{
  "success":true,
  "token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwicGFzc3dvcmQiOiI1NDMyMSIsImlhdCI6MTQ2Njk5NTk5MywiZXhwIjoxNDY3MDgyMzkzfQ.iPKqdFi3Fg71okTWHxpfKQUc1KvZ36PP3bmfP_Cu50Y"
}

The token is your JWT, which can be used to access the API.

To get quotes

GET http://localhost:3000/quotes

  • Headers
    • Authorization: Bearer YOUR_JWT_GOES_HERE ``

Example:

$ curl -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwicGFzc3dvcmQiOiI1NDMyMSIsImlhdCI6MTQ2Njk5NTk5MywiZXhwIjoxNDY3MDgyMzkzfQ.iPKqdFi3Fg71okTWHxpfKQUc1KvZ36PP3bmfP_Cu50Y" http://localhost:3000/quotes

[
  {"author":"Audrey Hepburn","text":"Nothing is impossible, the word itself says 'I'm possible'!"},
  {"author":"Walt Disney","text":"You may not realize it when it happens, but a kick in the teeth may be the best thing in the world for you"},
  {"author":"Unknown","text":"Even the greatest was once a beginner. Don't be afraid to take that first step."},
  {"author":"Neale Donald Walsch","text":"You are afraid to die, and you're afraid to live. What a way to exist."}
]

Build and Deploy to Production

This comes with a settings.dev.json file, which should only be used in development mode.

You should create a new settings file and secret for production.

  1. cp settings.dev.json settings.production.json
  2. Generate a new random secret:
$ node -e "console.log(require('crypto').randomBytes(32).toString('hex'));"
  1. In settings.production.json, replace jwt.secret with the new secret.
  2. npm run build
  3. NODE_ENV=production npm run serve

test-express-jwt's People

Contributors

jxjj avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.