Move auth controllers to new modules

This commit is contained in:
Adrian Obelmejias
2016-03-16 20:28:26 -04:00
parent b08cef1ffc
commit e79a98f8da
2 changed files with 36 additions and 0 deletions
+23
View File
@@ -0,0 +1,23 @@
import passport from 'koa-passport'
import jwt from 'jsonwebtoken'
import config from '../../config'
export async function authUser(ctx, next) {
return passport.authenticate('local', (user) => {
if (!user) {
ctx.throw(401)
}
const token = jwt.sign({ id: user.id }, config.token)
const response = user.toJSON()
delete response.password
delete response.salt
ctx.body = {
token,
user: response
}
})(ctx, next)
}
+13
View File
@@ -0,0 +1,13 @@
import * as auth from './controller'
export default {
base: '/auth',
'Authenticate user': {
method: 'POST',
route: '/',
handlers: [
auth.authUser
]
}
}