Skip to content

Instantly share code, notes, and snippets.

@AndrejGajdos
Last active June 24, 2018 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrejGajdos/a7cd27b4b2997564c628a850dc5b7135 to your computer and use it in GitHub Desktop.
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
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