如何使用Javascript和访问令牌移动到另一个页面?

时间:2016-01-18 19:39:04

标签: javascript jquery oauth-2.0

这个问题必须已经解决了数十亿次,但我很难找到自己的方式。我有一个后端服务,用用户/密码授予OAuth2令牌。当用户登录时,我希望浏览器转到下一页。问题是,这样的页面只能通过auth标头访问。 document.location.href = "http://nextpage"之类的方法不允许我设置授权标头,例如Authorization = Bearer 1234567890

用户点击"登录"时运行的代码按钮是:

var json = {
  grant_type: "password",
  client_id: "myClient",
  username: email, // <--- comes from a form
  password: password, // <--- comes from a form
  client_secret: "mySecret"
};

$.ajax({
  url: "http://localhost:9000/auth/token/grant",
  method: 'POST',
  contentType: "application/json",
  dataType: 'json',
  data: JSON.stringify(json),
  cache: false,
  success: function (data) {
    localStorage.authToken = data.access_token;
    localStorage.refreshToken = data.refresh_token;

    // Here I would like to move the browser to the next page
    document.location.href = "http://localhost:9000/nextPage";

  }.bind(this),
  error: function (xhr, status, err) {
    console.error("login request error: ", status, err.toString());
  }.bind(this)
});

感谢您的提示!

0 个答案:

没有答案