Rails Devise Omniauth new_user_registration_url

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Rails Devise Omniauth new_user_registration_url相关的知识,希望对你有一定的参考价值。

我在几个月之前就已经在我的应用程序中实现了设计gem和omniauth-facebook,并且一切都像往常一样工作,直到现在我从db中删除了我的fb用户。现在我尝试再次登录为不存在的用户,并且我一直被重定向到Devise Sign Up表单。

所以我无法登录/注册我的FB用户到我的应用程序。

什么可能导致这类问题?

的routes.rb

  devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

omn​​iouth_callback_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

  def failure
    redirect_to root_path
  end
end

user.rb模型

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:facebook]

  def self.new_with_session(params, session)
    super.tap do |user|
      if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
        user.email = data["email"] if user.email.blank?
      end
    end
  end


  def self.from_omniauth(auth)
    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.name = auth.info.name   # assuming the user model has a name
      user.image = auth.info.image # assuming the user model has an image
    end
  end

铁路线

     new_user_session GET      /users/sign_in(.:format)                 devise/sessions#new
                    user_session POST     /users/sign_in(.:format)                 devise/sessions#create
            destroy_user_session DELETE   /users/sign_out(.:format)                devise/sessions#destroy
user_facebook_omniauth_authorize GET|POST /users/auth/facebook(.:format)           users/omniauth_callbacks#passthru
 user_facebook_omniauth_callback GET|POST /users/auth/facebook/callback(.:format)  users/omniauth_callbacks#facebook
               new_user_password GET      /users/password/new(.:format)            devise/passwords#new
              edit_user_password GET      /users/password/edit(.:format)           devise/passwords#edit
                   user_password PATCH    /users/password(.:format)                devise/passwords#update
                                 PUT      /users/password(.:format)                devise/passwords#update
                                 POST     /users/password(.:format)                devise/passwords#create
        cancel_user_registration GET      /users/cancel(.:format)                  devise/registrations#cancel
           new_user_registration GET      /users/sign_up(.:format)                 devise/registrations#new
          edit_user_registration GET      /users/edit(.:format)                    devise/registrations#edit
               user_registration PATCH    /users(.:format)                         devise/registrations#update
                                 PUT      /users(.:format)                         devise/registrations#update
                                 DELETE   /users(.:format)                         devise/registrations#destroy
                                 POST     /users(.:format)                         devise/registrations#create
答案

我在user.rb模型中找到了原因,我有一个关联belongs_to:somemodel,我被放在那里测试一些模型关系。

所以评论这条线我登录的是facebook!

以上是关于Rails Devise Omniauth new_user_registration_url的主要内容,如果未能解决你的问题,请参考以下文章

在 Rails 3 中设置环境变量(设计 + Omniauth)

如何使用 redux-token-auth 设置 devise_token_auth 和 omniauth?

Rails 4、Devise 和 Mandrill 电子邮件

Rails:无法通过Facebook验证您,因为“凭据无效”

Devise + Omniauth Facebook错误:Oauth异常代码101

在访问 Omniauth 路由之前验证设计用户