2019-09-15 01:53:38 +00:00
|
|
|
import Staff from '../staff/model.mjs'
|
|
|
|
import Jwt from '../jwt.mjs'
|
|
|
|
|
|
|
|
export default class AuthHelper {
|
|
|
|
constructor(opts = {}) {
|
|
|
|
Object.assign(this, {
|
|
|
|
Staff: opts.Staff || Staff,
|
|
|
|
jwt: opts.jwt || new Jwt(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async loginStaff(ctx) {
|
|
|
|
let staff
|
|
|
|
|
|
|
|
try {
|
|
|
|
staff = await this.Staff
|
2021-02-05 11:50:01 +00:00
|
|
|
.getSingleQuery(
|
|
|
|
this.Staff.query(qb => qb.where({ email: ctx.request.body.username }), [], ['*']),
|
|
|
|
true
|
|
|
|
)
|
2019-09-15 01:53:38 +00:00
|
|
|
|
2021-02-05 11:50:01 +00:00
|
|
|
await this.Staff.compare(ctx.request.body.password, staff.password)
|
2019-09-15 01:53:38 +00:00
|
|
|
} catch (err) {
|
|
|
|
if (err.message === 'EmptyResponse' || err.message === 'PasswordMismatch') {
|
|
|
|
ctx.throw(422, 'The email or password did not match')
|
|
|
|
}
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
|
2021-02-05 11:50:01 +00:00
|
|
|
return this.jwt.createToken(staff.id, staff.email, staff.level)
|
2019-09-15 01:53:38 +00:00
|
|
|
}
|
|
|
|
}
|