Handling user authorization and authentication
We have now prepared our schema to use standard username-password authentication together with JWT authorization, using pipes. We will now implement authentication resolvers and authorization mechanisms.
To begin, create a file called src/auth.ts
– this is where we will keep all the functions connected to authentication and authorization.
Then, we will create some helper functions to keep our authorization mechanisms secure. To do so, we need to store the hashed password inside the database:
import crypto from 'node:crypto'; import jwt from "jsonwebtoken"; import { createResolvers } from '@/src/axolotl.js'; import { MongOrb } from '@/src/orm.js'; import { GraphQLError } from 'graphql'; const secretKey = 'your-secret-key'; const passwordSha512 = (password: string, salt: string) => { const hash = crypto.createHmac('sha512', salt); ...