import { Application , Request } from "express" ; import dayjs from "dayjs" ; import { IJWTToken } from "../../../cshub-shared/src/models" ; import { sign , validateAccessToken } from "./JWTHandler" ; import { Settings } from "../settings" ; import { logMiddleware } from "../utilities/LoggingMiddleware" ; export function addAuthMiddleware ( app ) { app . use ( ( req , res , next ) => { const tokenValidity = checkTokenValidityFromRequest ( req ) ; if ( tokenValidity ) { const newtoken = sign ( tokenValidity . user ) ; res . cookie ( "token" , newtoken , { maxAge : Settings . TOKENAGEMILLISECONDS , domain : Settings . DOMAIN , } ) ; logMiddleware ( req , tokenValidity ) ; } else { logMiddleware ( req , null ) ; res . clearCookie ( "token" ) ; } next ( ) ; } ) ; } export type ValidationType = false | IJWTToken ; export const checkTokenValidityFromJWT = ( jwt ) : ValidationType => { if ( ! jwt ) { return false ; } const tokenObj = validateAccessToken ( jwt ) ; if ( tokenObj !== undefined && dayjs ( tokenObj . expirydate * 1000 ) . isAfter ( dayjs ( ) ) && tokenObj . user . verified && ! tokenObj . user . blocked ) { return tokenObj ; } else { return false ; } } ; export const checkTokenValidityFromRequest = ( req ) : ValidationType => { if ( req . cookies === null ) { return false ; } const cookie = req . cookies [ "token" ] ; if ( cookie === undefined ) { return false ; } return checkTokenValidityFromJWT ( cookie ) ; } ;