環境
rails 6.0.2.1
ruby 2.6.3
【状況】
現在、こちらhttps://qiita.com/mailok1212/items/74e6dae08c1bafb874ecのURLに沿ってFacebookをつかったログイン機能を実装中です。
<%= link_to "facebook", user_facebook_omniauth_authorize_path %>をhttp://localhost:3000/loginを表示するhtmlファイルに貼り付けたところ、以下のエラーが起こり、facebookログインへのリンクがエラーを起こしているようです。
【予想しているエラーの原因】
route.dbでusersの定義をしていないのが原因で、link_toメソッドの第二引数user_facebook_omniauth_authorize_pathが動いていない、というのが見解です。
他に原因、解決法、間違っている場所、アドバイスがありましたらご教授ください。
以下、作業設定したファイルです。
new.html.erb(http://localhost:3000/loginを表示するhtmlファイル)↓
<% provide(:title, "Log in") %> <h1>Log in</h1> <div class="row"> <div class="col-md-6 col-md-offset-3"> <%= form_for(:session, url: login_path) do |f| %> <%= f.label :email %> <%= f.email_field :email, class: 'form-control' %> <%= f.label :password %> <%= f.password_field :password, class: 'form-control' %> <%= f.submit "Log in", class: "btn btn-primary" %> <% end %> <p>New user? <%= link_to "Sign up now!", signup_path %></p> <%= link_to "facebook", user_facebook_omniauth_authorize_path %> </div> </div>
route.db↓
Rails.application.routes.draw do devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' } root 'static_pages#home' get '/help', to: 'static_pages#help' get '/about', to: 'static_pages#about' get '/contact', to: 'static_pages#contact' get '/signup', to: 'users#new' get '/login', to: 'sessions#new' post '/login', to: 'sessions#create' delete '/logout', to: 'sessions#destroy' resources :users end
omniauth_callbacks_controller.rb↓
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController def facebook callback_from :facebook end private def callback_from(provider) provider = provider.to_s @user = User.find_for_oauth(request.env['omniauth.auth']) if @user.persisted? flash[:notice] = I18n.t('devise.omniauth_callbacks.success', kind: provider.capitalize) sign_in_and_redirect @user, event: :authentication else session["devise.#{provider}_data"] = request.env['omniauth.auth'] redirect_to new_user_registration_url end end end
user.rb↓
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :omniauthable def self.find_for_oauth(auth) user = User.where(uid: auth.uid, provider: auth.provider).first unless user user = User.create( uid: auth.uid, provider: auth.provider, email: auth.info.email, password: Devise.friendly_token[0, 20] ) end user end validates :name, presence: true, length: { maximum: 50 } VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } has_secure_password validates :password, presence: true, length: { minimum: 6 }, allow_nil: true def User.digest(string) cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost BCrypt::Password.create(string, cost: cost) end end
devise.rb
config.omniauth :facebook, FACEBOOK_ID='2884717221754622', FACEBOOK_SECRET_KEY='7ca97273c02f492218f08ea91f640d93' end
あなたの回答
tips
プレビュー