首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >电子和ReactJS,使用BrowserWindow进行GitHub oAuth身份验证

电子和ReactJS,使用BrowserWindow进行GitHub oAuth身份验证
EN

Stack Overflow用户
提问于 2015-05-16 23:23:17
回答 1查看 2.3K关注 0票数 5

我用ReactJ设置了github的电子。所以我有一个BrowserWindow和一个react应用程序在那个窗口里玩得很好。我想要实现的是使用GitHub进行身份验证。因此,当用户按下Login with Github按钮时,会打开一个新的BrowserWindow并转到github授权应用程序url。我的问题与回调有关,以及如何从回调中获得返回的代码。我已经在Apache和InAppBrowser中完成了这一工作,但是由于我能够使用localhost作为回调,所以情况就不同了。

到目前为止,我对电子所做的是打开新的BrowserWindow,但是在授权之后,我无法从回调中获得代码。

代码语言:javascript
复制
var authWindow = new BrowserWindow({ width: 800, height: 600, show: true, 'always-on-top': true });
var githubUrl = 'https://github.com/login/oauth/authorize?';
var authUrl = githubUrl + 'client_id=' + options.client_id + '&scope=' + options.scope;

authWindow.loadUrl(authUrl);
authWindow.setVisibleOnAllWorkspaces(true);
authWindow.setResizable(false);

authWindow.addListener('page-title-updated', function(stream) {
  console.log("LOADED");
  console.log(JSON.stringify(stream));
  console.log(stream);

  var url = (typeof stream.url !== 'undefined' ? stream.url : stream.originalEvent.url),
    raw_code = /code=([^&]*)/.exec(stream.url) || null,
    code = (raw_code && raw_code.length > 1) ? raw_code[1] : null,
    error = /\?error=(.+)$/.exec(strean.url);

  if (code || error) {
    authWindow.close();
  }

  // If there is a code in the callback, proceed to get token from github
  if (code) {
    // requestToken(code);
  } else if (error) {
    alert("Oops! Couldn't log authenticate you with using Github.");
  }
});

在我做console.log(JSON.stringify(stream));的地方,我得到了{},所以它必须要做eventListener?有什么主意或者更好的方法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-01 22:21:15

所以我错过的是正确的event。正确的做法是:

代码语言:javascript
复制
// Build the OAuth consent page URL
var authWindow = new BrowserWindow({ width: 800, height: 600, show: false, 'node-integration': false });
var githubUrl = 'https://github.com/login/oauth/authorize?';
var authUrl = githubUrl + 'client_id=' + options.client_id + '&scope=' + options.scopes;
authWindow.loadUrl(authUrl);
authWindow.show();

// Handle the response from GitHub
authWindow.webContents.on('did-get-redirect-request', function(event, oldUrl, newUrl) {

  var raw_code = /code=([^&]*)/.exec(newUrl) || null,
    code = (raw_code && raw_code.length > 1) ? raw_code[1] : null,
    error = /\?error=(.+)$/.exec(newUrl);

  if (code || error) {
    // Close the browser if code found or error
    authWindow.close();
  }

  // If there is a code in the callback, proceed to get token from github
  if (code) {
    requestGithubToken(options, code);
  } else if (error) {
    alert("Oops! Something went wrong and we couldn't log you in using Github. Please try again.");
  }

});

// Reset the authWindow on close
authWindow.on('close', function() {
    authWindow = null;
}, false);

我还编写了一个教程,其中描述了在http://manos.im/blog/electron-oauth-with-github/中可以找到的完整实现

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30281732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档