refactor(auth): Back-ported changes in auth controller

This commit is contained in:
danielhumgon
2020-01-29 03:32:42 -04:00
parent 9fa7f9e011
commit c22b301f85
2 changed files with 32 additions and 23 deletions
+29 -20
View File
@@ -1,6 +1,12 @@
const passport = require('koa-passport')
/**
let _this
class Auth {
constructor () {
_this = this
this.passport = passport
}
/**
* @apiDefine TokenError
* @apiError Unauthorized Invalid JWT token
*
@@ -12,7 +18,7 @@ const passport = require('koa-passport')
* }
*/
/**
/**
* @api {post} /auth Authenticate user
* @apiName AuthUser
* @apiGroup Auth
@@ -48,26 +54,29 @@ const passport = require('koa-passport')
* "error": "Unauthorized"
* }
*/
async authUser (ctx, next) {
try {
return _this.passport.authenticate('local', (err, user) => {
if (err) throw err
async function authUser (ctx, next) {
return passport.authenticate('local', (err, user, info, status) => {
if (err) throw err
if (!user) {
ctx.throw(401)
}
if (!user) {
const token = user.generateToken()
const response = user.toJSON()
delete response.password
ctx.body = {
token,
user: response
}
})(ctx, next)
} catch (error) {
ctx.throw(401)
}
const token = user.generateToken()
const response = user.toJSON()
delete response.password
ctx.body = {
token,
user: response
}
})(ctx, next)
}
}
module.exports.authUser = authUser
module.exports = Auth
+3 -3
View File
@@ -1,6 +1,6 @@
// import * as auth from './controller'
const auth = require('./controller')
const CONTROLLER = require('./controller')
const controller = new CONTROLLER()
// export const baseUrl = '/auth'
module.exports.baseUrl = '/auth'
@@ -9,6 +9,6 @@ module.exports.routes = [
{
method: 'POST',
route: '/',
handlers: [auth.authUser]
handlers: [controller.authUser]
}
]