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.

51 lines
1.3 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", function (req, res, next) {
  16. passport.authenticate("auth0", function (err, user, info) {
  17. if (err) { return next(err) }
  18. if (!user) { console.log(err, user, info); return res.redirect("/auth/login") }
  19. req.logIn(user, function (err) {
  20. if (err) { return next(err) }
  21. res.redirect(req.session.loginReferrer || "/");
  22. })
  23. })(req, res, next);
  24. })
  25. router.get("/user", (req, res) => {
  26. if (req.user == null) {
  27. return res.status(401).json({error: {message: "Unauthorized"}})
  28. }
  29. const name = req.user._json.name
  30. // TODO: Grab data from rpdata-api user database
  31. res.json({data: {
  32. name
  33. }})
  34. })
  35. router.get("/logout", saveReferer, (req, res) => {
  36. req.logout()
  37. res.redirect(req.session.loginReferrer || "/");
  38. })
  39. module.exports = router