I’m saving the cookie from a link before user sign up (http://localhost:3000/?ref=9). To create a user by Email, I’m using the code below, and this works:
class RegistrationsController < Devise::RegistrationsController
def build_resource(hash = {})
super
if cookies[:id] && referrer = (cookies[:id])
self.resource.referred_by = referrer
end
end
How I’m can Pass this ref to referred_by on sign Up with OmniAuth?
Below part of omniauth:
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def google_oauth2
@user = User.from_omniauth(request.env['omniauth.auth'])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication
set_flash_message(:notice, :success, :kind => "Google") if is_navigational_format?
elsif cookies[:id] && referrer = (cookies[:id])
self.resource.reference_institution = referrer
else
session['devise.google_data'] = request.env['omniauth.auth']
redirect_to new_user_registration_url
end
end
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, :confirmable
def self.from_omniauth(auth)
user = User.where(email: auth.info.email).first
if user
return user
else
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.fullname = auth.info.name
user.image = auth.info.image
user.uid = auth.uid
user.provider = auth.provider
user.skip_confirmation!
end
end
end
Ideas? Thanks!!!