Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devise github login not working after Turbo enabled. #45

Closed
ChrisZou opened this issue Dec 27, 2020 · 8 comments
Closed

Devise github login not working after Turbo enabled. #45

ChrisZou opened this issue Dec 27, 2020 · 8 comments

Comments

@ChrisZou
Copy link

I'm using Devise GitHub omniauth login for an app and the login link looks like this:

# layouts/_header.html.erb
<%= link_to "Github Login", user_github_omniauth_authorize_path, method: :post, class: "some classes", "data-turbo" => "false" %>

After I enable Turbo, it no longer works. After inspecting the network, I found that when a user clicks on the link, it issues a fetch type request to the server, rather then a document type request.
My app is open-sourced here and you can reproduce it by uncommenting the // import { Turbo, cable } from '@hotwired/turbo-rails' line from application.js pack file.

@seanpdoyle
Copy link
Contributor

Is this related to hotwired/turbo-rails#33?

@ChrisZou
Copy link
Author

Hi @seanpdoyle, thanks for the reply! I think they do are related. But after I tried changing link_to to button_to as the discussion over there suggested, it still didn't work. The generated form looked like this

<form class="button_to" method="post" action="/users/auth/github" data-turbo="false">
    <input class="some classes" data-turbo="false" type="submit" value="Github Login">
    <input type="hidden" name="authenticity_token" value="some token value">
</form>

I noticed that the data-turbo="false" attribute was on the submit input control rather than on the form itself. Not sure if that was the cause because I tried editing the form and added the data-turbo="false" attribute, and it still didn't work. I think it might also be related to #26?

@excid3
Copy link
Contributor

excid3 commented Jan 1, 2021

Here's what I did to get it working. I added a FailureApp to handle Warden auth failures and a custom responder. Both of those are needed to treat turbo_stream formats like html.

The latest Turbo will handle the 422 status for errors to correctly update the DOM without adding any turbo_stream views.

# config/initializers/devise.rb
# frozen_string_literal: true

class TurboFailureApp < Devise::FailureApp
  def respond
    if request_format == :turbo_stream
      redirect
    else
      super
    end
  end

  def skip_format?
    %w(html turbo_stream */*).include? request_format.to_s
  end
end

class TurboController < ApplicationController
  class Responder < ActionController::Responder
    def to_turbo_stream
      controller.render(options.merge(formats: :html))
    rescue ActionView::MissingTemplate => error
      if get?
        raise error
      elsif has_errors? && default_action
        render rendering_options.merge(formats: :html, status: :unprocessable_entity)
      else
        redirect_to navigation_location
      end
    end
  end

  self.responder = Responder
  respond_to :html, :turbo_stream
end

Devise.setup do |config|

  # ==> Controller configuration
  # Configure the parent class to the devise controllers.
  config.parent_controller = 'TurboController'

  # ==> Warden configuration
  config.warden do |manager|
    manager.failure_app = TurboFailureApp
  end
end

Screencast on how it all works: https://gorails.com/episodes/devise-hotwire-turbo?autoplay=1

@ChrisZou
Copy link
Author

ChrisZou commented Jan 2, 2021

Hi Chris @excid3 , thank you for your response! Now that #3 has been merged, I think another way to fix this is by simply adding data-turbo="false" to the login form. 😁 I tried it in my project here and it worked.
BTW, thank you for all the awesome gorails videos. They have been very helpful to me. ❤️

@boardfish
Copy link

@ChrisZou's workaround is good for now, but it's not perfect as a and button are semantically different. I've discussed this more in #74.

I think the root cause is failed CORS, since the redirect goes to github.com in this case, as I've explained over there.

@sstephenson
Copy link
Contributor

Closing this now that Turbo 7.0.0-beta.2 is out.

donrestarone referenced this issue in restarone/violet_rails Jul 1, 2022
Addresses: #842


The bug existed in _sign-in_, _sign-up_, and _users/edit_ page. You can view it in the attached video below.
 

https://user-images.githubusercontent.com/25191509/176618871-e3e9c2b5-b6bd-4dfb-a59b-80e9ccfecf0b.mp4


The following reference is used to patch this bug.
[https://github.com/hotwired/turbo/issues/45](https://github.com/hotwired/turbo/issues/45)

And, below is the demo video after the fix.

https://user-images.githubusercontent.com/25191509/176619214-bf10ce2c-60be-44ce-bfe8-ed97c9930dff.mp4



## fix for action text not loading in production / staging

Reference: rails/rails#37672

Addresses: #852

Co-authored-by: Prashant <[email protected]>
@marcoroth
Copy link
Member

@0q2 the latest version of Devise fixes this, so there is no need for the work-around anymore.

@0q2
Copy link

0q2 commented Jul 26, 2023

@0q2 the latest version of Devise fixes this, so there is no need for the work-around anymore.

You are correct!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

7 participants