nice auth flow working with verification and stuff with token
This commit is contained in:
@@ -2,6 +2,8 @@ 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
|
||||
@@ -13,16 +15,26 @@ export const authCheck = async (
|
||||
try {
|
||||
const { authorization } = req.headers;
|
||||
|
||||
// verify jwt token
|
||||
const jwtSecret = config.JWT_TOKEN_SECRET;
|
||||
|
||||
if (!authorization) {
|
||||
throw Error("No provided header");
|
||||
}
|
||||
|
||||
// verify jwt token with our api secret
|
||||
var decoded: JwtClaims = jwt.verify(authorization, config.JWT_TOKEN_SECRET);
|
||||
|
||||
res.locals.jwtClaims = decoded;
|
||||
|
||||
next();
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(401).send("unauthorized");
|
||||
}
|
||||
};
|
||||
|
||||
export interface JwtClaims {
|
||||
userId: string;
|
||||
googleUserId: string;
|
||||
picture: string;
|
||||
email: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user