如何在使用feathersjs响应NodeJs之前发送令牌jwt来validation?
我正在使用令牌jwt做pipe理仪表板。 我想用户只有login才能获得对中间件的访问权限。 我想这样做,如果他login,服务器显示主页,如果没有,然后他redirect到login页面。 下面是我的中间件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' })); };
和pupils.js使用手柄
const lang = require('../../config/pupils.json'); module.exports = function (options = {}) { return function login(req, res) { res.render('home', lang); }; };
如果我打开主path“/”,那么它将redirect到/login.html
我尝试authentication我的用户下面的代码:
app.authenticate({ strategy: 'local', email: 'username', password: 'password' }).then(function(result){ console.log('Authenticated!', result); }).catch(function(error){ console.error('Error authenticating!', error); });
当使用app.authenticate方法时,我收到一个令牌对象,它是存储在本地存储区,但是当我再次尝试打开主path时,我又redirect到/login.html。 我做错了什么?
我该如何做到这一点:
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