Files
nirvana-v3/packages/api/middleware/auth.ts
T
2022-05-22 05:35:03 -05:00

36 lines
808 B
TypeScript

import { NextFunction, Request, Response } from 'express';
import { loadConfig } from '../config';
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) => {
try {
const { authentication } = req.headers;
if (!authentication) {
throw Error('No provided header');
}
// verify jwt token with our api secret
var decoded: JwtClaims = jwt.verify(authentication, config.JWT_TOKEN_SECRET);
res.locals.userInfo = decoded;
next();
} catch (error) {
res.status(401).send('unauthorized');
}
};
export interface JwtClaims {
userId: string;
googleUserId: string;
picture: string;
email: string;
name: string;
}