25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const debug = require('debug')('server-connect:secure');
  2. const config = require('./config');
  3. module.exports = function (app) {
  4. if (config.passport) {
  5. const passport = require('passport');
  6. const ServerConnectStrategy = require('../auth/passport');
  7. passport.use(new ServerConnectStrategy({ provider: 'security' }));
  8. app.use(passport.initialize());
  9. app.use(passport.session());
  10. app.use(passport.authenticate('server-connect'));
  11. debug('Passport initialized', passport.strategies);
  12. app.use((req, res, next) => {
  13. debug('auth', req.isAuthenticated());
  14. debug('Session', req.session);
  15. if (req.user) {
  16. debug('User', req.user);
  17. }
  18. next();
  19. });
  20. app.use('/api/secure', restrict());
  21. }
  22. };
  23. // restrict middleware
  24. function restrict (options = {}) {
  25. return async function (req, res, next) {
  26. if (req.isAuthenticated()) {
  27. return next();
  28. }
  29. if (req.is('json')) {
  30. return res.status(401).json({ error: 'Unauthorized' });
  31. }
  32. if (options.redirect) {
  33. return res.redirect(options.redirect);
  34. }
  35. res.status(401).send('Unauthorized');
  36. };
  37. }