简体   繁体   中英

Google OAuth 2.0 + Ruby on Rails 404: “Not found. Authentication passthru.”

Below is the relevant code (do let me know if I'm missing anything.).

routes.rb:


devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }

devise_scope :user do 
    match '/sessions/user', to: 'devise/sessions#create', via: :post
    get '/join' => 'users/registrations#new'
end

resources :users do
    resources :video_lessons do
      resources :likes
    end
end

resources :users, only: [:show, :index]

devise.rb:

if Rails.env.production?
    config.omniauth :google_oauth2, ENV['GOOGLE_CLIENT_ID_PROD'[, ENV['GOOGLE_CLIENT_SECRET_PROD'], {}
else
    config.omniauth :google_oauth2, ENV['GOOGLE_CLIENT_ID_DEV'], ENV['GOOGLE_CLIENT_SECRET_DEV'], {}
end

omniauth_callbacks_controller.rb

  def google_oauth2
    # You need to implement the method below in your model (e.g. app/models/user.rb)
    @user = User.from_omniauth(request.env['omniauth.auth'])

    if @user.persisted?
      flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Google'
      sign_in_and_redirect @user, 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_user_registration_url, alert: @user.errors.full_messages.join("\n")
    end
  end

devise/registrations/new.html.erb

<%= link_to "Sign up with Google", user_google_oauth2_omniauth_authorize_path %>

The authorized redirect URIs in my Developer's Console are: http://localhost:3000/users/auth/google_oauth2 and https://localhost:3000/users/auth/google_oauth2

I've followed the docs ( https://github.com/zquestz/omniauth-google-oauth2 ) exactly, to no avail.

Thank you for your help!

I recently got this working through this guide . A more descriptive resource, and the source of the guide, is this official wiki example .

This commit shows all the files I changed to get google and github set up on my rails login page using the guide I mentioned first.

Just make sure when you use either of the guides, that you remove extra part of the data as it can overflow some session stores. The guides do not include this important piece of information. So in this line of omniauth_callbacks_controller.rb, your current code is correct :

...
    else
          session['devise.google_data'] = request.env['omniauth.auth'].except(:extra) # Removing extra as it can overflow some session stores
....

Good luck!

<%= link_to "Sign in with Google", user_google_oauth2_omniauth_authorize_path, method: :post %>

The request should be 'post'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM