Rails google-oauth2 404 - "Not found. Authentication passthru." - ruby-on-rails

When attempting to sign in with Google Oauth, I encounter a 404 error "Not found. Authentication passthru." This worked just a month ago on my site, and I have not changed anything that should have impacted authentication. I've seen several other posts about this, but none have seemed to work in my case.
initializers/devise.rb
Devise.setup do |config|
config.omniauth :google_oauth2, ENV['GOOGLE_OAUTH_CLIENT_ID'], ENV['GOOGLE_OAUTH_CLIENT_SECRET']
.
.
.
gemfile
gem 'devise', github: 'heartcombo/devise'
gem 'omniauth', '~> 1.6', '>= 1.6.1'
gem 'omniauth-google-oauth2'
gem "omniauth-rails_csrf_protection"
routes.rb
devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
devise_scope :user do
get 'users/sign_in', to: 'users/sessions#new'
get 'users/sign_out', to: 'users/sessions#destroy'
end
view
<%- if devise_mapping.omniauthable? %>
<%- resource_class.omniauth_providers.each do |provider| %>
<% if provider = "google_oauth2" %>
<%= link_to "Sign in with Google", omniauth_authorize_path(resource_name, provider), method: :post, class: "oauth-link" %><br />
<% else %>
.
.
.

I was hitting this myself and thought the 404 was some kind of error. I believe it's the intended response when the Omniauth controller doesn't recognize the provider. Try:
<%= link_to "Sign in with Google", user_google_omniauth_authorize_path, method: :post, class: "oauth-link" %>
If you're using Turbo, you'll want to change it to a button_to and set data-turbo: false.

Related

Why don't my OmniAuth callbacks update the page location during feature testing?

I'm running into an issue where my OmniAuth callbacks seem to be operating correctly in production, but when feature testing the login links take me to the correct page BUT the current path is not updated.
Setup:
Devise 4.2.0
OmniAuth 1.3.1
Rails 5.0.0.1
Capybara 2.10.2
Replication:
In a feature example group with JS: true, put OmniAuth into test mode:
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:facebook] = auth_hash
Construct the test
context 'signing in' do
it 'should operate correctly' do
visit root_path
expect(page).to have_content(I18n.t 'home.index.title')
expect(page).to have_content(I18n.t 'home.index.text')
page.find('.google-login').click
wait_for_ajax
# Fails here with:
# expected "/users/auth/google_oauth2" to equal "/journal"
expect(page).to have_current_path journal_root_path
end
end
When I inspect the page visually, the correct page has been rendered. What's happening? This doesn't seem to be a Capybara or Selenium problem; the browser correctly captures the (wrong) path.
It turns out that the problem was with Turbolinks.
It appears that when Turbolinks 5 is operating on that link, it snags the redirect. This is presumably because setting OmniAuth.config.test_mode = true tells OmniAuth to redirect to another page on the test server, whereas the production configuration sends the link to the Facebook servers.
In the test scenario, Turbolinks intercepts it because its on the same server, but in production Turbolinks does NOT intercept the link.
The solution was to update my social media link tags with a data-turbolinks="false" attribute. from:
<%- resource_class.omniauth_providers.each do |provider| %>
<%= link_to omniauth_authorize_path(resource_name, provider), class: "#{(provider_name provider).downcase}-login social-login-small" do %>
<i></i> <%= provider_name provider %> Login
<% end %>
<% end -%>
to:
<%- resource_class.omniauth_providers.each do |provider| %>
<%= link_to omniauth_authorize_path(resource_name, provider), class: "#{(provider_name provider).downcase}-login social-login-small", data: {turbolinks: false} do %>
<i></i> <%= provider_name provider %> Login
<% end %>
<% end -%>

Devise login with username, signin failure not showing error message

I have been trying to login with username so followed this tutorial
https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address
Sign in with username and signup is happening perfectly. But if i try to sign in with invalid username or password, it is not showing any error messages.
i have <%= devise_error_messages! %> mentioned in sessions/new.html.erb.
Also tried binding.pry resource doesn't have any errors attached to it.
routes.rb
devise_for :users
devise_scope :user do
root to: "devise/sessions#new"
end
devise.rb
config.authentication_keys = [ :login ]
other details same as mentioned in the url.
I think you are not displaying the flash messages.
So add the below in your application.html.erb above <%= yield %>
<% flash.each do |key, value| %>
<%= content_tag :div, value, class: "flash #{key}" %>
<% end %>
actually devise logi errors are work with flash. so ad flash to your code
like: <%= flash %>

Pulling images from Instagram Gem over secure domain

I am using the Instagram gem to pull information from the API, but I am getting an error in my browser console because the images are coming through as HTTP on an HTTPS site.
View
<% instagram.each do |i| %>
<li>
<%= link_to i.link, :target => "_blank" do %>
<%= image_tag i.images.standard_resolution.url %>
<% end %>
</li>
<% end %>
Application Controller
def instagram Instagram.user_recent_media("xxxxxx", {:count => 6})
rescue nil
end
Initializer
require "instagram"
Instagram.configure do |config|
config.client_id = "xxx"
config.access_token = "xxx"
end
Instagram will serve assets over http and https, so just adjust your URL. You can make the URL protocol relative, so that the image is always loaded in the browser using the correct protocol. Try this in your view:
<%= image_tag i.images.standard_resolution.url.sub(/^https?\:/, '') %>

How to edit the bootstrap.js file of gem twitter-bootstrap-rails

I know this is definitely wrong, but I cannot seem to get sub menu links to work on mobile devices, and other posts suggest edditing the bootstrap.min.js file.
However, since I am using the gem "twitter-bootstrap-rails" I do not know where the file is and even if it would work to change it.
Ideas?
This is my code, omitted some parts.
<%= nav_bar :fixed => :top, :brand => image_tag('logo.png'), :responsive => true do %>
<% if user_signed_in? %>
<%= menu_group :pull => :right do %>
<%= drop_down "Scan" do %>
<%= menu_item "Android", 'http://sasfad' %>
<%= menu_item "iPhone", 'zxing://asfasdf' %>
<% end %>
<% end %>
<% end %>
You can't and you shouldn't edit those files. But you can fork the gem, edit the bootstrap.min.js file and use your forked gem in your Gemfile.
My recommendation is not to use the twitter-bootstrap-rails gem as the only thing it does is adding for you the assets to the asset pipeline.

Adding dynamic login and logout links rails 3.1

I have a very basic sign up and log in setup running and all I want to know if how to add a link at the very top of my root page that displays 'Log in' or 'Sign out' depending on whether the user is logged in or not.
I have tried various methods I have found on here but can't seem to get them to work as they often create undefined method errors.
What is the simplest way to create this?
Many thanks in advance for your help.
Tom
if you have a session variable where you save the id of the current user (i call it user_id) you could do it like this:
<% if session[:user_id] %>
<!-- user is logged in -->
<%= link_to logout_path %>
<% else %>
<!-- user is not logged in -->
<%= link_to login_path %>
<% end %>
that is what you have to change:
config/routes.rb:
resources :users
# login stuff
controller :sessions do
get "login" => "sessions#new"
post "login" => "sessions#create"
delete "logout" => "sessions#destroy"
end
app/views/sessions/new.html.erb:
# replace this line
<%= form_tag new_session_path do %>
# with
<%= form_tag login_path do %>
the login link is now:
<%= link_to "Login", login_path %>
the logout link:
<%= link_to "Logout", logout_path, :method => :delete %>
Not much of an answer but this Railscast was very helpful to me in learning about how authentication works in rails. The Railscast is Twitter login specific using OmniAuth but the process is much the same. He includes the dynamic links you asked about in his code.
http://railscasts.com/episodes/241-simple-omniauth

Resources