authChecker.ts 632 B

123456789101112131415161718192021
  1. import { AuthChecker } from 'type-graphql';
  2. import User from '../../modules/auth/user.entity';
  3. import { MyContext } from '../../types';
  4. export const customAuthChecker: AuthChecker<MyContext> = async ({ context }) => {
  5. // here we can read the user from context
  6. // and check his permission in the db against the `roles` argument
  7. // that comes from the `@Authorized` decorator, eg. ["ADMIN", "MODERATOR"]
  8. if (!context.req?.session?.userId) {
  9. return false;
  10. }
  11. const { userId } = context.req.session;
  12. const user = await User.findOne({ where: { id: userId } });
  13. if (!user) {
  14. return false;
  15. }
  16. return true;
  17. };