wiki/server/models/users.js
/**
   * Generate a new token for a user
   */
Generate a new token for a user
Generate a new token for a user
static async refreshToken(user) {
    if (_.isSafeInteger(user)) {
      user = await WIKI.models.users.query().findById(user).withGraphFetched('groups').modifyGraph('groups', builder => {
        builder.select('groups.id', 'permissions')
      })
      if (!user) {
        WIKI.logger.warn(`Failed to refresh token for user ${user}: Not found.`)
        throw new WIKI.Error.AuthGenericError()
      }
      if (!user.isActive) {
        WIKI.logger.warn(`Failed to refresh token for user ${user}: Inactive.`)
        throw new WIKI.Error.AuthAccountBanned()
      }
    } else if (_.isNil(user.groups)) {
      user.groups = await user.$relatedQuery('groups').select('groups.id', 'permissions')
    }
    await WIKI.models.knex('users').where('id', user.id).update({ lastLoginAt: new Date().toISOString() })
    return {
      token: jwt.sign({
        id: user.id,
        email: user.email,
        name: user.name,
        av: user.pictureUrl,
        tz: user.timezone,
        lc: user.localeCode,
        df: user.dateFormat,
        ap: user.appearance,
        permissions: user.getGlobalPermissions(),
        groups: user.getGroups()
      }, {
        key: WIKI.config.certs.private,
        passphrase: WIKI.config.sessionSecret
      }, {
        algorithm: 'RS256',
        expiresIn: WIKI.config.auth.tokenExpiration,
        audience: WIKI.config.auth.audience,
        issuer: 'urn:wiki.js'
      }),
      user
    }
  }
static async refreshToken ( user ) { if ( _ . isSafeInteger ( user ) ) { user = await WIKI . models . users . query ( ) . findById ( user ) . withGraphFetched ( 'groups' ) . modifyGraph ( 'groups' , builder => { builder . select ( 'groups.id' , 'permissions' ) } ) if ( ! user ) { WIKI . logger . warn ( ` ${ user } ` ) throw new WIKI . Error . AuthGenericError ( ) } if ( ! user . isActive ) { WIKI . logger . warn ( ` ${ user } ` ) throw new WIKI . Error . AuthAccountBanned ( ) } } else if ( _ . isNil ( user . groups ) ) { user . groups = await user . $relatedQuery ( 'groups' ) . select ( 'groups.id' , 'permissions' ) } await WIKI . models . knex ( 'users' ) . where ( 'id' , user . id ) . update ( { lastLoginAt : new Date ( ) . toISOString ( ) } ) return { token : jwt . sign ( { id : user . id , email : user . email , name : user . name , av : user . pictureUrl , tz : user . timezone , lc : user . localeCode , df : user . dateFormat , ap : user . appearance , permissions : user . getGlobalPermissions ( ) , groups : user . getGroups ( ) } , { key : WIKI . config . certs . private , passphrase : WIKI . config . sessionSecret } , { algorithm : 'RS256' , expiresIn : WIKI . config . auth . tokenExpiration , audience : WIKI . config . auth . audience , issuer : 'urn:wiki.js' } ) , user } }
Generate a new token for a user
Generate a new token for a user
// Update Last Login Date // -> Bypass Objection.js to avoid updating the updatedAt field // defaultEditor: user.defaultEditor,