The frontend/UI server, written in JS using the MarkoJS library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.1 KiB

6 years ago
  1. const express = require("express")
  2. const passport = require("../middleware/passport")
  3. const router = express.Router()
  4. function saveReferer(req, res, next) {
  5. req.session.loginReferrer = req.get("Referer")
  6. req.session.save()
  7. next()
  8. }
  9. // Perform the login, after login Auth0 will redirect to callback
  10. router.get("/login", saveReferer, passport.authenticate("auth0", {scope: "openid username profile"}), (req, res) => {
  11. res.redirect("/")
  12. })
  13. router.get("/callback", passport.authenticate("auth0", { failureRedirect: "/auth/login" }), (req, res) => {
  14. if (req.user == null) {
  15. throw new Error("user null");
  16. }
  17. res.redirect(req.session.loginReferrer || "/");
  18. })
  19. router.get("/user", (req, res) => {
  20. if (req.user == null) {
  21. return res.status(401).json({error: {message: "Unauthorized"}})
  22. }
  23. const name = req.user._json.name
  24. // TODO: Grab data from rpdata-api user database
  25. res.json({data: {
  26. name
  27. }})
  28. })
  29. router.get("/logout", saveReferer, (req, res) => {
  30. req.logout()
  31. res.redirect(req.session.loginReferrer || "/");
  32. })
  33. module.exports = router