项目作者: satheeshnatarajan

项目描述 :
Node OAuth2-Server with JWT. Boilerplate application to start project from scratch
高级语言: JavaScript
项目地址: git://github.com/satheeshnatarajan/node-oauth2-jwt-example.git


Node Project Setup

  • Node boilerplate code
  • Configured OAuth2 login with JWT
  • Eslint, Prettier, Custom logger has configured for best practice of coding

1) Prerequisites

Please install the following software:
1) Node JS (v 10+)
2) NPM (v 6+)

2) Project Setup

Install the dependencies

  1. npm install

3) Before start the project

  • Config required environmental variables in .env file
  • Used mysql in this project. Change dialect whatever you want which sequelize support database in confighook.js file
  • Using sequelize-cli migration, will create database, tables and insert default records
  • Create database by npm run db:create
  • Once DB created, create users table. Run npm run db:migrate
  • Insert few records into users. Run npm run db:seed:all

3) Run the Project

  1. npm run start

Use cURL / Postman to validate Authorization and Authentication API
NOTE: Basic token should be replaced by btoa(clientId:clientSecret) which configure in environment variables

  • Login API

    curl —location —request POST ‘http://localhost:5000/auth/login‘ \
    —header ‘Authorization: Basic clientId:clientSecret‘ \
    —header ‘Content-Type: application/x-www-form-urlencoded’ \
    —data-urlencode ‘grant_type=password’ \
    —data-urlencode ‘username=admin’ \
    —data-urlencode ‘password=admin’

  • Get All Users Secure API (Replace accesstoken from login api response)

    curl —location —request GET ‘http://localhost:5000/api/v1/users‘ \
    —header ‘Authorization: Bearer accesstoken

4) Logger

The Project includes a custom logger for development and error tracking, please use in the code the most appropriate log level
based on your use case. Custom logger and log level for application and database queries.

  1. logger.debug('information for development ');
  2. logger.log('information for development - production');
  3. logger.info('useful information at runtime');
  4. logger.warn('something unusual happened but we can manage');
  5. logger.error('something broke and we will notify the server or do some special handling');

As part of the log message you can also send data that can reviewed in the Browser Console, where you can also filter by log levels:

  1. logger.debug('information for development: ', item.price);
  2. logger.warn('Service Error: ', response.error);

NOTE: logger available in global. No need to import.

5) Lint - Code Style Guideline

Using Eslint to establish a code style guideline that we should follow, in order to merge the code we should comply with the
linter and do not include lint errors as part of the code.

  1. npm run elint

This command will execute the lint process and provide feedback on warnings and errors.

  1. npm run elint:fix

6) Prettier - Code Format Style Guideline

Using Prettier to establish a code formatting guideline that we should follow.

  1. npm run prettier

This command will execute the lint process and provide feedback on warnings and errors.

  1. npm run prettier:fix

This command will execute the formatting process and will automatically fix all the errors that could be fixed by not introducing a side effect, it will do its best
to save some time to fix the most common formatting errors.

Third party libraries Documentation