質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Q&A

解決済

1回答

1039閲覧

Rails SNS認証機能

r.h

総合スコア3

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

0グッド

0クリップ

投稿2021/12/13 11:06

編集2021/12/14 13:37

問題点

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']の環境変数定義できてます。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

gem 'omniauth-rails_csrf_protection'
を追加後リンクタグに,method: :postを指定したことで改善しました。
400エラーが出てしまってますが頑張ります!

投稿2021/12/16 09:29

r.h

総合スコア3

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問