Rails 6.0.3
Ruby 2.7.1
without devise

I am a beginner creating a web application with Rails. I originally created an authentication function that does not use divise, and tried to add an SNS authentication function, but it does not work.
I tried to set the Callback URL on twitter, but I can't register because it is said to be invalid if it is local.
I could register the URL of the production environment, but when I access/auth/twitter, I get a 404 error.

I also tried google, but when I access/auth/google_oauth2 locally, I get the error No route matches [GET] "/ auth/google_oauth2".

It was rails 5 series at the time of creating the application, but I updated to rails 6 on the way. At that time, there may have been something that wasn't going well.

Applicable source code

* It is actually entered in the<>part.

Rails.application.config.middleware.use OmniAuth :: Builder do
  provider: google_oauth2, ENV ['GOOGLE_CLIENT_ID'], ENV ['GOOGLE_CLIENT_SECRET']
  provider: twitter, ENV ["TWITTER_KEY"], ENV ["TWITTER_SECRET"]
oauth (0.5.4)
    oauth2 (1.4.4)
      faraday (>= 0.8,<2.0)
      jwt (>= 1.0,<3.0)
      multi_json (~>1.3)
      multi_xml (~>0.5)
      rack (>= 1.2,<3)
    omniauth (1.9.1)
      hashie (>= 3.4.6)
      rack (>= 1.6.2,<3)
    omniauth-google-oauth2 (0.8.0)
      jwt (>= 2.0)
      omniauth (>= 1.1.1)
      omniauth-oauth2 (>= 1.6)
    omniauth-oauth (1.1.0)
      omniauth (~>1.0)
    omniauth-oauth2 (1.6.0)
      oauth2 (~>1.1)
      omniauth (~>1.9)
    omniauth-rails_csrf_protection (0.1.2)
      actionpack (>= 4.2)
      omniauth (>= 1.3.1)
    omniauth-twitter (1.4.0)
      omniauth-oauth (~>1.1)
  get '/ login', to: 'sessions # new'
  post '/ login', to: 'sessions # create'
  get 'auth /: provider/callback', to: 'sessions # create'
  get 'auth/failure', to: redirect ('/')
  delete '/ logout', to: 'sessions # destroy'
What I tried

I have set the environment variables for the production environment.
I also restarted the local server. ‥
I reviewed the settings on twitter and google while looking at some sites, but I could not find any problems.
There may be some mistakes regarding twitter because the UI has changed.

When I tried new rails new and connected to with the same code, I was successfully redirected to the google authentication page.
What is causing the route error?

  • Answer # 1

    gem 'omniauth-rails_csrf_protection'

    After removing the above gem, the redirect was successful.

Related articles