mikker / passwordless

🗝 Authentication for your Rails app without the icky-ness of passwords
MIT License
1.28k stars 88 forks source link

Ability to specify flash messages upon redirects #101

Closed mzrnsh closed 1 year ago

mzrnsh commented 3 years ago

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 commented 3 years ago

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.

rickychilcott commented 3 years ago

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

2) 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
keilmillerjr commented 1 year ago

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.

mikker commented 1 year ago

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.