passwordless icon indicating copy to clipboard operation
passwordless copied to clipboard

Ability to specify flash messages upon redirects

Open mzrnsh opened this issue 3 years ago • 2 comments

Hello everyone. I wasn't able to figure out how to set flash messages when user is redirected after actions like successful login, failure or sign out.

image

My use case would be something like "Welcome back!" or "You are logged out, see you soon".

Is there a way to do this?

mzrnsh avatar Aug 08 '21 15:08 mzrnsh

Off-topic: I am using Passwordless for the first time and I am really loving it! Thanks @mikker and everyone else for your amazing work! I really look forward to contribute.

mzrnsh avatar Aug 08 '21 15:08 mzrnsh

It looks like there aren't any flashes set for those items.

I think you have two options.

  1. Monkey patch SessionsController https://github.com/mikker/passwordless/blob/master/app/controllers/passwordless/sessions_controller.rb to have the intended behavior

or

  1. Make a PR which does a flash in all of the locations you want, using the key structure found in: https://github.com/mikker/passwordless/blob/master/config/locales/en.yml You'll likely just modify https://github.com/mikker/passwordless/blob/master/app/controllers/passwordless/sessions_controller.rb#L43-L55 but there are other opportunities for flash such as SessionsController#destroy. If you go this route, it might be nice to look up whether there is a value present for the given i18n key, and if so provide a flash, otherwise don't. Something like:
    def show
      # Make it "slow" on purpose to make brute-force attacks more of a hassle
      BCrypt::Password.create(params[:token])
      sign_in(passwordless_session)
      
      flash_if_key(:success, ".passwordless.sessions.create.signed_in")
      redirect_to(passwordless_success_redirect_path)
    rescue Errors::TokenAlreadyClaimedError
      flash_if_key(:error, ".passwordless.sessions.create.token_claimed")
      redirect_to(passwordless_failure_redirect_path)
    rescue Errors::SessionTimedOutError
      flash_if_key(:error, ".passwordless.sessions.create.session_expired")
      redirect_to(passwordless_failure_redirect_path)
    end


    private def flash_if_key(flash_type, i18n_key)
      flash[flash_type] = I18n.t(i18n_key) if I18n.t(i18n_key, default: nil).present?
    end

rickychilcott avatar Aug 08 '21 17:08 rickychilcott

It doesn't seem as though this issue is fully completed. While a flash is generated for unsuccessful new session and token authentication, a flash for successful authentication and destroy is not implemented.

keilmillerjr avatar Oct 15 '23 23:10 keilmillerjr

I'm not sure they're needed? IMO, if it works it would be pretty self evident. Not completely sure I'm right now so feel free to try and convince me otherwise.

mikker avatar Oct 21 '23 18:10 mikker