流星,反应,身份验证和授权问题



我正在使用流星和反应构建一个小应用程序。我不使用很棒的流星帐户库来处理用户身份验证,因为我需要消耗外部API。

基本上,流星的服务器端被用作与API通信的代理。

我创建了从流星到API的身份验证:

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);
    }
  }
});

这很好...

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');
    }
  });

}

不幸的是,我看不到会话值正确保存,因此我无法创建控件以将身份验证或未经身份验证的用户重定向到正确的页面...

我不知道我的方法是正确的...我想知道我在做什么错,如果有更好的解决方案来处理它。

例如,我不想将令牌和user_id保存在客户端中,我想像Meteor为他的用户收集一样保存IT服务器端,并且能够每次都可以处理我的所有API请求而无需每次传递令牌...

不幸的是,我看不到会话值正确保存,所以我可以 不创建控件以重定向身份验证或未经确认的用户 到正确的页面...

流星会话需要一个键值对。

因此,您可能宁愿尝试:

Session.set("userAuth",{
    user_id: res.response.user_id,
    token: res.response.token
});

Session.set("userId", res.response.user_id);
Session.set("userToken",res.response.token);

例如,我不想将令牌和user_id保存在客户端中,我 想要像流星为他的用户那样保存服务器端 收集并能够处理我的所有API请求而无需通过 每次都签名...

实际上,流星在使用浏览器的LocalStorage上成功登录到客户端后存储用户令牌。

使用帐户使用流星应用登录并检查您的本地信息; - )

最新更新