Rails4 ログイン認証 Devise omniauth-facebook <% if f.object.password_required? %>が効いていない
ログイン認証で下記のgemを使用しています。
・Devise
・omniauth-facebook
omniauth-facebook認証時にuser_nameを追加で入力させたいです。
user_nameカラムを追加し、viewに
<%= f.text_field :user_name, autofocus: true, class: "form-control login-field",placeholder: "ユーザネーム",id: "login-name" %>
</div>
を追加すると、user_name以外に、パスワードの入力を求められるようになりました。
パスワードを入力すると通常の登録はできますがfacebook認証となりません(uid,nameなどが取れていない状態)
facebook認証時にはpassword入力を求めないようにpassword_required?メソッドcontrollerに追加し、viewには<% if f.object.password_required? %>を追加しましたが
動作しませんでした。
facebook認証時user_nameのみを入力項目とし、パスワードの入力を求めないように
実装するにはどうしたら良いでしょうか
因みにuser_nameカラムと
<%= f.text_field :user_name, autofocus: true, class: "form-control login-field",placeholder: "ユーザネーム",id: "login-name" %>
</div>
を追加する前はワンクリックでfacebook認証可能でした。
#user.rb
validates :user_name, presence: true, uniqueness: true
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
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
def self.current_user
Thread.current[:current_user]
end
def password_required?
super && provider.blank?
end
#omniauth_callbacks_controller.rb
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
end
#registrations/new.html.erb
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<div class="login-form">
<p>新規登録</p>
<div class="form-group">
<%= link_to user_omniauth_authorize_path(:facebook) do%>
<div class="btn-sm btn-block btn-social btn-facebook">
<i class="fa fa-facebook"></i> Facebookで登録
</div>
<% end %>
</div>
<div class="form-group">
<label class="login-field-icon fui-mail" for="user_name"></label>
<%= f.text_field :user_name, autofocus: true, class: "form-control login-field",placeholder: "ユーザネーム",id: "login-name" %>
</div>
<% if f.object.password_required? %>
<div class="form-group">
<label class="login-field-icon fui-mail" for="email"></label>
<%= f.email_field :email, autofocus: true, class: "form-control login-field",placeholder: "Email",id: "login-name" %>
</div>
<div class="form-group">
<label class="login-field-icon fui-lock" for="login-pass"></label>
<%= f.password_field :password, autocomplete: "off", class: "form-control login-field",placeholder: "Password",id: "login-pass" %>
</div>
<div class="form-group">
<label class="login-field-icon fui-lock" for="login-pass"></label>
<%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control login-field",placeholder: "Password確認用",id: "login-pass" %>
</div>
<p class="palette-paragraph">半角英数字8文字以上</p>
<% end %>
<br>
<div class="form-group text-center">
<%= f.submit "登録する", class: 'btn btn-embossed btn-sm btn-block btn-warning ' %>
</div>
</div>
<% end %>
サーバログ
Started GET "/users/auth/facebook/callback?code=AQCb50YD18DG8vYG0h6gBQl-tjdoi2bupij4e138eFhfrwSXS_lIShlaJ27AM3TRQQHozaE7yI4zYgjI5Pv11Ojv8HvamGEn5pTvo4obImWbmq41QBbERe-8LUBWZuwdFCa2iwj8MmV4V4eAD678L_YfaUiYZUOII6CJOcrOcjs-rVFXOMK5Ssi8SvZtEDyqRg535Zz5aVg8DdErest8tl1nCTQETxX526LhwXoFYvb7sWlpAgb3m1nlCz7nCAbTdpAqe8uHk9-p02IfUBFvFDv4x1qb2x_DT7D64eouxae3KgonR4z7sssmfKie-BKs43c&state=2e2edfb450620fdb442d92acad47e24723fc04582d073264" for 127.0.0.1 at 2015-04-30 01:43:29 +0900
Processing by Users::OmniauthCallbacksController#facebook as HTML
Parameters: {"code"=>"AQCb50YD18DG8vYG0h6gBQl-tjdoi2bupij4e138eFhfrwSXS_lIShlaJ27AM3TRQQHozaE7yI4zYgjI5Pv11Ojv8HvamGEn5pTvo4obImWbmq41QBbERe-8LUBWZuwdFCa2iwj8MmV4V4eAD678L_YfaUiYZUOII6CJOcrOcjs-rVFXOMK5Ssi8SvZtEDyqRg535Zz5aVg8DdErest8tl1nCTQETxX526LhwXoFYvb7sWlpAgb3m1nlCz7nCAbTdpAqe8uHk9-p02IfUBFvFDv4x1qb2x_DT7D64eouxae3KgonR4z7sssmfKie-BKs43c", "state"=>"2e2edfb450620fdb442d92acad47e24723fc04582d073264"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL AND "users"."provider" = 'facebook' AND "users"."uid" = '10204167973454999' ORDER BY "users"."id" ASC LIMIT 1
(0.1ms) begin transaction
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'xxxxxxxxx@yahoo.co.jp' LIMIT 1
User Exists (0.1ms) SELECT 1 AS one FROM "users" WHERE "users"."user_name" IS NULL LIMIT 1
(0.0ms) rollback transaction
Redirected to http://0.0.0.0:3000/users/sign_up
Completed 302 Found in 122ms (ActiveRecord: 2.9ms)
Started GET "/users/sign_up" for 127.0.0.1 at 2015-04-30 01:43:30 +0900
Processing by Devise::RegistrationsController#new as HTML
Rendered devise/registrations/new.html.erb within layouts/application (3.6ms)
Completed 200 OK in 37ms (Views: 31.0ms | ActiveRecord: 0.2ms)