我正在使用Vagrant在Windows 7 32位主机中使用ubuntu/trusty32框设置Rails应用。该应用已通过Google OAuth登录。 Gemfile有以下宝石:
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 :(得分: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
我希望这会对你有所帮助!