Last active
June 1, 2021 15:59
-
-
Save benawad/f553c5a1719bf3cf479cd28005ccc09c to your computer and use it in GitHub Desktop.
middleware role
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
export const hasRole: (role: string) => MiddlewareFn<MyContext> = (role: string) => async ({ context }, next) => { | |
if (!context.req.session!.userId) { | |
throw new Error("not authenticated"); | |
} | |
const user = await User.findOne(context.req.session!.userId); | |
if (!user) { | |
throw new Error('not authenticated') | |
} | |
if (user.role !== role) { | |
throw new Error('not authorized'); | |
} | |
return next(); | |
}; |
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
@UseMiddleware(hasRole("admin")) | |
@Query(() => String) | |
async hello() { | |
return "Hello World!"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment