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 default flash notice on SessionsController#destroy #178

Merged
merged 3 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add default flash notice on SessionsController#destroy
This commit adds a default flash notice to destroy action
while also keeping the flexibility to customize redirect options
via config.

When creating a session in case of success there is a flash notice
displayed while when destroying a session there was not flash noticed
displayed in case of success.

Also having a successful flash message for destroying a session makes
the system tests in the apps using this gem a bit easier to check
if the logout happened successfully.
  • Loading branch information
lucianghinda committed Nov 1, 2023
commit 254f628422b0333c00065e9610777291cdd776c6
7 changes: 6 additions & 1 deletion app/controllers/passwordless/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ def confirm
# @see ControllerHelpers#sign_out
def destroy
sign_out(authenticatable_class)
redirect_to(passwordless_sign_out_redirect_path, Passwordless.config.redirect_to_response_options.dup)

redirect_to_response_options = {
notice: I18n.t("passwordless.sessions.destroy.signed_out")
}.merge(redirect_to_options)

redirect_to(passwordless_sign_out_redirect_path, redirect_to_response_options)
end

protected
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ en:
invalid_token: "Token is invalid"
session_expired: "Your session has expired, please sign in again."
token_claimed: "This link has already been used, try requesting the link again"
destroy:
signed_out: "Signed out successfully"
mailer:
sign_in:
subject: "Signing in ✨"
Expand Down
22 changes: 22 additions & 0 deletions test/controllers/passwordless/sessions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,31 @@ class << User

assert_equal 200, status
assert_equal "/", path
assert_match I18n.t("passwordless.sessions.destroy.signed_out"), flash[:notice]
assert pwless_session(User).blank?
end

test("DELETE /:passwordless_for/sign_out :: When response options are configured ") do
Passwordless.configure do |config|
config.redirect_to_response_options = {
notice: "my custom notice"
}
end

user = User.create(email: "a@a")
passwordless_session = create_pwless_session(authenticatable: user, token: "hi")

get "/users/sign_in/#{passwordless_session.id}/#{passwordless_session.token}"
assert_not_nil pwless_session(User)

get "/users/sign_out"
follow_redirect!

assert_equal 200, status
assert pwless_session(User).blank?
assert_match "my custom notice", flash[:notice]
end

class Helpers
extend Passwordless::ControllerHelpers
end
Expand Down