我想使用身份提供程序为我的应用程序使用单独的管理员登录。
我已经在 config/initializers/omniauth.rb 中写了这个
Rails.application.config.middleware.use OmniAuth::Builder do
provider :identity, :model => Credential, :on_failed_registration =>SessionsController.action(:register)
provider :identity, :model => Credential, :name => 'admin', :on_failed_registration => SessionsController.action(:login_admin)
provider :google_oauth2, '000000000.apps.googleusercontent.com', '00000000000'
end
在 config/routes.rb
match '/auth/admin/callback', :to => 'sessions#authenticate_admin'
在 app/controllers/sessions_controller.rb
def authenticate_admin
auth_hash = request.env['omniauth.auth']
session[:admin_user] = auth_hash['user_info']['email']
if admin?
redirect_to '/'
else
render :text => '401 Unauthorized', :status => 401
end
end
但是当我尝试访问 request.env['omniauth.auth'] 时,它总是为零。虽然在 sessison#create 操作中为普通用户使用默认回调时可以访问它。我只想知道这段代码中是否遗漏了什么。我正在关注这个博客http://www.intridea.com/blog/2011/1/31/easy-rails-admin-login-with-google-apps-and-omniauth。