2016-07-15 91 views
2

如何實現使用OAuth與devise_token_auth寶石的認證系統?如何使用devise_token_auth gem進行社交登錄?

我已搜查幾乎無處不在互聯網上,包括https://github.com/lynndylanhurley/devise_token_auth回購所有文件和所有它的問題搜索。

我正在開發一個API來處理用戶註冊並通過社交網絡日誌,但我不能找到一種方法,使這一瑰寶這一工作。

問題

def social_login 
    uri = URI('http://localhost:3000/auth/facebook') 
    response = Net::HTTP.get_response(uri) 
    puts response.body 
    redirect_to :back 
    end 

這種方法用於測試API(本地主機:3000)給了我這樣的響應:

<html><body>You are being <a href="http://localhost:3000/omniauth/facebook?resource_class=User">redirected</a>.</body></html> 

我的Facebook的回調URL:

http://localhost:4000/auth/facebook/callback 

客戶端測試人員:localhost:4000

我的API路線:

    Prefix Verb URI Pattern            Controller#Action 
     new_user_session GET /auth/sign_in(.:format)         devise_token_auth/sessions#new 
      user_session POST /auth/sign_in(.:format)         devise_token_auth/sessions#create 
    destroy_user_session DELETE /auth/sign_out(.:format)        devise_token_auth/sessions#destroy 
      user_password POST /auth/password(.:format)        devise_token_auth/passwords#create 
     new_user_password GET /auth/password/new(.:format)       devise_token_auth/passwords#new 
     edit_user_password GET /auth/password/edit(.:format)       devise_token_auth/passwords#edit 
         PATCH /auth/password(.:format)        devise_token_auth/passwords#update 
         PUT /auth/password(.:format)        devise_token_auth/passwords#update 
cancel_user_registration GET /auth/cancel(.:format)         devise_token_auth/registrations#cancel 
     user_registration POST /auth(.:format)           devise_token_auth/registrations#create 
    new_user_registration GET /auth/sign_up(.:format)         devise_token_auth/registrations#new 
    edit_user_registration GET /auth/edit(.:format)         devise_token_auth/registrations#edit 
         PATCH /auth(.:format)           devise_token_auth/registrations#update 
         PUT /auth(.:format)           devise_token_auth/registrations#update 
         DELETE /auth(.:format)           devise_token_auth/registrations#destroy 
    auth_validate_token GET /auth/validate_token(.:format)       devise_token_auth/token_validations#validate_token 
      auth_failure GET /auth/failure(.:format)         devise_token_auth/omniauth_callbacks#omniauth_failure 
         GET /auth/:provider/callback(.:format)      devise_token_auth/omniauth_callbacks#omniauth_success 
         GET /omniauth/:provider/callback(.:format)     devise_token_auth/omniauth_callbacks#redirect_callbacks 
     omniauth_failure GET /omniauth/failure(.:format)        devise_token_auth/omniauth_callbacks#omniauth_failure 
         GET /auth/:provider(.:format)        redirect(301) 

回答