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

Ability to specify flash messages upon redirects #101

Closed
mzrnsh opened this issue Aug 8, 2021 · 4 comments
Closed

Ability to specify flash messages upon redirects #101

mzrnsh opened this issue Aug 8, 2021 · 4 comments

Comments

@mzrnsh
Copy link
Contributor

mzrnsh commented Aug 8, 2021

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
Copy link
Contributor Author

mzrnsh commented Aug 8, 2021

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
Copy link
Collaborator

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

@mikker mikker closed this as completed Sep 21, 2023
@keilmillerjr
Copy link

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
Copy link
Owner

mikker commented Oct 21, 2023

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.

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

No branches or pull requests

4 participants