mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.0 KiB
35 lines
1.0 KiB
/* global WIKI */
|
|
import bcrypt from 'bcryptjs'
|
|
|
|
// ------------------------------------
|
|
// Local Account
|
|
// ------------------------------------
|
|
export default class LocalAuthentication {
|
|
constructor(strategyId, conf) {
|
|
this.strategyId = strategyId
|
|
this.conf = conf
|
|
}
|
|
|
|
async authenticate({ username, password }) {
|
|
const user = await WIKI.models.users.getByEmail(username.toLowerCase())
|
|
if (user) {
|
|
const authStrategyData = user.auth[this.strategyId]
|
|
if (!authStrategyData) {
|
|
throw new Error('ERR_INVALID_STRATEGY')
|
|
} else if ((await bcrypt.compare(password, authStrategyData.password)) !== true) {
|
|
throw new Error('ERR_LOGIN_FAILED')
|
|
} else if (!user.isActive) {
|
|
throw new Error('ERR_INACTIVE_USER')
|
|
} else if (authStrategyData.restrictLogin) {
|
|
throw new Error('ERR_LOGIN_RESTRICTED')
|
|
} else if (!user.isVerified) {
|
|
throw new Error('ERR_USER_NOT_VERIFIED')
|
|
} else {
|
|
return user
|
|
}
|
|
} else {
|
|
throw new Error('ERR_LOGIN_FAILED')
|
|
}
|
|
}
|
|
}
|