Javascript Meteor、React、身份验证和授权问题
我正在使用Meteor和React构建一个小应用程序。我没有使用awesome Meteor帐户库来处理用户身份验证,因为我需要使用外部API 基本上,Meteor的服务器端用作与API通信的代理 我创建了从Meteor到API的身份验证:Javascript Meteor、React、身份验证和授权问题,javascript,reactjs,meteor,Javascript,Reactjs,Meteor,我正在使用Meteor和React构建一个小应用程序。我没有使用awesome Meteor帐户库来处理用户身份验证,因为我需要使用外部API 基本上,Meteor的服务器端用作与API通信的代理 我创建了从Meteor到API的身份验证: Meteor.methods({ 'user.authentication': function (email, password) { try { const res = request.postSync(authentica
Meteor.methods({
'user.authentication': function (email, password) {
try {
const res = request.postSync(authenticate, {
method: 'POST',
json: true,
body: {
email, password
}
});
if (res.response.statusCode === 200) {
return res.body;
}
throw new Meteor.Error(res.response.statusCode, res.response.body.error.message);
} catch (err) {
throw new Meteor.Error(400, err.message);
}
}
});
这很好用。。。登录组件发送和接收成功的数据,我正在尝试使用Meteor会话“保存”用户会话:
Login.js:
onSubmit(e) {
e.preventDefault();
const email = this.refs.email.value.trim();
const password = this.refs.password.value.trim();
Meteor.call('user.authentication', email, password, (error, res) => {
if (error) {
console.error(error.reason);
} else {
Session.set({
user_id: res.response.user_id,
token: res.response.token
});
history.push('/account');
}
});
}
不幸的是,我没有看到正确保存的会话值,因此我无法创建控件将经过身份验证或未经身份验证的用户重定向到正确的页面
我不知道我的方法是否正确。。。我想知道我做错了什么,万一有更好的解决办法来处理它
例如,我不想在客户端保存令牌和用户id,我想像Meteor那样在服务器端为他的用户集合保存它,并且能够处理我所有的API请求,而无需每次传递令牌
不幸的是,我没有看到正确保存的会话值,因此我可以
不创建重定向已验证或未验证用户的控件
到正确的页面
流星会议
因此,您不妨尝试:
Session.set("userAuth",{
user_id: res.response.user_id,
token: res.response.token
});
或
例如,我不想在客户端保存令牌和用户id,我
想保存它的服务器端像流星为他的用户做
收集并能够处理所有API请求,而无需传递
每次都是代币
实际上,Meteor在使用浏览器的登录成功登录客户端后存储用户令牌
使用帐户登录Meteor应用程序并检查您的本地存储;-)
Session.set("userId", res.response.user_id);
Session.set("userToken",res.response.token);