Skip to content

Instantly share code, notes, and snippets.

@vinodchauhan7
Created November 10, 2019 13:13
Show Gist options
  • Save vinodchauhan7/288b30db716ddd10739dbdf2b8b09fdc to your computer and use it in GitHub Desktop.
Save vinodchauhan7/288b30db716ddd10739dbdf2b8b09fdc to your computer and use it in GitHub Desktop.
import { MiddlewareFn } from "type-graphql";
import { verify } from "jsonwebtoken";
import { MyContext } from "./MyContext";
//format like bearer 21321n2bmbbj
export const isAuth: MiddlewareFn<MyContext> = ({ context }, next) => {
const authorization = context.req.headers["authorization"];
if (!authorization) {
throw new Error("Not authenticated");
}
try {
const token = authorization.split(" ")[1];
const payload = verify(token, "MySecretKey");
console.log(payload);
context.payload = payload as any;
} catch (err) {
console.log(err);
throw new Error("Not authenticated");
}
return next();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment