nfp_sites/api/authentication/routes.mjs
2022-07-26 02:56:07 +00:00

30 lines
726 B
JavaScript

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,
})
}
/** GET: /api/authentication/login */
async login(ctx) {
let res = await ctx.db.safeCallProc('common.auth_login', [
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
}
}