Giter Site home page Giter Site logo

juffalow / express-jwt-example Goto Github PK

View Code? Open in Web Editor NEW
32.0 3.0 20.0 54 KB

Example project for Express.js with JWT blogpost

Home Page: https://juffalow.com/javascript/express-server-with-jwt-authentication

JavaScript 100.00%
nodejs jwt mocha sinon chai expressjs

express-jwt-example's Introduction

Express.js with JWT example

This is an example project to show how to create login route and some other route that is protected - could be called only if valid JWT token is provided.

Everything is well tested with mocha, chai, sinon and supertest.

Dependencies

  • express
  • body-parser
  • jsonwebtoken
  • chai
  • mocha
  • sinon
  • supertest

Scripts

npm run start

npm run test

How to run the project

Install dependencies :

yarn install

# or

npm install

Edit config.example.js and save it as config.js :

module.exports = {
    port: 8080,
    jwtSecret: 'your jwt secret'
};

Run tests :

npm run test

If everything is OK, run the project :

npm start

How to test the project

When you run the project, you should be able to load the URL http://localhost:8080/, but you shouldn't be able to access http://localhost:8080/api/hello-world.

You can log in by sending a post on http://localhost:8080/login and send there username and password, both set to admin.

curl -XPOST -H "Content-Type: application/json" 'http://localhost:8080/login' -d '{"username":"admin","password":"admin"}'

You should get back something like :

{
"id":1,
"username":"admin",
"jwt":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNDg3NjM3OTg0LCJleHAiOjE0ODc2NDE1ODR9.1jMwROveQeR64baJOPdZV4SdpmKKVRvgPg0wJX9sHnI"
}

Now, when you want to load http://localhost:8080/api/hello-world and you send there Authorization header with jwt token from the previous response, you should be successful :

curl -XGET -H 'Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNDg3NjM3OTg0LCJleHAiOjE0ODc2NDE1ODR9.1jMwROveQeR64baJOPdZV4SdpmKKVRvgPg0wJX9sHnI' 'http://localhost:8080/api/hello-world'

express-jwt-example's People

Contributors

juffalow avatar yassineelouafi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.