nfp_sites/base/authentication/routes.mjs
Jonatan Nilsson 531c7acefe
Some checks failed
/ deploy (push) Failing after -65h42m50s
base: Fix error logging and reporting, make it better.
2023-11-14 07:26:54 +00:00

34 lines
854 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,
})
}
register(server) {
server.flaska.post('/api/authentication/login', server.jsonHandler(), this.login.bind(this))
}
/** GET: /api/authentication/login */
async login(ctx) {
let res = await ctx.db.safeCallProc('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
}
}