我是新手在轨道上,试图让我的登录工作使用设计宝石和Omniauth,来自this tutorial Here的facebook我认为问题出在我的routes.rb文件中但我不能发现它,你能发现它吗?
这是我的routes.rb文件
Rails.application.routes.draw do
resources :requests
mount RailsAdmin::Engine => '/rabbit', as: 'rails_admin'
devise_for :users, class_name: 'FormUser', :controllers => { omniauth_callbacks: 'omniauth_callbacks',
registrations: 'registrations'}
#devise_for :users
devise_scope :user do
get '/users/auth/:provider/upgrade' => 'omniauth_callbacks#upgrade', as: :user_omniauth_upgrade
get '/users/auth/:provider/setup', :to => 'omniauth_callbacks#setup'
end
resources :listings do
resources :reviews, except: [:show, :index] do
put "upvote", to: "reviews#upvote"
put "downvote", to: "reviews#downvote"
resources :user
end
end
get 'pages/about'
get 'pages/how'
get 'pages/faqs'
get 'pages/contact'
get 'pages/privacy'
get 'pages/tos'
get 'pages/guidelines'
root 'listings#index'
resources :notifications do
collection do
post :mark_as_read
end
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

这是我的omniauth_callback_controller.rb
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def instagram
generic_callback( 'instagram' )
end
def facebook
generic_callback( 'facebook' )
end
def twitter
generic_callback( 'twitter' )
end
def google_oauth2
generic_callback( 'google_oauth2' )
end
def generic_callback( provider )
@identity = Identity.find_for_oauth env["omniauth.auth"]
@user = @identity.user || current_user
if @user.nil?
@user = User.create( email: @identity.email || "" )
@identity.update_attribute( :user_id, @user.id )
end
if @user.email.blank? && @identity.email
@user.update_attribute( :email, @identity.email)
end
if @user.persisted?
@identity.update_attribute( :user_id, @user.id )
# This is because we've created the user manually, and Device expects a
# FormUser class (with the validations)
@user = FormUser.find @user.id
sign_in_and_redirect @user, event: :authentication
set_flash_message(:notice, :success, kind: provider.capitalize) if is_navigational_format?
else
session["devise.#{provider}_data"] = env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end
&#13;
这是我的错误
答案 0 :(得分:1)
必须调用该文件omniauth_callbacks_controller.rb
。现在,它被称为omniauth_callback_controller.rb
。
答案 1 :(得分:1)
我最近在我的应用程序中添加了facebook身份验证。这是设置代码的方式..
我的网址是 - my-localhost/users/auth/facebook
######in routes
devise_for :users,:controllers => {
:sessions => "users/sessions",
:registrations => "users/registrations",
:passwords => "users/passwords",
:confirmations => "users/confirmations",
omniauth_callbacks: 'users/omniauth_callbacks'
}
####in controllers/users/omniauth_callbacks_controller.rb
def facebook
logger.tagged("FACEBOOK LOGIN"){ logger.info "=====Login with facebook================"}
auth = request.env["omniauth.auth"]
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.find_for_facebook_oauth(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"]
flash[:error] = "Unable to login from Facebook because of these errors:"
flash[:error] << "#{@user.errors.full_messages.to_sentence}"
flash[:error] << ".Kindly signup"
redirect_to new_user_registration_url
end
end
####in my user.rb
class << self
##============for facebook
def find_for_facebook_oauth(auth)
@facebook_user = where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.username = auth.info.name
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
##using Koala gem to fetch address too
@graph = Koala::Facebook::API.new(auth.credentials.token,configatron.fb_app_secret)
##Check more permissions at https://developers.facebook.com/docs/facebook-login/permissions/v2.2
user_location = @graph.get_object("me?fields=first_name,last_name,email,location")
end
return @facebook_user
end
end
希望有所帮助:)