A auth lib provides service to service authentication.
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
npm install @animal-high-tech/animal-service-auth
"@animal-high-tech/animal-service-auth": "^1.1.0"
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.
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;
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});
});