Created
August 6, 2019 03:48
-
-
Save rkpattnaik780/a520df1b4528b3fff61563f17693f668 to your computer and use it in GitHub Desktop.
/routes/auth-routes.js
This file contains hidden or 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
var express = require('express'); | |
var router = express.Router(); | |
var passport = require('passport'); | |
// To return the user data to the client | |
router.get("/check", (req, res) => { | |
console.log("user - " + req.user); | |
console.log(req.session.passport); | |
if (req.user === undefined) { | |
res.json({}); | |
} else { | |
res.json({ | |
user: req.user | |
}); | |
} | |
}); | |
// Navigating to auth/github provides us with option to sign in via github | |
router.get("/github", passport.authenticate("github")); | |
// The redirect url | |
router.get( | |
"/github/redirect", | |
passport.authenticate("github", { failureRedirect: "/" }), | |
(req, res) => { | |
// For redirecting into the client app | |
res.redirect("http://localhost:8080/"); | |
} | |
); | |
// The API to log out, it clears req.user | |
router.get('/logout', function(req, res, next) { | |
req.logout(); | |
res.json({ msg: "Logged out" }); | |
}); | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment