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

Always redirect magic link requests back to the sign_in page and render generic flash #120

Merged
merged 1 commit into from
Jun 16, 2023

Conversation

nickhammond
Copy link
Contributor

Hey @mikker,

I realized that I couldn't get magic link requests to work at all without always doing a redirect for Turbo. Turbo is always going to complain about the response needing to be a redirect when it's a simple render call without a status: :unprocessable_entity.

If sessions/create.html.erb is removed though I was thinking it might be nice to be able to specify a redirect location instead of the default or some way to detect that a magic link was just requested after the redirect.

Related to: #116
Root issue: hotwired/turbo-rails#12

@mikker
Copy link
Owner

mikker commented Mar 29, 2022

Nice debugging, thank you!

I think we'd need a special page with what used to be in create.html.erb, as redirecting to sign in is going to confuse people, I think. Something like completed.html.erb. Maybe completed is the wrong word. waiting.html.erb maybe. What do you think?

@nickhammond
Copy link
Contributor Author

@mikker I have what used to be on the create.html.erb page as a flash now so the user still sees it if they have flash configured which is pretty standard. Since the message is a locale(passwordless.sessions.create.email_sent_if_record_found) this can easily be overridden at the app level too.

I suppose not everyone is going to need a full post request magic link flow but right now you can easily override create.html.erb which is what I was thinking might need to be catered to.

What about just another default redirect route?

# Default redirection paths
Passwordless.success_redirect_path = '/' # When a user succeeds in logging in.
Passwordless.failure_redirect_path = '/' # When a a login is failed for any reason.
Passwordless.sign_out_redirect_path = '/' # When a user logs out.
Passwordless.session_creation_redirect_path = '/' # When a user requests a magic link

sign-in-example

Which makes your setup paths:

  1. Do nothing and by default get the redirect to request a magic link with the flash showing
  2. Specify a redirect path where the user can expand on whatever additional information they need to explain

@vhazbun
Copy link

vhazbun commented Jun 14, 2023

@mikker do you think this will be merged soon?

@mikker mikker merged commit 8ae3ec7 into mikker:master Jun 16, 2023
@mikker
Copy link
Owner

mikker commented Jun 16, 2023

Sure, thank you for this.

@nickhammond nickhammond deleted the session-creation-redirect branch June 16, 2023 14:38
@henrikbjorn
Copy link
Contributor

@mikker This is a breaking change, and "broke" my application. Why can't we support the previous logic with turbo?

E.g someone could just create a create.turbo_stream.erb template if they wanted to replace the form inline, or just have it target a specific id via data: { turbo_frame: 'asdasd' }

@henrikbjorn
Copy link
Contributor

Also turbo can just be turned off for the form.

@mikker
Copy link
Owner

mikker commented Jun 19, 2023

Apologies, I didn't realize.

I think actually your suggestion to just disable Turbo on our forms is the best and easiest approach. I think the slight downgrade in app speed is acceptable as what the user sees before and after signing is probably not very alike anyway.

@ecomba
Copy link

ecomba commented Aug 23, 2023

I just saw this after bundle upgrading.

As @henrikbjorn commented, it broke my app flow and I decided to revert to the previous version as a flash message will just not do it for me.

@henrikbjorn
Copy link
Contributor

@mikker Since we have an easy way of specifying the controller. We could maybe create some helper methods in the SessionController so that it is easier overwrite the standard create behavior ?

@henrikbjorn
Copy link
Contributor

@ecomba

You can do something like this:

# frozen_string_literal: true

class SessionsController < Passwordless::SessionsController
  layout 'passwordless'

  unauthenticated

  def create
    @resource = find_authenticatable

    @session = build_passwordless_session(@resource)

    Passwordless.config.after_session_save.call(@session, request) if @session.save

    # Not sure this is needed..
    render 'passwordless/sessions/create'
  end
end

Just use the new controller option for the route helper.

@henrikbjorn
Copy link
Contributor

Then just remember to disable turbo on the form, since it requires a redirect...

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

Successfully merging this pull request may close these issues.

None yet

5 participants