/**
 * Handles auth actions on the API.
 */ import * as bcrypt from 'bcryptjs' ; import * as jwt from 'jsonwebtoken' ; import { User } from '@prisma/client' ; param password export function hashPassword ( password ) { return bcrypt . hashSync ( password , 10 ) ; } param password param hashedPassword export function comparePasswords ( password , hashedPassword ) { return bcrypt . compareSync ( password , hashedPassword ) ; } const getAppSecret = ( ) : string => { const appSecret = process . env . APP_SECRET ; if ( ! appSecret ) { throw new Error ( 'APP_SECRET is not set' ) ; } return appSecret ; } ; param user export const appJwtForUser = ( user ) : string => { return jwt . sign ( { userId : user . id } , getAppSecret ( ) ) ; } ; param header export const verifyAuthHeader = ( header ? ) : JWT | undefined => { if ( ! header ) return ; const token = header . replace ( 'Bearer ' , '' ) ; try { return jwt . verify ( token , getAppSecret ( ) ) as JWT ; } catch ( e ) { return ; } } ; interface JWT { userId : string ; }