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 44 45 46 47 48 | import googleAuth from './googleAuth'
import githubAuth from './githubAuth'
export default {
isLoggedIn(crowi, req) {
const { user = {} } = req
const User = crowi.model('User')
const isLoggedIn = user && '_id' in user && user.status === User.STATUS_ACTIVE
return isLoggedIn
},
isAccessTokenExpired(req) {
const { auth = {} } = req.session
const { expiryDate = null } = auth
if (expiryDate === null) {
return false
}
const now = new Date().getTime()
return expiryDate < now
},
async reauth(req, config) {
const { auth = {} } = req.session
const { provider = '', accessToken = null, refreshToken = null } = auth
const authServices = {
google: googleAuth(config),
github: githubAuth(config),
}
const providers = [authServices.google.PROVIDER, authServices.github.PROVIDER]
if (providers.includes(provider)) {
const authService = authServices[provider]
const serviceId = req.user[provider + 'Id']
const { success, tokens } = await authService.reauth(serviceId, { accessToken, refreshToken })
if (success) {
this.saveTokenToSession(req, provider, tokens)
}
return success
}
return false
},
saveTokenToSession(
req,
provider = '',
{ accessToken = null, refreshToken = null, expiryDate = null }: { accessToken: string | null; refreshToken: string | null; expiryDate: number | null },
) {
expiryDate = expiryDate || new Date().getTime() + 60 * 60 * 1000
req.session.auth = { provider, accessToken, refreshToken, expiryDate }
},
}
|