简体   繁体   English

我如何知道用户何时注册与在 Rails 中使用 Devise 和 Omniauth 登录

[英]How do I know when a user is registering vs logging in with Devise and Omniauth in Rails

I'm using Rails 6 + Devise for login/registration.我正在使用 Rails 6 + Devise 进行登录/注册。 Users can register/login with Facebook via omniauth.用户可以通过omniauth注册/登录Facebook。

I want to log one analytics event when the user logs in, and a different analytics event when they register for the first time.我想在用户登录时记录一个分析事件,并在他们第一次注册时记录一个不同的分析事件。

app/controllers/users/omniauth_callbacks_controller.rb应用程序/控制器/用户/omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    @user = User.from_omniauth(request.env["omniauth.auth"])

    if @user.persisted?
      sign_in_and_redirect @user, :event => :authentication
      set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
      flash[:log_event] = {
        'event_category' => 'engagement',
        'event_name' => 'login',
        'method' => 'facebook'
      }
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end

  def failure
    redirect_to root_path
  end

end

The flash[:log_event] is passed to Google Analytics. flash[:log_event]被传递给 Google Analytics。 My problem is that Devise seems to follow the same code path for first registration as it does for a regular login.我的问题是 Devise 似乎遵循与常规登录相同的代码路径进行首次注册。

I suppose I could check the @user.created_at timestamp, and treat it as a registration if it's a couple of minutes old, but I'm sure there's a cleaner solution.我想我可以检查@user.created_at时间戳,如果它是几分钟前的,则将其视为注册,但我确信有一个更清洁的解决方案。

You can always make your own version of User.from_omniauth .您始终可以制作自己的User.from_omniauth版本。 Notice the first_or_initialize instead of ..._create注意first_or_initialize而不是..._create

# in app/models/user.rb

def self.from_omniauth(auth)
  user = where(auth.slice(:provider, :uid)).first_or_initialize do |new_user|
    new_user.provider = auth.provider
    new_user.uid = auth.uid
    new_user.username = auth.info.nickname
  end
  user.image_url = auth.info.image # need to update in case image changed on provider's site
  user
end

Then in your controller check for new_record?然后在您的 controller 检查new_record?

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

if @user.new_record?
  @user.save  # Important step I missed earlier
  # ... Do some custom thing here for your app
end

if @user.persisted?
# ....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Omniauth和Devise为用户登录Rails应用程序创建密码 - Create password for user logging into Rails app using Omniauth and Devise 如何在已有的omniauth和devise rails app上设置omniauth-facebook? - How do I setup omniauth-facebook on an already existing omniauth and devise rails app? Rails &amp; Devise - 如何确定用户何时注销? - Rails & Devise - how to determine when a user is logging out? 当用户使用Rails设计的社交媒体帐户登录时,如何显示omniauthlinkedin Flash消息 - how to show the omniauth linkedin flash message when user signin with social media account with rails devise Omniauth,Devise,Open ID,CanCan - 什么是什么以及何时使用哪个解决方案用于Rails API应用程序 - Omniauth, Devise, Open ID, CanCan - Whats what and When do I use which solution for a Rails API app 使用Rails Omniauth gem和Google OpenID时,如何不要求用户发送电子邮件 - How do I NOT require user's email when using Rails Omniauth gem and Google OpenID 如何确定Rails中正在使用哪个身份验证用户(OmniAuth) - How do I tell which authentication user is using in rails (OmniAuth) 如何将使用omniauth(Rails with Devise)注册的用户重定向到特定页面? - How to redirect a user signed up with omniauth (Rails with Devise) to a specific page? Rails 3和Devise:Omniauth vs. Facebook Connect - Rails 3 and Devise: Omniauth vs. Facebook Connect Rails 3和设计,在用户出现错误时设计注销 - Rails 3 and devise, devise logging out when there are errors on user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM