首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Rails设计Omniauth new_user_registration_url

Rails设计Omniauth new_user_registration_url
EN

Stack Overflow用户
提问于 2017-12-20 19:50:38
回答 1查看 334关注 0票数 0

几个月前,我已经实现了devise gem和omniauth-facebook,一切都像往常一样,直到现在我从数据库中删除了我的fb用户。现在我试图再次作为不存在的用户登录,所有时间我都被重定向到设计注册表单。

因此,我无法登录/注册我的FB用户到我的应用程序。

是什么导致了这种类型的问题?

routes.rb

代码语言:javascript
运行
复制
  devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

omniouth_callback_controller.rb

代码语言:javascript
运行
复制
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模型

代码语言:javascript
运行
复制
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

rails路由

代码语言:javascript
运行
复制
     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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-20 21:33:06

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

所以评论这一行,我是通过facebook登录的!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47905428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档