3

I'm doing administration dashboard with use a token jwt. I would like to the user can obtain access to the middleware only if logged. I want to do that if he is logged, the server is shows home page if not, then he is redirect to login page. Below is my middleware index.js

const auth = require('feathers-authentication');

const pupils = require('./pupils');
const login = require('./login');

const home = require('./home');

module.exports = function () {
  // Add your custom middleware here. Remember, that
  // in Express the order matters
  const app = this; // eslint-disable-line no-unused-vars

  app.use('/pupils.html', pupils());
  app.use('/login.html', login());
  app.use('/', home());

  app.post('/login', auth.express.authenticate('local', { successRedirect: '/', failureRedirect: '/login.html' }));
};

and pupils.js with use a handlebars

const lang = require('../../config/pupils.json');

module.exports = function (options = {}) {

  return function login(req, res) {

      res.render('home', lang);

  };
};

If I open the main path '/' then it is redirecting to /login.html

I try authentication my user below code:

  app.authenticate({
      strategy: 'local',
      email: 'username',
      password: 'password'
    }).then(function(result){
      console.log('Authenticated!', result);
    }).catch(function(error){
      console.error('Error authenticating!', error);
    });

When use app.authenticate method I receive a token object and it is storage in local storege but when I try again open main path, again I redirect to /login.html. What I do wrong?

How can I to do in this way:

1) I open this address http://127.0.0.1:3030:/
2) middleware on the server check if user is logged
3) if logged show home page, if not show login page
SeaDog
  • 645
  • 1
  • 9
  • 32

0 Answers0