Devise + Omniauth Facebook重定向到注册 [英] Devise + Omniauth Facebook redirecting to signup

查看:248
本文介绍了Devise + Omniauth Facebook重定向到注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已遵循 https://github.com/plataformatec/这个指南devise / wiki / OmniAuth:-Overview

首先,它验证了我,签了名,并将我保存到db。

First round it authenticated me, signed me in, and saved me to db.

我清除浏览器历史记录并测试了其他用户。

I cleared browser history and tested a different user.

它需要新用户到Facebook页面登录,登录后自动重定向到

It takes new user to facebook page to sign in, and after sign in automatically redirects them to

 http://localhost:3000/users/sign_up#_=_

并且不登录或保存到DB。任何想法?

and does not sign them in or save to DB. Any Ideas?

路线

 get "static/home"
 devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
 resources :users

 root 'static#home'

User.rb

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

def self.from_omniauth(auth)
    where(auth.slice(:provider, :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


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



 protected 
    def password_required? 
    true 
 end 




end

devise.rb

devise.rb

config.omniauth :facebook, "#######", "################"

omn​​iauth_callbacks

omniauth_callbacks

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 #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"].except('extra')
      redirect_to new_user_registration_url
    end
  end
end

home.html.erb

home.html.erb

<%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>


推荐答案

下面的代码重定向他。原来只是他的Facebook帐号。需要知道他的帐户为什么这样做...

The code below is redirecting him. Turns out it is only his facebook account. Need to know why his account is doing that...

 if @user.persisted?
      sign_in_and_redirect @user #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"].except('extra')
      redirect_to new_user_registration_url
 end

这篇关于Devise + Omniauth Facebook重定向到注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆