Skip to content

Instantly share code, notes, and snippets.

@AndrejGajdos
Last active June 24, 2018 07:37
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/74cd18b6533963c5e83beb1a19c62ad8 to your computer and use it in GitHub Desktop.
Save AndrejGajdos/74cd18b6533963c5e83beb1a19c62ad8 to your computer and use it in GitHub Desktop.
koa router with routes for authentication project. Whole file is available https://github.com/AndrejGajdos/auth-flow-spa-node-react/blob/master/script/server.js
const auth = require('./controllers/auth');
const router = new Router();
router
/* Handle Login POST */
.post('/login', ctx => passport.authenticate('local', (err, user) => {
if (!user) {
ctx.throw(401, err);
} else {
ctx.body = user;
return ctx.login(user);
}
})(ctx))
/* GET User Profile */
.get('/users/profile', auth.getLoggedUser)
/* Handle Logout POST */
.get('/logout', (ctx) => {
ctx.logout();
ctx.body = {};
})
/* Handle Login Facebook */
.get('/auth/facebook', passport.authenticate('facebook'))
.get(
'/auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect: '/facebook/success/',
failureRedirect: '/',
}),
);
app.use(router.routes()).use(router.allowedMethods());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment