https://github.com/plataformatec/devise/wiki/OmniAuth:-Overviewのこのガイド スポットに従いました。
最初のラウンドで認証され、サインインし、db に保存されました。
ブラウザの履歴を消去し、別のユーザーをテストしました。
新規ユーザーはサインインするために facebook ページに移動し、サインイン後に自動的にリダイレクトされます。
http://localhost:3000/users/sign_up#_=_
サインインしたり、DB に保存したりしません。何か案は?
ルート
get "static/home"
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
resources :users
root 'static#home'
ユーザー.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) %>