我在我的Rails 3.2应用程序上运行了Devise.Google Oauth用于登录.
新用户尝试使用Google登录,并在未登录的情况下重定向到登录页面.我检查数据库,并使用正确的凭据创建用户帐户(一切都正确,但IP除外).
返回用户登录时没有问题,退出也很有效.
我不确定我做错了什么,我遵循了这个教程:http://blogs.burnsidedigital.com/2013/03/rails-3-devise-omniauth-and-google/
不同之处在于我不需要额外的用户信息,我只想通过他们的谷歌帐户验证用户,确保他们属于某个域 foobar.com
我有一个OmniAuth控制器,如下所示:
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def google_oauth2
auth = request.env["omniauth.auth"]
proceeder = !!(auth.info.email =~ /^[a-zA-Z0-9.]+@foobar\.com$/)
if proceeder
user = User.from_omniauth(auth)
flash.notice = "Signed in!"
sign_in_and_redirect user
redirect_to :root
else
flash[:notice] = "You must use an email ending in @foobar.com"
redirect_to signup_path
end
Run Code Online (Sandbox Code Playgroud)
我的用户模型如下:
class User < ActiveRecord::Base
has_many :posts
has_many :comments
has_many :votes
has_many :reports
devise :database_authenticatable, :registerable, :omniauthable,
:recoverable, :rememberable, :trackable, :validatable#, :omniauth_providers => [:google_oauth2]
attr_accessible :email, :password, …Run Code Online (Sandbox Code Playgroud)