2016-05-16 54 views
3

我正嘗試在Torii中使用github-oauth2提供程序,但是我很難理解如何設置一些回調。我會跟蹤我使用的代碼,以及我對它的理解,並希望能夠幫助我確定哪裏出錯。通過Ember.js和Torii連接到github(oauth2)

首先,在我的行動,我打電話牌坊的open方法,因爲它說,在文檔中要做到:

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

,當然,我有我的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' 
     } 
    } 
    }, 
} 

redirectUri適用於我的Rails服務器。我有我的github應用程序相同的redirectUri設置,所以他們匹配。

這是我在我的服務器上。這可能是問題所在。我會在最後解決症狀。

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 

所以我發佈到GitHub的端點的access_token,因爲我應該和我回去與訪問令牌的結果。然後,我將該訪問令牌打包爲json。

這樣做的結果是,牌坊彈出去鐵軌頁:

enter image description here

不幸的是,我希望的是爲牌坊彈出消失,給我的應用程序的access_token,和代碼移動並執行我的then塊中的代碼。

我哪裏錯了?

回答

5

非常感謝Kevin Pfefferle,他幫我解決了這個問題,並將代碼分享給了他的應用程序(gitzoom),他在那裏實施了一個解決方案。

因此,第一個修復方法是清除我的redirectUri,並將其設置爲github至localhost:4200。這使得應用重定向,因此它是重定向到的Ember應用。

第二個補丁是創建一個自定義的牌坊提供商

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

不知道爲什麼這then觸發器,但是我用的是then之前沒有。無論如何,它抓住數據並返回它,然後我以前使用的承諾正確地獲取數據。

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

所以我們走吧!希望這有助於未來的其他人。