import jwt from 'jsonwebtoken' ; import uniq from 'lodash/uniq' ; import { User } from './hasura' ; import * as vars from './vars' ; export const generateJwtAccessToken = ( payload ) => { const jwtOptions = { algorithm : vars . jwtAlgorithm as jwt . Algorithm , expiresIn : vars . jwtTokenExpiresIn , } ; return jwt . sign ( payload , vars . jwtSecretKey , jwtOptions ) ; } ; export const generateJwtRefreshToken = ( payload ) => { const jwtOptions = { algorithm : vars . jwtAlgorithm as jwt . Algorithm , expiresIn : vars . jwtRefreshTokenExpiresIn , } ; return jwt . sign ( payload , vars . jwtSecretKey , jwtOptions ) ; } ; export const generateClaimsJwtToken = ( user , sessionId ) => { const payload = { [ vars . hasuraGraphqlClaimsKey ] : { [ `${ vars . hasuraHeaderPrefix }allowed-roles` ] : uniq ( [ user . default_role , ... user . user_roles . map ( ( { role } ) => role ) , ] ) . filter ( role => ! ! role ) , [ `${ vars . hasuraHeaderPrefix }default-role` ] : user . default_role , [ `${ vars . hasuraHeaderPrefix }session-id` ] : sessionId , [ `${ vars . hasuraHeaderPrefix }user-id` ] : user . id . toString ( ) , [ `${ vars . hasuraHeaderPrefix }organization-id` ] : user . organization_id === null ? '' : user . organization_id , } , } ; return generateJwtAccessToken ( payload ) ; } ;