Rails 5:未保存Devise Oauth推荐参数

时间:2019-01-27 18:05:54

标签: ruby-on-rails oauth parameters devise

我有一个应用程序,用户可以使用电子邮件,facebook或google进行注册,也有一个小型的推荐系统,用户可以共享链接并获得积分。

现在,一切都可以完美地进行注册和登录。但是推荐系统仅在使用社交网络创建帐户时有效。

以下是我的代码:

omniauth_callbacks_controller.rb

class Users::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
# google callback
def google_oauth2
    @user = User.from_omniauth(request.env['omniauth.auth'])
    if @user.persisted?
        sign_in_and_redirect @user
        set_flash_message(:notice, :success, kind: 'Google') if is_navigational_format?
    else
        flash[:error] = 'There was a problem signing you in through Google. Please register or try signing in later.'
        redirect_to new_user_registration_url
    end 
end

def failure
    flash[:error] = 'There was a problem signing you in. Please register or try signing in later.' 
    redirect_to new_user_registration_url
end
end

registrations_controller.rb

def build_resource(hash = {})
  super

  if cookies[:referral_code] && referrer = User.find_by(referral_code: cookies[:referral_code])
    self.resource.referred_by = referrer
  end
end

user.rb

before_validation :set_referral_code
validates :referral_code, uniqueness: true
def set_referral_code
    loop do
        self.referral_code = SecureRandom.hex(6)
        break unless self.class.exists?(referral_code: referral_code)
    end
end

application_controller.rb

protect_from_forgery with: :exception

before_action :configure_permitted_parameters, if: :devise_controller?

before_action :set_referral_cookie
def set_referral_cookie
    if params[:ref]
      cookies[:referral_code] = {
        value: params[:ref],
        expires: 30.days.from_now,
      }
    end
end

我在此存储库的帮助下构建了系统

gorails referral system

编辑

Started GET "/?ref=6c28f9668715" for 127.0.0.1 at 2019-01-27 21:23:32 +0100
Processing by HomeController#index as HTML
  Parameters: {"ref"=>"6c28f9668715"}
  Rendering home/index.html.erb within layouts/application
  Rendered home/index.html.erb within layouts/application (0.6ms)
  Rendered shared/_navbar.html.erb (1.4ms)
Completed 200 OK in 125ms (Views: 90.7ms | ActiveRecord: 0.0ms)


Started GET "/users/sign_up" for 127.0.0.1 at 2019-01-27 21:23:34 +0100
Processing by Users::RegistrationsController#new as HTML
  User Load (1.6ms)  SELECT  "users".* FROM "users" WHERE "users"."referral_code" = $1 LIMIT $2  [["referral_code", "6c28f9668715"], ["LIMIT", 1]]
  ↳ app/controllers/users/registrations_controller.rb:7
  Rendering users/registrations/new.html.erb within layouts/application
  Rendered users/registrations/new.html.erb within layouts/application (6.7ms)
  Rendered shared/_navbar.html.erb (1.2ms)
Completed 200 OK in 300ms (Views: 142.0ms | ActiveRecord: 1.6ms)


Started GET "/users/auth/facebook" for 127.0.0.1 at 2019-01-27 21:23:37 +0100
I, [2019-01-27T21:23:37.266108 #67828]  INFO -- omniauth: (facebook) Request phase initiated.
Started GET "/users/auth/facebook" for 127.0.0.1 at 2019-01-27 21:23:38 +0100
I, [2019-01-27T21:23:38.488342 #67828]  INFO -- omniauth: (facebook) Request phase initiated.
Started GET "/users/auth/facebook/callback?code=AQAemLam_HRACk1q5NTfix5Sve6rA1fsDD5z_21vKpDlaTq4hfuXM2Oh_CThPspwk1BIg4Tjc1bm0UOcXLo_X0XGVI8XdLsirhPV6wKnGiCO3uU3l4y6y31qnhC1xjzd-21wx_cWVO-ipPCzrZ8kqWCdvQrxxKOQXMj10LsKlTAbuSqMEpx90XvcZw3RAYGLSiEFQGJSgCpABpboh_n_ewjTbfbTB01JATW6hM9Wy8iN1AQLpXrRgOZ-5P1NdowqdHdjU420N6QoB7R9tyHXegioQ47J8cjgCMUFwDPi_T--zHK6_-sIkW_QE6P5ryot1qHxzHpOASvx46WHvJun5_Yh&state=bd3dfbf7e7a5c83c42d1d754149d7be5ea7f39b6d5b99b28" for 127.0.0.1 at 2019-01-27 21:23:39 +0100
I, [2019-01-27T21:23:39.483990 #67828]  INFO -- omniauth: (facebook) Callback phase initiated.
Processing by Users::OmniauthCallbacksController#facebook as HTML
  Parameters: {"code"=>"AQAemLam_HRACk1q5NTfix5Sve6rA1fsDD5z_21vKpDlaTq4hfuXM2Oh_CThPspwk1BIg4Tjc1bm0UOcXLo_X0XGVI8XdLsirhPV6wKnGiCO3uU3l4y6y31qnhC1xjzd-21wx_cWVO-ipPCzrZ8kqWCdvQrxxKOQXMj10LsKlTAbuSqMEpx90XvcZw3RAYGLSiEFQGJSgCpABpboh_n_ewjTbfbTB01JATW6hM9Wy8iN1AQLpXrRgOZ-5P1NdowqdHdjU420N6QoB7R9tyHXegioQ47J8cjgCMUFwDPi_T--zHK6_-sIkW_QE6P5ryot1qHxzHpOASvx46WHvJun5_Yh", "state"=>"bd3dfbf7e7a5c83c42d1d754149d7be5ea7f39b6d5b99b28"}
  User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."provider" = $1 AND "users"."uid" = $2 ORDER BY "users"."id" ASC LIMIT $3  [["provider", "facebook"], ["uid", "311741132783400"], ["LIMIT", 1]]
  ↳ app/models/user.rb:60
  Disk Storage (5.7ms) Uploaded file to key: Sp8DiDxRWFn3uUfGTJJHdABc (checksum: tjmoDYRADOqNyXiWy90gxg==)
   (0.3ms)  BEGIN
  ↳ app/models/user.rb:66
  ActiveStorage::Blob Create (44.9ms)  INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["key", "Sp8DiDxRWFn3uUfGTJJHdABc"], ["filename", "avatar.jpg"], ["content_type", "image/jpeg"], ["metadata", "{\"identified\":true}"], ["byte_size", 1640], ["checksum", "tjmoDYRADOqNyXiWy90gxg=="], ["created_at", "2019-01-27 20:23:42.423862"]]
  ↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/bundler/gems/globalize-3fe2f93ab2d0/lib/patches/active_record/persistence.rb:12
   (0.9ms)  COMMIT
  ↳ app/models/user.rb:66
   (0.2ms)  BEGIN
  ↳ app/models/user.rb:66
   (0.2ms)  COMMIT
  ↳ app/models/user.rb:66
   (0.2ms)  BEGIN
  ↳ app/models/user.rb:60
  User Exists (0.4ms)  SELECT  1 AS one FROM "users" WHERE "users"."provider" = $1 AND "users"."uid" = $2 AND "users"."referral_code" = $3 LIMIT $4  [["provider", "facebook"], ["uid", "311741132783400"], ["referral_code", "2925f372e912"], ["LIMIT", 1]]
  ↳ app/models/user.rb:79
  User Exists (0.4ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "qubitam@gmail.com"], ["LIMIT", 1]]
  ↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/bundler/gems/globalize-3fe2f93ab2d0/lib/patches/active_record/rails5_1/uniqueness_validator.rb:38
  User Exists (0.3ms)  SELECT  1 AS one FROM "users" WHERE "users"."referral_code" = $1 LIMIT $2  [["referral_code", "2925f372e912"], ["LIMIT", 1]]
  ↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/bundler/gems/globalize-3fe2f93ab2d0/lib/patches/active_record/rails5_1/uniqueness_validator.rb:38
  User Create (2.5ms)  INSERT INTO "users" ("email", "encrypted_password", "name", "created_at", "updated_at", "provider", "uid", "referral_code") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["email", "qubitam@gmail.com"], ["encrypted_password", "$2a$11$JUxrDl1I4uDQwxi3yRVrnuqRJNYQJA8BcvLrmAj29nwBggJknrP3i"], ["name", "Abdelmoumin Mokhtari"], ["created_at", "2019-01-27 20:23:42.483417"], ["updated_at", "2019-01-27 20:23:42.483417"], ["provider", "facebook"], ["uid", "311741132783400"], ["referral_code", "2925f372e912"]]
  ↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/bundler/gems/globalize-3fe2f93ab2d0/lib/patches/active_record/persistence.rb:12
  ActiveStorage::Attachment Create (0.7ms)  INSERT INTO "active_storage_attachments" ("name", "record_type", "record_id", "blob_id", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["name", "image"], ["record_type", "User"], ["record_id", 13], ["blob_id", 10], ["created_at", "2019-01-27 20:23:42.487979"]]
  ↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/bundler/gems/globalize-3fe2f93ab2d0/lib/patches/active_record/persistence.rb:12
  User Update (41.5ms)  UPDATE "users" SET "updated_at" = $1 WHERE "users"."id" = $2  [["updated_at", "2019-01-27 20:23:42.490719"], ["id", 13]]
  ↳ app/models/user.rb:60
   (40.9ms)  COMMIT
  ↳ app/models/user.rb:60
[ActiveJob] Enqueued ActiveStorage::AnalyzeJob (Job ID: 1f7ce124-dc8b-4302-912e-7b1138656e35) to Async(default) with arguments: #<GlobalID:0x00007fa636abf0e8 @uri=#<URI::GID gid://classifyads/ActiveStorage::Blob/10>>
   (1.0ms)  BEGIN
  ↳ app/controllers/users/omniauth_callbacks_controller.rb:8
  ActiveStorage::Blob Load (0.4ms)  SELECT  "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 10], ["LIMIT", 1]]
  ↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
[ActiveJob] [ActiveStorage::AnalyzeJob] [1f7ce124-dc8b-4302-912e-7b1138656e35] Performing ActiveStorage::AnalyzeJob (Job ID: 1f7ce124-dc8b-4302-912e-7b1138656e35) from Async(default) with arguments: #<GlobalID:0x00007fa636aac790 @uri=#<URI::GID gid://classifyads/ActiveStorage::Blob/10>>
[ActiveJob] [ActiveStorage::AnalyzeJob] [1f7ce124-dc8b-4302-912e-7b1138656e35]   Disk Storage (0.2ms) Downloaded file from key: Sp8DiDxRWFn3uUfGTJJHdABc
[ActiveJob] [ActiveStorage::AnalyzeJob] [1f7ce124-dc8b-4302-912e-7b1138656e35] Skipping image analysis because the mini_magick gem isn't installed
[ActiveJob] [ActiveStorage::AnalyzeJob] [1f7ce124-dc8b-4302-912e-7b1138656e35]    (0.2ms)  BEGIN
[ActiveJob] [ActiveStorage::AnalyzeJob] [1f7ce124-dc8b-4302-912e-7b1138656e35]   ↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
[ActiveJob] [ActiveStorage::AnalyzeJob] [1f7ce124-dc8b-4302-912e-7b1138656e35]   ActiveStorage::Blob Update (0.6ms)  UPDATE "active_storage_blobs" SET "metadata" = $1 WHERE "active_storage_blobs"."id" = $2  [["metadata", "{\"identified\":true,\"analyzed\":true}"], ["id", 10]]
[ActiveJob] [ActiveStorage::AnalyzeJob] [1f7ce124-dc8b-4302-912e-7b1138656e35]   ↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/bundler/gems/globalize-3fe2f93ab2d0/lib/patches/active_record/persistence.rb:7
  User Update (82.8ms)  UPDATE "users" SET "sign_in_count" = $1, "current_sign_in_at" = $2, "last_sign_in_at" = $3, "current_sign_in_ip" = $4, "last_sign_in_ip" = $5, "updated_at" = $6 WHERE "users"."id" = $7  [["sign_in_count", 1], ["current_sign_in_at", "2019-01-27 20:23:42.581519"], ["last_sign_in_at", "2019-01-27 20:23:42.581519"], ["current_sign_in_ip", "127.0.0.1/32"], ["last_sign_in_ip", "127.0.0.1/32"], ["updated_at", "2019-01-27 20:23:42.584122"], ["id", 13]]
  ↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/bundler/gems/globalize-3fe2f93ab2d0/lib/patches/active_record/persistence.rb:7
[ActiveJob] [ActiveStorage::AnalyzeJob] [1f7ce124-dc8b-4302-912e-7b1138656e35]    (61.8ms)  COMMIT
[ActiveJob] [ActiveStorage::AnalyzeJob] [1f7ce124-dc8b-4302-912e-7b1138656e35]   ↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
[ActiveJob] [ActiveStorage::AnalyzeJob] [1f7ce124-dc8b-4302-912e-7b1138656e35] Performed ActiveStorage::AnalyzeJob (Job ID: 1f7ce124-dc8b-4302-912e-7b1138656e35) from Async(default) in 122.16ms
   (79.1ms)  COMMIT
  ↳ app/controllers/users/omniauth_callbacks_controller.rb:8
Redirected to http://localhost:3000/users/edit
Completed 302 Found in 2383ms (ActiveRecord: 297.1ms)


Started GET "/users/edit" for 127.0.0.1 at 2019-01-27 21:23:42 +0100
Processing by Users::RegistrationsController#edit as HTML
  User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 13], ["LIMIT", 1]]
  ↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
  Rendering users/registrations/edit.html.erb within layouts/application
  ActiveStorage::Attachment Load (0.6ms)  SELECT  "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4  [["record_id", 13], ["record_type", "User"], ["name", "image"], ["LIMIT", 1]]
  ↳ app/views/users/registrations/edit.html.erb:8
  ActiveStorage::Blob Load (0.9ms)  SELECT  "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 10], ["LIMIT", 1]]
  ↳ app/views/users/registrations/edit.html.erb:8
  Rendered users/registrations/edit.html.erb within layouts/application (14.4ms)
  Rendered shared/_navbar.html.erb (3.9ms)
Completed 200 OK in 105ms (Views: 100.1ms | ActiveRecord: 2.2ms)

0 个答案:

没有答案