Devise + Omniauth Facebook redirecting to signup

让人想犯罪 __ 提交于 2019-12-13 15:59:24

问题


I have followed this guide spot on https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview

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

I cleared browser history and tested a different user.

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

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

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

Routes

 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

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

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

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

回答1:


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



回答2:


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

I had exactly the same response. I was running the server locally and looking at the logs, it said it couldn't authenticate the client.

Turns out, it was because I had set the ENV keys locally using export FACEBOOK_APP_SECRET='xxxxxxx' which only works if you then run the server from the same tab in your terminal.

Things to try:

  1. If using exports to set the env key - run $bin/rails s from the same tab. Can check if the key has been set by typing in $printenv in the terminal.
  2. Set env key to the bash profile. Clear instructions from osx daily here.
  3. Use dotenv module to manage keys.

All the best.



来源:https://stackoverflow.com/questions/24963840/devise-omniauth-facebook-redirecting-to-signup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!