2017-10-06 24 views
0

Windows 7 32ビットホストでVagrantubuntu/trusty32というボックスを使用してRailsアプリを設定しています。アプリにはGoogle OAuthのログインがあります。 Gemfileには、それに対して次の宝石を持っている:omniauth-google-oauth2でログインできません。

gem 'omniauth' 
gem 'omniauth-google-oauth2' 

そして/config/initializers/devise.rb中:私がログインしようとすると

config.omniauth :google_oauth2, ENV["GOOGLE_APP_ID"], ENV["GOOGLE_APP_SECRET"] 
    OmniAuth.config.full_host = lambda do |env| 
    forwarded = env['HTTP_X_FORWARDED_FOR'] 
    forwarded.blank? ? "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}" : "https://#{env['HTTP_HOST']}" 
    end 

、それは「あなたがリダイレクトされる」とは何も次の起こっていない見て、このURLで停止します。

http://localhost:3000/omniauths/auth/google_oauth2/callback?state=xxxx&code=xxx

私は、次のログを得た:

Started GET "/omniauths/auth/google_oauth2" for 10.0.2.2 at 2017-10-07 11:07:21 +0000 
log writing failed. closed stream 
I, [2017-10-07T11:07:21.507524 #2581] INFO -- omniauth: (google_oauth2) Request phase initiated. 
Started GET "/omniauths/auth/google_oauth2/callback?state=xxxx&code=xxxx" for 10.0.2.2 at 2017-10-07 11:07:21 +0000 
log writing failed. closed stream 
I, [2017-10-07T11:07:22.155462 #2581] INFO -- omniauth: (google_oauth2) Callback phase initiated. 
Processing by SessionController#google_oauth2 as HTML 
log writing failed. closed stream 
    Parameters: {"state"=>"a6a1e7b1733c564b96ab650f360fbae63cff5bdc6a488f1a", "code"=>"4/D4aX9Bm9XQmSIm_RXBhBi1EXE1AHKp3Q-WVbSpNSdvE"} 
log writing failed. closed stream 
    Omniauth Load (4.5ms) SELECT `omniauths`.* FROM `omniauths` WHERE `omniauths`.`provider` = 'google_oauth2' AND `omniauths`.`uid` = '110281880098696358918' LIMIT 1 
log writing failed. closed stream 
    Permission::Organization Load (1.6ms) SELECT `permission_organizations`.* FROM `permission_organizations` WHERE `permission_organizations`.`domain` = 'gmail.com' LIMIT 1 
log writing failed. closed stream 
Redirected to http://localhost:3000/ 
log writing failed. closed stream 
Completed 403 Forbidden in 302ms (ActiveRecord: 17.4ms) 

コードは私ではないと私はあまりにもルビーの専門家ではありません。だから私はなぜ私はその画面で立ち往生しているのか分かりません。ここで

class SessionController < Devise::OmniauthCallbacksController 
    def google_oauth2 
    @omniauth = Omniauth.find_for_google(request.env['omniauth.auth']) 

    if @omniauth&.persisted? 
     @omniauth.user.last_sign_in = Time.now 
     @omniauth.user.save 
     flash[:notice] = 'I signed in with Google authentication' 
     sign_in_and_redirect @omniauth, event: :authentication 
    else 
     #redirect_to new_user_registration_url, alert: @user.errors.full_messages.join("\n") 
     redirect_to '/', status: :forbidden 
    end 
    end 
end 

情報は、この問題のために十分に見えないconfig/routes.rb

Rails.application.routes.draw do 
    devise_for :omniauths, controllers: { 
     omniauth_callbacks: "session" 
    } 
    mount RailsAdmin::Engine => '/rails_admin', as: 'rails_admin' 

    post '/api', to: "api#post" 
    post '/error_catch', to: "api#error_catch" 
    post '/error_log', to: "api#error_log" 
    get '/file/:uuid/:filename', to: "api#file" 
    get '/svg_parts/*dxf_path', to: "svg#part" 
    post '/svg/project(.:format)' 
    post '/svg/requirement(.:format)' 
    get '/svg/test(.:format)' 
    get '/pdf/owner_estimate' 
    get '/pdf/builder_estimate' 
    get '/pdf/wholesale_estimate' 

    get '/api/perform_test' 

    get '/pdf/requirement' 

    root to: "angular#index" 
    get '/assets/*path', controller: 'application', action: 'handle_404' 
    get '*path', to: "angular#index" 
end 

です:私はまた、コントローラを見つけましたか?私は、さらに役立つ情報を提供することもできます。

+0

はのは、試してみましょう。この=> '' 'devise_for:ユーザー、コントローラー:{omniauth_callbacks: "セッション"}' ''私は多分omniauthableないリソース上の誤り 'マッピングomniauth_callbacks(例外ArgumentError)' –

+0

@RonanLouarnあなたのモデルの中で '' devise:omniauthable、omniauth_providers:[:google_oauth2] '' 'と書いていました@Sithu – Sithu

+0

を得た –

答えて

0

gemfile.rb

gem 'omniauth-google-oauth2' 

config/initializer/omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do 
    provider :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"], 
    { 
     :name => "google", 
     :scope => "email, profile, plus.me, http://gdata.youtube.com", 
     :prompt => "select_account", 
     :image_aspect_ratio => "square", 
     :image_size => 50 
    } 
end 

config/initializer/devise.rb

config.omniauth :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"], {} 

routes.rb

devise_for :users, controllers: {registrations: 'users/registrations',omniauth_callbacks: 'users/omniauth_callbacks'} 

controllers/users/omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController 

def google_oauth2 
    @user = User.from_omniauth(request.env["omniauth.auth"]) 

    if @user.persisted? 
     flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google" 
     sign_in_and_redirect @user, :event => :authentication 
    else 
     session["devise.google_data"] = request.env["omniauth.auth"] 
     redirect_to new_user_registration_url 
    end 
    end 
end 

app/views/devise/registrations/new.html.erb

<%- resource_class.omniauth_providers.each do |provider| %> 

    <%= link_to omniauth_authorize_path(resource_name, provider) do %> 
     <i class="fa fa-google-plus-official fa-2x" aria-hidden="true"</i> 

    <% end %> 
<% end -%> 
[... orginal devise view] 

/app/models/user.rb

class User 
    devise :omniauthable, omniauth_providers: [:google_oauth2] 
    [...] 
end 

私はあなたを助けることを願っています!

関連する問題