Created
November 10, 2019 13:13
-
-
Save vinodchauhan7/288b30db716ddd10739dbdf2b8b09fdc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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