简体   繁体   English

使用Ember.js和Torii(oauth2)连接到github

[英]Connecting to github with Ember.js and Torii (oauth2)

I'm trying to use the github-oauth2 provider in Torii, but I'm stumped on how I'm supposed to se tup some of the callbacks. 我正试图在Torii中使用github-oauth2提供程序,但我很难理解我应该如何设置一些回调。 I'll trace the code I'm using, as well as my understanding of it, and hopefully that can help pinpoint where I'm going wrong. 我将跟踪我正在使用的代码,以及我对它的理解,并希望这可以帮助确定我出错的地方。

First, in my action, I'm calling torii's open method as it says to do in the docs: 首先,在我的行动中,我正在调用torii的open方法,正如它在文档中所说的那样:

this.get('torii').open('github-oauth2').then((data) => {
  this.transitionTo('dashboard')
})

And, of course, I have the following setup in my config/environment.js : 当然,我在config/environment.js进行了以下设置:

var ENV = {
  torii: {
    // a 'session' property will be injected on routes and controllers
    sessionServiceName: 'session',
    providers: {
      'github-oauth2': {
        apiKey:      'my key',
        redirectUri: 'http://127.0.0.1:3000/github_auth'
      }
    }
  },
}

The redirectUri is for my Rails server. redirectUri用于我的Rails服务器。 I have the same redirectUri setup on my github app, so they match. 我在我的github应用程序上有相同的redirectUri设置,所以它们匹配。

Here's what I have on my server. 这是我在服务器上的内容。 It's likely this is where the problem is. 这可能就是问题所在。 I'll get to the symptoms at the end. 我会在最后得到症状。

def github
  client_id = 'my id'
  client_secret = 'my secret'
  code = params[:code]
  @result = HTTParty.post("https://github.com/login/oauth/access_token?client_id=#{client_id}&client_secret=#{client_secret}&code=#{code}")
  @access_token = @result.parsed_response.split('&')[0].split('=')[1]
  render json: {access_token: @access_token}  
end

So I post to github's access_token endpoint, as I'm supposed to, and I get back a result with an access token. 所以我发布到github的access_token端点,就像我应该的那样,我得到一个带有访问令牌的结果。 Then I package up that access token as json. 然后我将该访问令牌打包为json。

The result of this is that the torii popup goes to the rails page: 结果是torii弹出窗口转到rails页面:

在此输入图像描述

Unfortunately, what I was hoping for was for the torii popup to disappear, give my app the access_token , and for the code to move on and execute the code in my then block. 不幸的是,我希望的是torii popup消失,给我的应用程序access_token ,并让代码继续前进并执行我的then块中的代码。

Where am I going wrong? 我哪里错了?

Many thanks to Kevin Pfefferle, who helped me solve this and shared the code to his app ( gitzoom ) where he had implemented a solution. 非常感谢Kevin Pfefferle,他帮助我解决了这个问题,并将代码分享给了他的应用程序( gitzoom ),他已经实现了一个解决方案。

So the first fix is to clear my redirectUri , and to set it on github to localhost:4200 . 所以第一个修复是清除我的redirectUri ,并将它设置在github上localhost:4200 This made the app redirect so that it's an Ember app that it's redirected to. 这使得应用程序重定向,因此它是重定向到的Ember应用程序。

The second fix was to create a custom torii provider 第二个修复是创建自定义torii提供程序

//app/torii-providers/github.js
import Ember from 'ember';
import GitHubOauth2Provider from 'torii/providers/github-oauth2';

export default GitHubOauth2Provider.extend({
  ajax: Ember.inject.service(),
  fetch(data) {
    return data;
  },
  open() {
    return this._super().then((toriiData) => {
      const authCode = toriiData.authorizationCode;
      const serverUrl =  `/github_auth?code=${authCode}`;

      return this.get('ajax').request(serverUrl)
        .then((data) => {
          toriiData.accessToken = data.token;
          return toriiData;
        });
    });
  }
});

Not sure why this then triggers but the then I was using before didn't. 不知道为什么这then触发,但then我用没了。 Anyways, it grabs the data and returns it, and then the promise I was using before gets the data correctly. 无论如何,它抓取数据并返回它,然后我正在使用的承诺才能正确获取数据。

this.get('torii').open('github-oauth2').then((data) => {
  //do signon stuff with the data here
  this.transitionTo('dashboard')
})

So there we go! 我们去了! Hopefully this helps other folks who are stuck in the future. 希望这有助于其他陷入困境的人们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM