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.

47 lines
1.1 KiB

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