import { Request , Response , NextFunction } from 'express' ; import { getUserData } from '../jwt' ; export const isConnected = ( isLogged = true , privileges = [ 'user' ] , redirectTo = '/' ) => async ( req , res , next ) : Promise < void > => { const user = await getUserData ( req . cookies . at ) ; if ( ! user && ! isLogged ) { return next ( ) ; } if ( user && isLogged ) { if ( privileges . includes ( 'god' ) && user . privilege === 'god' ) { return next ( ) ; } if ( privileges . includes ( 'admin' ) && user . privilege === 'admin' ) { return next ( ) ; } res . redirect ( redirectTo ) ; } else { res . redirect ( redirectTo ) ; } } ;