starting implementation

This commit is contained in:
talksik
2022-05-22 05:35:03 -05:00
parent f98026a3a5
commit 29cc445092
5 changed files with 95 additions and 57 deletions
+9 -13
View File
@@ -1,32 +1,28 @@
import { NextFunction, Request, Response } from "express";
import { NextFunction, Request, Response } from 'express';
import { loadConfig } from "../config";
import { loadConfig } from '../config';
const jwt = require("jsonwebtoken");
const jwt = require('jsonwebtoken');
const config = loadConfig();
// used by specific routes that need to authentication
export const authCheck = async (
req: Request,
res: Response,
next: NextFunction
) => {
export const authCheck = async (req: Request, res: Response, next: NextFunction) => {
try {
const { authorization } = req.headers;
const { authentication } = req.headers;
if (!authorization) {
throw Error("No provided header");
if (!authentication) {
throw Error('No provided header');
}
// verify jwt token with our api secret
var decoded: JwtClaims = jwt.verify(authorization, config.JWT_TOKEN_SECRET);
var decoded: JwtClaims = jwt.verify(authentication, config.JWT_TOKEN_SECRET);
res.locals.userInfo = decoded;
next();
} catch (error) {
res.status(401).send("unauthorized");
res.status(401).send('unauthorized');
}
};