Skip to content

Commit

Permalink
added tests for logging in
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellen-Wittingen committed May 25, 2024
1 parent a4a58a0 commit f4e4f67
Show file tree
Hide file tree
Showing 4 changed files with 274 additions and 262 deletions.
18 changes: 9 additions & 9 deletions app/controllers/callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@ def identity
if user.persisted?
identity = Identity.find_by(user_id: user.id)
if user.deactivated
render(json: { login_success: false, otp_required: false, error_message: "Uw account is gedeactiveerd, dus inloggen is niet mogelijk." })
render(json: { state: "password_prompt", error_message: "Uw account is gedeactiveerd, dus inloggen is niet mogelijk." })
elsif identity && identity.otp_enabled
one_time_password = params[:verification_code]
if !one_time_password
# OTP code not present, so request it
render(json: { login_success: false, otp_required: true })
render(json: { state: "otp_prompt" })
elsif identity.authenticate_otp(one_time_password)
# OTP code correct
sign_in(:user, user)
render(json: { login_success: true, redirect_url: user.roles.any? ? root_path : user_path(user.id) })
render(json: { state: "logged_in", redirect_url: user.roles.any? ? root_path : user_path(user.id) })
else
# OTP code incorrect
render(json: { login_success: false, otp_required: true, error_message: "Inloggen mislukt. De authenticatiecode is incorrect." })
render(json: { state: "otp_prompt", error_message: "Inloggen mislukt. De authenticatiecode is incorrect." })
end
elsif identity
# no OTP enabled
sign_in(:user, user)
render(json: { login_success: true, redirect_url: user.roles.any? ? root_path : user_path(user.id) })
render(json: { state: "logged_in", redirect_url: user.roles.any? ? root_path : user_path(user.id) })
else
# identity does not exist, should not be possible
render(json: { login_success: false, otp_required: false, error_message: "Inloggen mislukt door een error. Herlaad de pagina en probeer het nog een keer. <br/><i>Werkt het na een paar keer proberen nog steeds niet? Neem dan contact op met de ICT-commissie.</i>" })
render(json: { state: "password_prompt", error_message: "Inloggen mislukt door een error. Herlaad de pagina en probeer het nog een keer. <br/><i>Werkt het na een paar keer proberen nog steeds niet? Neem dan contact op met de ICT-commissie.</i>" })
end
else
render(json: { login_success: false, otp_required: false, error_message: "Inloggen mislukt. De ingevulde gegevens zijn incorrect." })
render(json: { state: "password_prompt", error_message: "Inloggen mislukt. De ingevulde gegevens zijn incorrect." })
end
end

Expand All @@ -51,9 +51,9 @@ def failure
else
error_message << " #{request.env['omniauth.error.type'].to_s}"

Check warning on line 52 in app/controllers/callbacks_controller.rb

View workflow job for this annotation

GitHub Actions / Lint

[Correctable] Lint/RedundantStringCoercion: Redundant use of Object#to_s in interpolation. (https://rubystyle.guide#no-to-s)
end
render(json: { login_success: false, otp_required: false, error_message: error_message })
render(json: { state: "password_prompt", error_message: error_message })
else
render(json: { login_success: false, otp_required: false, error_message: error_message })
render(json: { state: "password_prompt", error_message: error_message })
end
end
end
18 changes: 12 additions & 6 deletions app/views/identities/login.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,22 @@ async function authenticate_login() {

if (response.headers.get('Content-Type').includes("application/json")) {
const json = await response.json();
document.querySelector("#authenticate_flash_holder").classList.add('d-none');

if (json.login_success) {
if (json.state == "logged_in") {
window.location.replace(json.redirect_url);
} else if (json.otp_required) {
// username and password correct, now ask for authentication code
document.querySelector("#authenticate_flash_holder").classList.add('d-none');
} else if (json.state == "otp_prompt") {
// show prompt for authentication code
document.querySelector("#login_card").classList.add('d-none');
document.querySelector("#otp_card").classList.remove('d-none');
} else {
// username or password is wrong
} else if (json.state == "password_prompt") {
// show prompt for password
document.querySelector("#authenticate_flash_holder").classList.remove('d-none');
document.querySelector("#login_card").classList.remove('d-none');
document.querySelector("#otp_card").classList.add('d-none');
}

if (json.error_message) {
document.querySelector("#authenticate_flash_message").textContent = json.error_message;
document.querySelector("#authenticate_flash_holder").classList.remove('d-none');
}
Expand Down
Loading

0 comments on commit f4e4f67

Please sign in to comment.