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

Add controller to routes and add test to cover the feature #160

Merged
merged 3 commits into from
Sep 14, 2023

Conversation

henrikbjorn
Copy link
Contributor

No description provided.

@henrikbjorn
Copy link
Contributor Author

@mikker This got lost in the next branch

@mikker
Copy link
Owner

mikker commented Sep 14, 2023

Sorry about that! Thank you Henrik.

Now that you're here: How do you feel about the other breaking changes?

@mikker mikker merged commit 7fd380d into mikker:master Sep 14, 2023
3 checks passed
@henrikbjorn henrikbjorn deleted the readd-controller branch September 14, 2023 11:03
@henrikbjorn
Copy link
Contributor Author

@mikker Makes sense to have the possibility for entering a token, to keep the current tab 👍🏻

I think the new SessionsController is kind of complex and needs some extension points. Personally I don't like the usage of flashes and would instead prefer to attach such errors to the form elements.

My current SessionsController

# frozen_string_literal: true

class SessionsController < Passwordless::SessionsController
  layout 'passwordless'

  unauthenticated except: %i[destroy]

  before_action :build_request, only: %i[show new]
  before_action :build_request_with_params, only: %i[create update]

  class Request
    include ActiveModel::Model
    include ActiveModel::Attributes

    attribute :email
    attribute :token
  end

  def create
    @resource = find_authenticatable

    unless @resource
      @request.errors.add(:email, :invalid)

      return render :new, status: :unprocessable_entity
    end

    @session = build_passwordless_session(@resource)

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

    return redirect_to_confirm_path if Rails.env.development?

    redirect_to url_for(id: @session.id, action: :show), status: :see_other
  end

  protected

  def build_request
    @request = Request.new
  end

  def build_request_with_params
    @request = Request.new(passwordless_session_params)
  end

  def authenticate_and_sign_in(session, token)
    unless session.authenticate(token)
      @request.errors.add(:token, I18n.t("passwordless.sessions.errors.invalid_token"))

      return render(status: :forbidden, action: "show")
    end

    sign_in(session)

    redirect_to(passwordless_success_redirect_path, status: :see_other, **redirect_to_options)
  rescue \
    Errors::TokenAlreadyClaimedError,
    Errors::SessionTimedOutError

    @request.errors.add(:token, I18n.t("passwordless.sessions.errors.invalid_token"))

    render :show, status: :forbidden
  end

  def redirect_to_confirm_path
    return redirect_to url_for(action: :new), status: :see_other unless @session.persisted?

    redirect_to url_for(id: @session.id, token: @session.token, action: :confirm), status: :see_other
  end
end

new.html.erb

<%= custom_form_with model: @request, scope: :passwordless, url: url_for(action: :create), data: { turbo: false } do |f| %>
  <%= f.input :email, as: :email_field, autofocus: true, autocomplete: "email" %>

  <%= f.actions do %>
      <%= f.submit t('passwordless.sessions.new.submit'), class: 'w-full' %>
    <% end %>
<% end %> 

show.html.erb:

<%= custom_form_with model: @request, scope: :passwordless, url: url_for(action: :update), method: :patch, data: { turbo: false } do |f| %>
  <%= f.input :token, autofocus: true, autocomplete: "one-time-password", error: 'something' %>

  <%= f.actions do %>
      <%= f.submit t('passwordless.sessions.new.submit'), class: 'w-full' %>
    <% end %>
<% end %> 

Using ViewComponent::Form makes the input have an error message directly attached to the input.

@henrikbjorn
Copy link
Contributor Author

Would like a new release instead of doing the github: 'mikker/passwordless' in gemfiles :)

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

2 participants