问题描述
我在我的项目中使用feathersjs框架。在较旧的版本中,我的中间件可以运行,但是可以更新框架,并在创建新应用并验证中间件后无法正常工作。
const cookieParser = require('cookie-parser');
const { authenticate } = require('@feathersjs/express');
module.exports = function (app) {
app.get('/login',(req,res) => {
res.sender('login');
});
app.use('/',cookieParser(),authenticate('jwt'),async (req,res) => {
const { user } = req;
try {
await app.service('users').get(user._id);
res.sender('home');
} catch(e){
res.redirect('/login');
}
});
}
$(document).ready(function(){
const socket = io();
const app = feathers();
app.configure(feathers.socketio(socket));
app.configure(feathers.authentication({
storage: window.localStorage
}));
$('.form-signin').submit(function(){
app.authenticate({
strategy: 'local',username: $('#inputUsername').val(),password: $('#inputPassword').val(),}).then( result => {
document.cookie = "feathers-jwt=" + result.accesstoken;
window.location.href = "/";
}).catch(error => {});
});
});
我的问题是,当我正确单击带有数据的登录按钮时,我收到一个accesstoken,但是每次401错误代码-都没有授权时,我看不到主页和应用程序向我显示。
控制台会向我显示以下信息:info: Invalid authentication @R_714_4045@ion (no `strategy` set) {"type":"FeathersError","name":"NotAuthenticated","code":401,"className":"not-authenticated","errors":{}}
在新版本中也无法正常工作失败重定向:'/ login'
解决方案
在app.get('/',authenticate('jwt'),(req,res)=> {})之前添加以下代码;
app.use('/',res,next) => {
var cookies = req.cookies;
var token = cookies['feathers-jwt'];
req.authentication = {
strategy: 'jwt',accesstoken: token
}
next();
})
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)