Last active
June 24, 2018 07:40
-
-
Save AndrejGajdos/a7cd27b4b2997564c628a850dc5b7135 to your computer and use it in GitHub Desktop.
Authenticated route in Koa using Passport. Whole file is available https://github.com/AndrejGajdos/auth-flow-spa-node-react/blob/master/script/controllers/auth.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.getLoggedUser = async (ctx) => { | |
if (ctx.isAuthenticated()) { | |
const reqUserId = ctx.req.user.id; | |
let user = null; | |
await getAsync('usersMockDatabase').then((users) => { | |
user = JSON.parse(users).find(currUser => currUser.id === reqUserId); | |
}); | |
if (user) { | |
delete user.password; | |
ctx.response.body = user; | |
} else { | |
const statusCode = 500; | |
ctx.throw(statusCode, "User doesn't exist"); | |
} | |
} else { | |
ctx.redirect('/'); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment