controllers/static/user.js
/**
   * @method handleRequest
   * @description handle http request for static spa route.  Lookup user based on
   * JWT token, look up nice name if one is not provided in token.  Impersonate
   * user if admin and impersonate flag set. Returns user object
   * 
   * @param {Request} req http request
   * 
   * @returns {Object}
   */
@method handleRequest
@description handle http request for static spa route.  Lookup user based on
JWT token, look up nice name if one is not provided in token.  Impersonate
user if admin and impersonate flag set. Returns user object

@param {Request} req http request

@returns {Object}
@method handleRequest @description handle http request for static spa route . Lookup user based on JWT token look up nice name if one is not provided in token . Impersonate user if admin and impersonate flag set . Returns user object @param { Request } req http request @returns { Object }
async handleRequest(req) {
    let user = null;
    let token = auth.getTokenFromRequest(req);
    if( token ) {
      try {
        user = await auth.verifyToken(token);
        try {
          user.expertsId = await this.getExpertsId(user.uid || (user.username || '').split('@')[0] );
        } catch(e) {}
        let authProps = await redis.client.get(config.redis.prefixes.authProperties+user.username);
        if( authProps ) {
          authProps = JSON.parse(authProps);
          config.server.userAuthProps.forEach(prop => user[prop] = authProps[prop]);
        }
      } catch(e) {
        logger.error('error parsing jwt token: ', e);
      }
    }
    if( user && req.cookies.impersonate && (user.roles || []).includes('admin') ) {
      logger.info(`user ${user.username} is impersonating: ${req.cookies.impersonate}`);
      user = {
        impersonatedBy : user,
        roles : [],
        username : '',
        expertsId : req.cookies.impersonate
      };
      let authProps = await redis.client.get(config.redis.prefixes.authProperties+user.username);
      if( authProps ) {
        authProps = JSON.parse(authProps);
        config.server.userAuthProps.forEach(prop => user[prop] = authProps[prop]);
      }
    }
    if( user && !user.displayname ) {
      let record = await this.getExpertRecord(user.expertsId);
      user.displayname = record.label || '';
    }
    return user;
  }
async handleRequest ( req ) { let user = null ; let token = auth . getTokenFromRequest ( req ) ; if ( token ) { try { user = await auth . verifyToken ( token ) ; try { user . expertsId = await this . getExpertsId ( user . uid || ( user . username || '' ) . split ( '@' ) [ 0 ] ) ; } catch ( e ) { } let authProps = await redis . client . get ( config . redis . prefixes . authProperties + user . username ) ; if ( authProps ) { authProps = JSON . parse ( authProps ) ; config . server . userAuthProps . forEach ( prop => user [ prop ] = authProps [ prop ] ) ; } } catch ( e ) { logger . error ( 'error parsing jwt token: ' , e ) ; } } if ( user && req . cookies . impersonate && ( user . roles || [ ] ) . includes ( 'admin' ) ) { logger . info ( ` ${ user . username } ${ req . cookies . impersonate } ` ) ; user = { impersonatedBy : user , roles : [ ] , username : '' , expertsId : req . cookies . impersonate } ; let authProps = await redis . client . get ( config . redis . prefixes . authProperties + user . username ) ; if ( authProps ) { authProps = JSON . parse ( authProps ) ; config . server . userAuthProps . forEach ( prop => user [ prop ] = authProps [ prop ] ) ; } } if ( user && ! user . displayname ) { let record = await this . getExpertRecord ( user . expertsId ) ; user . displayname = record . label || '' ; } return user ; }
@method handleRequest
 @description handle http request for static spa route.
@method handleRequest @description handle http request for static spa route .
// grab current user // set has profile flag // fetch additional user properties // check for admin impersonation // fetch additional user properties // try to get a nice name from experts record if we don't have one from some reason