users.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License.
  4. */
  5. var express = require('express');
  6. var router = express.Router();
  7. var fetch = require('../fetch');
  8. var { GRAPH_ME_ENDPOINT } = require('../authConfig');
  9. // custom middleware to check auth state
  10. function isAuthenticated(req, res, next) {
  11. if (!req.session.isAuthenticated) {
  12. return res.redirect('/tab-auth'); // redirect to sign-in route
  13. }
  14. next();
  15. };
  16. router.get('/id',
  17. isAuthenticated, // check if user is authenticated
  18. async function (req, res, next) {
  19. res.render('id', { idTokenClaims: req.session.account.idTokenClaims });
  20. }
  21. );
  22. router.get('/profile',
  23. isAuthenticated, // check if user is authenticated
  24. async function (req, res, next) {
  25. try {
  26. const graphResponse = await fetch(GRAPH_ME_ENDPOINT, req.session.accessToken);
  27. res.render('profile', { profile: graphResponse });
  28. } catch (error) {
  29. next(error);
  30. }
  31. }
  32. );
  33. module.exports = router;