Skip to content

Commit

Permalink
Use enhanced header navigation with POST to sign out
Browse files Browse the repository at this point in the history
  • Loading branch information
steventux committed Apr 9, 2024
1 parent 711a81f commit c548e3e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
10 changes: 7 additions & 3 deletions app/assets/stylesheets/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ $govuk-images-path: "/";
@import "govuk-frontend/dist/govuk/all";

button.govuk-header__link {
background: none !important;
background: none;
border: none;
padding: 0 !important;
color: #fff;
padding: 0;
color: $govuk-body-background-colour;
cursor: pointer;
font-size: 1rem;
font-weight: bold;
}

button.govuk-header__link:focus {
background-color: $govuk-focus-colour;
}
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "dfe/analytics/filtered_request_event"
require "govuk_component/header_component"

class ApplicationController < ActionController::Base
include DfE::Analytics::Requests
Expand Down
6 changes: 5 additions & 1 deletion app/views/layouts/base.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
<%= govuk_header(service_name: t("service.name")) do |header|
if request.path != main_app.not_authorised_path
if current_dsi_user
header.with_navigation_item(href: main_app.dsi_sign_out_path(id_token_hint: session[:id_token]), text: "Sign out")
header.with_navigation_item(
href: main_app.dsi_sign_out_post_path({ id_token_hint: session[:id_token] }),
post: true,
text: "Sign out"
)
else
header.with_navigation_item(href: main_app.sign_in_path, text: "Sign in")
end
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
get "/sign-in", to: "sign_in#new"
get "/sign-out", to: "sign_out#new"
get "/auth/dfe/sign-out", to: "sign_out#new", as: :dsi_sign_out
post "/auth/dfe/sign-out", to: "sign_out#new", as: :dsi_sign_out_post

get "/auth/dfe/callback", to: "omniauth_callbacks#dfe"
post "/auth/developer/callback", to: "omniauth_callbacks#dfe_bypass"
Expand Down
7 changes: 4 additions & 3 deletions lib/govuk_component/header_component.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class GovukComponent::HeaderComponent
class GovukComponent::HeaderComponent::NavigationItem
class NavigationItem
attr_reader :post, :params

def initialize(text:, href: nil, post: false, options: {}, active: nil, classes: [], html_attributes: {})
Expand All @@ -10,8 +10,9 @@ def initialize(text:, href: nil, post: false, options: {}, active: nil, classes:
@post = post

if button_to?
query = URI(@href).query
@params = query.present? ? Hash[URI.decode_www_form(query)] : {}
uri = URI(@href)
@href = uri.path
@params = uri.query.present? ? Hash[URI.decode_www_form(uri.query)] : {}
end

super(classes:, html_attributes:)
Expand Down
9 changes: 5 additions & 4 deletions spec/system/user_signs_in_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@

def then_i_am_signed_in
within("header") do
expect(page).to have_link("Sign out")
sign_out_link = find_link("Sign out")
# Expect the token from mocked auth to be in the sign out link
expect(sign_out_link[:href]).to include "id_token_hint=abc123"
expect(page).to have_button("Sign out")
sign_out_button = find_button("Sign out")
# Expect the token from mocked auth to be in the sign out form
token_hint_field = sign_out_button.sibling("input[name=id_token_hint]", visible: false)
expect(token_hint_field.value).to eq("abc123")
end
expect(DsiUser.count).to eq 1
expect(DsiUserSession.count).to eq 1
Expand Down

0 comments on commit c548e3e

Please sign in to comment.