■環境
OS: macOS Big Sur(M1 mac) 11.5.2
Ruby: 3.0.0p0 (2020-12-25 revision 95aff21468) [arm64-darwin20]
Rails: 6.1.4.1
■背景
deviseを使用せず、Google認証で新規登録・ログイン画面を作成しようとしているのですが、
routeが通らず、コールバックされない状況です。
OAuthクライアントIDの作成や承認済みURIの設定は完了しているのですが、
何が原因でrouteエラーになっているのでしょうか?
■補足
「deviseなしでGoogleOAuth認証のサインインの実装」を参考にGoogle Developers Consoleの登録とRailsを実装しました。
■エラー内容: development.log
ruby
1ActionController::RoutingError (No route matches [GET] "/auth/google_oauth2"):
■Gemfile
Ruby
1source 'https://rubygems.org' 2git_source(:github) { |repo| "https://github.com/#{repo}.git" } 3 4ruby '3.0.0' 5 6# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' 7gem 'rails', '~> 6.1.4', '>= 6.1.4.1' 8# Use sqlite3 as the database for Active Record 9gem 'sqlite3', '~> 1.4' 10# Use Puma as the app server 11gem 'puma', '~> 5.0' 12# Use SCSS for stylesheets 13gem 'sass-rails', '>= 6' 14# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker 15gem 'webpacker', '~> 5.0' 16# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks 17gem 'turbolinks', '~> 5' 18# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 19gem 'jbuilder', '~> 2.7' 20# Use Redis adapter to run Action Cable in production 21# gem 'redis', '~> 4.0' 22# Use Active Model has_secure_password 23# gem 'bcrypt', '~> 3.1.7' 24 25# Use Active Storage variant 26# gem 'image_processing', '~> 1.2' 27 28# Reduces boot times through caching; required in config/boot.rb 29gem 'bootsnap', '>= 1.4.4', require: false 30 31gem 'omniauth-google-oauth2' 32 33group :development, :test do 34 # Call 'byebug' anywhere in the code to stop execution and get a debugger console 35 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] 36 gem 'dotenv-rails' 37end 38 39group :development do 40 # Access an interactive console on exception pages or by calling 'console' anywhere in the code. 41 gem 'web-console', '>= 4.1.0' 42 # Display performance information such as SQL time and flame graphs for each request in your browser. 43 # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md 44 gem 'rack-mini-profiler', '~> 2.0' 45 gem 'listen', '~> 3.3' 46 # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 47 gem 'spring' 48 gem 'better_errors' 49end 50 51group :test do 52 # Adds support for Capybara system testing and selenium driver 53 gem 'capybara', '>= 3.26' 54 gem 'selenium-webdriver' 55 # Easy installation and use of web drivers to run system tests with browsers 56 gem 'webdrivers' 57end 58 59# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 60gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
■config/routes.rb
ruby
1Rails.application.routes.draw do 2 root "posts#index" 3 get 'auth/:provider/callback', to: 'sessions#create' 4 get 'auth/failure', to: redirect('/') 5 get 'signout', to: 'sessions#destroy', as: 'signout' 6 7 resources :sessions, only: %i(new create destroy) 8 resources :posts, only: %i(index) 9end
■app/models/user.rb
ruby
1class User < ApplicationRecord 2 def self.from_omniauth(auth) 3 where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user| 4 user.provider = auth.provider 5 user.uid = auth.uid 6 user.name = auth.info.name 7 user.email = auth.info.email 8 user.token = auth.credentials.token 9 user.expires_at = Time.at(auth.credentials.expires_at) 10 return user 11 end 12 end 13end
■app/controllers/sessions_controller.rb
ruby
1class SessionsController < ApplicationController 2 def new 3 end 4 5 def create 6 user_data = request.env["omniauth.auth"] 7 user = User.from_omniauth(request.env["omniauth.auth"]) 8 if user.save 9 session[:user_id] = user.id 10 redirect_to root_path 11 else 12 redirect_to new_session_path 13 end 14 end 15 16 def destroy 17 session[:user_id] = nil 18 redirect_to new_session_path 19 end 20end
■app/controllers/application_controller.rb
ruby
1class ApplicationController < ActionController::Base 2 helper_method :current_user 3 4 def current_user 5 User.find(session[:user_id]) if session[:user_id] 6 end 7end
■app/views/posts/index.html.erb
ruby
1<h1>Post#index</h1> 2<p>Find me in app/views/posts/index.html.erb</p> 3<%= link_to "Sign in with Google", "/auth/google_oauth2", id: "sign_in" %>
■config/initializers/omniauth.rb
ruby
1Rails.application.config.middleware.use OmniAuth::Builder do 2 provider :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"] 3end
■.env
ruby
1GOOGLE_CLIENT_ID="======================" 2GOOGLE_CLIENT_SECRET="=================="
あなたの回答
tips
プレビュー