All files / middlewares loginRequired.js

6.9% Statements 2/29
0% Branches 0/30
50% Functions 1/2
6.9% Lines 2/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 4415x 15x                                                                                    
module.exports = (crowi, app) => {
  return async (req, res, next) => {
    const User = crowi.model('User')
    const config = crowi.getConfig()
    const { path = '', originalUrl } = req
    const auth = require('../util/auth')
    const query = originalUrl === '/' ? '' : `?continue=${originalUrl}`
    const isAuthPage = path.startsWith('/me/auth/')
    const isAPI = path.startsWith('/_api/')
 
    if (!isAuthPage && auth.isAccessTokenExpired(req)) {
      const success = await auth.reauth(req, config)
      if (!success) {
        res.redirect('/logout')
      }
    }
 
    if (req.user && '_id' in req.user) {
      const { 'auth:requireThirdPartyAuth': requireThirdPartyAuth = '' } = config.crowi
      const hasValidThirdPartyId = req.user.hasValidThirdPartyId()
      if (!isAuthPage && !isAPI && requireThirdPartyAuth && !hasValidThirdPartyId) {
        return res.redirect(`/me/auth/third-party${query}`)
      }
 
      if (req.user.status === User.STATUS_ACTIVE) {
        // Active の人だけ先に進める
        return next()
      } else if (req.user.status === User.STATUS_REGISTERED) {
        return res.redirect('/login/error/registered')
      } else if (req.user.status === User.STATUS_SUSPENDED) {
        return res.redirect('/login/error/suspended')
      } else if (req.user.status === User.STATUS_INVITED) {
        return res.redirect('/login/invited')
      }
    }
 
    if (isAPI) {
      return res.sendStatus(403)
    }
 
    return res.redirect(`/login${query}`)
  }
}