1

尝试登录任何用户时,会导致以下 POST 错误:

XMLHttpRequest cannot load https://api.stormpath.com/v1/applications/[APP_HREF]/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 401.

在我的 server.js 文件中,我输入了以下内容:

var express = require('express');
var stormpath = require('express-stormpath'); 
var cors = require('cors');

var app = express();

app.use(stormpath.init(app, {

  apiKey: {
    id: 'xyz',
    secret: 'abc'  // Using the unsafe inline option for example purposes
  },
  application: {
    href: `[APP_HREF]`
  },
  web: {
    produces: ['application/json']
  },
  debug: 'info'
}));

app.use(cors({
  origin: 'http://localhost:8080',
  credentials: true
}));

app.post('/', stormpath.loginRequired, function (req, res) {
  function writeError(message) {
    res.status(400);
    res.json({ message: message, status: 400 });
    res.end();
  }



});

app.on('stormpath.ready', function () {
  var server = app.listen(process.env.PORT, function() {
    try {
      process.send('CONNECTED');
    } catch(e) {}
  });
});

在我的 login.jsx 文件中,我包括:

login: function(e) {
      e.preventDefault();
      e.stopPropagation();


      this.serverRequest = $.post('https://api.stormpath.com/v1/applications/[APP_HREF]/login', 
        { 
        "username": document.getElementById("email").value,
        "password": document.getElementById("pass").value
        }, function (result) {
        console.log(result);

      });

我还保存了我的stormpath.yml 文件。

我没有使用 React-Stormpath,因为我已经为登录和注册创建了视图。看起来我只需要使用 Stormpath 访问 REST api,但我不确定我需要添加什么才能验证 API 密钥。

在实际的 login.jsx 文件中,我是否必须发送 ID:SECRET 对以及 POST 请求的一部分?

4

1 回答 1

2

我在您的文章中看到login.jsx您正在尝试直接发布到 Stormpath REST API,但不幸的是,这还不可能。相反,您将发布到您的 Express 服务器,然后它将与 Stormpath 通信。

您已经拥有express-stormpathExpress 服务器,因此您只需将登录表单发布到/login,Stormpath 将负责其余的工作:)

如果您遇到任何问题,请告诉我们!仅供参考,我们将很快添加“无服务器”功能,您可以在此处关注:http: //ideas.stormpath.com/ideas/IAM-I-59

于 2016-08-29T16:51:08.470 回答