GithubHelp home page GithubHelp logo

animal-service-auth's Introduction

Animal-Service-Auth

A auth lib provides service to service authentication.

Install

Step 0: Create Github access token

Create Github Access Token

Step 1: Config .npmrc file

Create .npmrc file on root directory, and use your Github access token

@animal-high-tech:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken=YOUR-GITHUB-ACCESS-TOKEN

Step 2: Install the library to your project

Install from the command line:
    npm install @animal-high-tech/animal-service-auth

OR

Install via package.json:
    "@animal-high-tech/animal-service-auth": "^1.1.0"

Usage

Key Generator

Typescript Example:

import { AuthKeyGen } from '@animal-high-tech/animal-service-auth';

const apiSecret = 'animal';
const authServices = ['animal-apollo'];

const keyGenerator = new AuthKeyGen(apiSecret,authServices);

console.log(keyGenerator.generateKeys());

/*
    Map(1) {
        'animal-apollo' => 'eyJhbGciOiJIUzI1NiJ9.bW92bi1hcG9sbG8.zqMTlPEBzJkPLr-fXQEuSXokiiZTyexGPQWLenh3qLk'
    }
*/

Use the generated token as a bearer token.

Auth Checker

Step 1: Create a serviceAuth.ts file


import { AuthChecker } from '@animal-high-tech/animal-service-auth';
const apiSecret = 'animal';
const authServices = ['animal-apollo'];
const AuthCheckerMiddleware = new AuthChecker(apiSecret,authServices).build();

export default AuthCheckerMiddleware;

Step 2: Use the AuthCheckerMiddleware to verify incoming requests.

Typescript Example:

import express, { Express, Request, Response } from 'express';

const app: Express = express();

import AuthCheckerMiddleware from './serviceAuth';
app.use(AuthCheckerMiddleware);
app.post('/test',async(req: Request, res: Response, next: any)=>{
    res.json({world:"hello",...req.body});
});

Or

import express, { Express, Request, Response } from 'express';

const app: Express = express();

import AuthCheckerMiddleware from './serviceAuth';

app.post('/test', AuthCheckerMiddleware, async(req: Request, res: Response, next: any)=>{
    res.json({world:"hello",...req.body});
});

animal-service-auth's People

Contributors

chrisyao4700 avatar

Watchers

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