問題点
deviseとomniauth-google-oauth2を使ったSNS認証機能を実装しているのですが、リンクの遷移先で
Not found. Authentication passthru.
と出てしまいます。
該当のソースコード
config/initializers/devise.rb
Devise.setup do |config| config.omniauth :google_oauth2,ENV['GOOGLE_CLIENT_ID'],ENV['GOOGLE_CLIENT_SECRET']
routes.rb
devise_for :customers,skip: [:passwords,], controllers: { omniauth_callbacks: 'customer/omniauth_callbacks', registrations: "customer/registrations", sessions: 'customer/sessions' }
controllers/customer/omniauth_callbacks_controller.rb
class Customer::OmniauthCallbacksController < Devise::OmniauthCallbacksController def google_oauth2 # You need to implement the method below in your model (e.g. app/models/.rb) @customer = Customer.from_omniauth(request.env['omniauth.auth']) if @customer.persisted? flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Google' sign_in_and_redirect @customer, event: :authentication else session['devise.google_data'] = request.env['omniauth.auth'].except(:extra) # Removing extra as it can overflow some session stores redirect_to new_customer_registration_url, alert: @customer.errors.full_messages.join("\n") end end end
models/customer.rb
class Customer < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :omniauthable, omniauth_providers: [:google_oauth2] def self.from_omniauth(access_token) data = access_token.info customer = Customer.where(email: data['email']).first # Uncomment the section below if you want customers to be created if they don't exist # unless customer # customer = Customer.create(name: data['name'], # email: data['email'], # password: Devise.friendly_token[0,20] # ) # end customer end end
schema.rb
create_table "customers", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.string "last_name", null: false t.string "first_name", null: false t.string "last_name_kana", null: false t.string "first_name_kana", null: false t.boolean "is_deleted", default: false, null: false t.string "customer_image_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "provider" t.string "uid" t.index ["email"], name: "index_customers_on_email", unique: true t.index ["reset_password_token"], name: "index_customers_on_reset_password_token", unique: true end
views/customer/registrations/new.html.erb
<%= link_to "Sign in with Google", customer_google_oauth2_omniauth_authorize_path %>
試したこと
https://qiita.com/bino98/items/596b5cffeca7c104bd90
https://qiita.com/akioneway94/items/35641ad30c2acb23b562
https://qiita.com/manbolila/items/8caa1f5d2b1fb96d2646
いくつかのサイトを参考にしながらやってみましたがダメでした…
正直動作の理解ができていないのでどこを修正するべきかも考えられていない状況です。
どなたかご教授いただけないでしょうか…
補足情報
rails 5.2.5
ruby 2.6.3
gem 'omniauth-google-oauth2'
gem 'dotenv-rails'
.envファイルにENV['GOOGLE_CLIENT_ID'],ENV['GOOGLE_CLIENT_SECRET']の環境変数定義できてます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。