This commit is contained in:
Adrian Obelmejias
2016-02-11 19:20:50 -05:00
parent bffdd32550
commit 59264a030e
3 changed files with 38 additions and 10 deletions
+28
View File
@@ -0,0 +1,28 @@
import Router from 'koa-router'
import passport from 'koa-passport'
import jwt from 'jsonwebtoken'
import config from '../../config/config'
const router = new Router({ prefix: '/auth' })
router.post('/', async (ctx, next) =>
passport.authenticate('local', (user) => {
if (!user) {
ctx.throw(401)
}
const token = jwt.sign({ id: user.id }, config.tokenSecret)
const response = user.toJSON()
delete response.password
delete response.salt
ctx.body = {
token,
user: response
}
})(ctx, next)
)
export default router