nfp_sites/base/authentication/routes.mjs

35 lines
842 B
JavaScript
Raw Normal View History

2022-07-20 00:33:06 +00:00
import crypto from 'crypto'
import * as util from '../util.mjs'
import config from '../config.mjs'
export default class AuthenticationRoutes {
constructor(opts = {}) {
Object.assign(this, {
secret: config.get('jwtsecret'),
crypto: crypto,
util: util,
})
}
register(server) {
server.flaska.post('/api/authentication/login', server.jsonHandler(), this.login.bind(this))
}
2022-07-20 00:33:06 +00:00
/** GET: /api/authentication/login */
async login(ctx) {
2022-07-27 08:41:18 +00:00
let res = await ctx.db.safeCallProc('auth_login', [
2022-07-20 00:33:06 +00:00
ctx.req.body.email,
ctx.req.body.password,
])
let out = res.results[0][0]
const hmac = this.crypto.createHmac('sha256', this.secret)
hmac.update(out.token)
let apiSignature = this.util.encode(hmac.digest())
out.token = out.token + '.' + apiSignature
ctx.body = out
}
}