BeccaN
BeccaN

Reputation: 121

"No route matches [GET] "/auth/google_oauth2" error keeps coming up

Error Message: "No route matches [GET] "/auth/google_oauth2"

View page link: <%= link_to "Log In with Google", "/auth/google_oauth2" %>

Relevant gems:

gem 'omniauth'
gem 'dotenv-rails'
gem 'omniauth-google-oauth2'

Initializer file:

Rails.application.config.middleware.use OmniAuth::Builder do
    provider :google_oauth2, ENV["GOOGLE_CLIENT_ID"],ENV["GOOGLE_CLIENT_SECRET"], skip_jwt: true
end

I'm using this as my guide: Medium Guide

I've gone through the process of setting up the app twice now on Developer Tools to get the Client ID and Secret in case that was the issue and both times I was getting the same error. I am new to coding so I'm sure this is something very silly and obvious, but I just can't seem to find it. Thank you for the help!

EDIT: I also have in my routes file: get '/auth/:provider/callback' => 'sessions#omniauth'

Rails Routes:

                               Prefix Verb   URI Pattern                                                                              Controller#Action
                                 root GET    /                                                                                        sessions#home
                                      GET    /auth/:provider/callback(.:format)                                                       sessions#omniauth
                               signup GET    /signup(.:format)                                                                        users#new
                                      POST   /signup(.:format)                                                                        users#create
                                login GET    /login(.:format)                                                                         sessions#new
                                      POST   /login(.:format)                                                                         sessions#create
                               logout DELETE /logout(.:format)                                                                        sessions#destroy
                          user_groups GET    /users/:user_id/groups(.:format)                                                         groups#index
                                      POST   /users/:user_id/groups(.:format)                                                         groups#create
                       new_user_group GET    /users/:user_id/groups/new(.:format)                                                     groups#new
                                users GET    /users(.:format)                                                                         users#index
                                      POST   /users(.:format)                                                                         users#create
                             new_user GET    /users/new(.:format)                                                                     users#new
                            edit_user GET    /users/:id/edit(.:format)                                                                users#edit
                                 user GET    /users/:id(.:format)                                                                     users#show
                                      PATCH  /users/:id(.:format)                                                                     users#update
                                      PUT    /users/:id(.:format)                                                                     users#update
                                      DELETE /users/:id(.:format)                                                                     users#destroy
                                      GET    /users(.:format)                                                                         users#index
                                      POST   /users(.:format)                                                                         users#create
                                      GET    /users/new(.:format)                                                                     users#new
                                      GET    /users/:id(.:format)                                                                     users#show
                           edit_group GET    /groups/:id/edit(.:format)                                                               groups#edit
                                group GET    /groups/:id(.:format)                                                                    groups#show
                                      DELETE /groups/:id(.:format)                                                                    groups#destroy
                           categories GET    /categories(.:format)                                                                    categories#index
                             category GET    /categories/:id(.:format)                                                                categories#show
                                cards GET    /cards(.:format)                                                                         cards#index
                                      POST   /cards(.:format)                                                                         cards#create
                             new_card GET    /cards/new(.:format)                                                                     cards#new
                            edit_card GET    /cards/:id/edit(.:format)                                                                cards#edit
                                 card GET    /cards/:id(.:format)                                                                     cards#show
                                      PATCH  /cards/:id(.:format)                                                                     cards#update
                                      PUT    /cards/:id(.:format)                                                                     cards#update
                                      DELETE /cards/:id(.:format)                                                                     cards#destroy
        rails_postmark_inbound_emails POST   /rails/action_mailbox/postmark/inbound_emails(.:format)                                  action_mailbox/ingresses/postmark/inbound_emails#create
           rails_relay_inbound_emails POST   /rails/action_mailbox/relay/inbound_emails(.:format)                                     action_mailbox/ingresses/relay/inbound_emails#create
        rails_sendgrid_inbound_emails POST   /rails/action_mailbox/sendgrid/inbound_emails(.:format)                                  action_mailbox/ingresses/sendgrid/inbound_emails#create
  rails_mandrill_inbound_health_check GET    /rails/action_mailbox/mandrill/inbound_emails(.:format)                                  action_mailbox/ingresses/mandrill/inbound_emails#health_check
        rails_mandrill_inbound_emails POST   /rails/action_mailbox/mandrill/inbound_emails(.:format)                                  action_mailbox/ingresses/mandrill/inbound_emails#create
         rails_mailgun_inbound_emails POST   /rails/action_mailbox/mailgun/inbound_emails/mime(.:format)                              action_mailbox/ingresses/mailgun/inbound_emails#create
       rails_conductor_inbound_emails GET    /rails/conductor/action_mailbox/inbound_emails(.:format)                                 rails/conductor/action_mailbox/inbound_emails#index
                                      POST   /rails/conductor/action_mailbox/inbound_emails(.:format)                                 rails/conductor/action_mailbox/inbound_emails#create
    new_rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/new(.:format)                             rails/conductor/action_mailbox/inbound_emails#new
   edit_rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format)                        rails/conductor/action_mailbox/inbound_emails#edit
        rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#show
                                      PATCH  /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#update
                                      PUT    /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#update
                                      DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                             rails/conductor/action_mailbox/inbound_emails#destroy
rails_conductor_inbound_email_reroute POST   /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format)                      rails/conductor/action_mailbox/reroutes#create
                   rails_service_blob GET    /rails/active_storage/blobs/:signed_id/*filename(.:format)                               active_storage/blobs#show
            rails_blob_representation GET    /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
                   rails_disk_service GET    /rails/active_storage/disk/:encoded_key/*filename(.:format)                              active_storage/disk#show
            update_rails_disk_service PUT    /rails/active_storage/disk/:encoded_token(.:format)                                      active_storage/disk#update
                 rails_direct_uploads POST   /rails/active_storage/direct_uploads(.:format)                                           active_storage/direct_uploads#create

Upvotes: 8

Views: 4638

Answers (4)

bjelli
bjelli

Reputation: 10090

I was getting this error after upgrading from Rails 6 to Rails 6. My old link:

 <%= link_to "log in",  '/auth/xxx/', method: :post %>

did not generate POST requests anymore. This is because Rails-ujs is no longer used by default in Rails 7.

See Rails API: Deprecated: Rails UJS Attributes and Rails Guide for Rails 7: Working with JavaScript.

The new way of setting the method is with data { "turbo-method" ... }

 <%= link_to "log in",  '/auth/xxx/', data: { "turbo-method": :post } %>

But in my case this did not work either, because this is sent with JavaScript by default, which messed up my oauth2 flow. I ended up using button_to instead. button_to uses post by default, which is what I needed, and I could disable turbo and turn the HTTP request into a normal request and avoid problems with CORS and oauth:

  <%= button_to "log in",  '/auth/fhs/',  data: { "turbo": false } %> 

Upvotes: 1

kev
kev

Reputation: 1200

I was experiencing this exact error, except that it was working fine on my main layouts but not working on the landing page layouts (I have 2 different layouts)

the signin link

<%= link_to '/auth/google_oauth2', method: :post, :class => "btn-get-started font-weight-semibold" do %>
  <span class="text-pink-400">
    <i class="icon-cart2 mr-2"></i> Order
  </span>
<% end %>

The link method is clearly a :post but the error reported a missing get route

Fixed this by ensuring that the landing page layout has all the required javascripts

<%= javascript_pack_tag 'application' %>

Upvotes: 1

Sarah Marie
Sarah Marie

Reputation: 429

Depending on the OmniAuth version (2.0.0):

OmniAuth now defaults to only POST as the allowed request_phase method

Add:

# Gemfile
gem 'omniauth-rails_csrf_protection', '~> 0.1'

Change Links to POST requests:

link_to "Log In with Google", "/auth/google_oauth2", method: :post
# or
button_to "Log In with Google", "/auth/google_oauth2"

If you still need GET requests, add knowing there is a security concern:

# config/initializers/omniauth.rb or similar
OmniAuth.config.allowed_request_methods = [:post, :get]

For omniauth < 2.0.0

Consider CVE remediation described here.

Upvotes: 21

Kishan Ku. Patel
Kishan Ku. Patel

Reputation: 341

OmniAuth now defaults to only POST as allowed request_phase methods.

You need to update all the get requests to post requests.

In your view file you can replace anchor tag with a form and submit button

<a href='/auth/developer'>Login with Developer</a>

above code can be replaced with the following:-

<%= form_tag('/auth/developer', method: 'post') do %>
  <button type='submit'>Login with Developer</button>
<% end %>

In case you don't want to use the post method you can add the below code:

config/initializers/omniauth.rb

OmniAuth.config.allowed_request_methods = [:post, :get]
OmniAuth.config.silence_get_warning = true

Check out here for more information.

Upvotes: 3

Related Questions