const express = require("express") const passport = require("../middleware/passport") const router = express.Router() function saveReferer(req, res, next) { req.session.loginReferrer = req.get("Referer") req.session.save() next() } // Perform the login, after login Auth0 will redirect to callback router.get("/login", saveReferer, passport.authenticate("auth0", {scope: "openid username profile"}), (req, res) => { res.redirect("/") }) router.get("/callback", passport.authenticate("auth0", { failureRedirect: "/auth/login" }), (req, res) => { if (req.user == null) { throw new Error("user null"); } res.redirect(req.session.loginReferrer || "/"); }) router.get("/user", (req, res) => { if (req.user == null) { return res.status(401).json({error: {message: "Unauthorized"}}) } const name = req.user._json.name // TODO: Grab data from rpdata-api user database res.json({data: { name }}) }) router.get("/logout", saveReferer, (req, res) => { req.logout() res.redirect(req.session.loginReferrer || "/"); }) module.exports = router