From 5f2e0619e5c336a9ffb0de43b77079d5cc2f324f Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Mon, 23 Mar 2015 16:25:18 +0200 Subject: [PATCH] MID response handling --- .../registrar/sessions_controller.rb | 49 +++++++++++++------ app/views/registrar/sessions/login_mid.haml | 15 ++++-- config/locales/en.yml | 8 +++ 3 files changed, 52 insertions(+), 20 deletions(-) diff --git a/app/controllers/registrar/sessions_controller.rb b/app/controllers/registrar/sessions_controller.rb index cc92659b6..81a97ea08 100644 --- a/app/controllers/registrar/sessions_controller.rb +++ b/app/controllers/registrar/sessions_controller.rb @@ -22,12 +22,12 @@ class Registrar::SessionsController < SessionsController phone = params[:user][:phone] client = Digidoc::Client.new - country_codes = {'+372' => 'EST'} + # country_codes = {'+372' => 'EST'} response = client.authenticate( - :phone => "+372#{phone}", - :message_to_display => 'Authenticating', - :service_name => 'Testing' + phone: "+372#{phone}", + message_to_display: 'Authenticating', + service_name: 'Testing' ) @user = find_user_by_idc(response.user_id_code) @@ -37,12 +37,8 @@ class Registrar::SessionsController < SessionsController session[:mid_session_code] = client.session_code render json: { message: t('check_your_phone_for_confirmation_code') }, status: :ok else - flash[:alert] = t('no_such_user') - flash.keep(:alert) - render js: "window.location = '#{registrar_login_mid_path}'" + render json: { message: t('no_such_user') }, status: :unauthorized end - - # client.authentication_status end def mid_status @@ -50,13 +46,34 @@ class Registrar::SessionsController < SessionsController client.session_code = session[:mid_session_code] auth_status = client.authentication_status - # binding.pry - # flash[:notice] = I18n.t('welcome') - # flash.keep(:notice) - - # sign_in @user - # render js: "window.location = '#{registrar_invoices_path}'" - render json: { message: t('not_ok') }, status: :request_timeout + case auth_status.status + when 'OUTSTANDING_TRANSACTION' + render json: { message: t('check_your_phone_for_confirmation_code') }, status: :ok + when 'USER_AUTHENTICATED' + @user = find_user_by_idc(session[:user_id_code]) + sign_in @user + flash[:notice] = t('welcome') + flash.keep(:notice) + render js: "window.location = '#{registrar_invoices_path}'" + when 'NOT_VALID' + render json: { message: t('user_signature_is_invalid') }, status: :bad_request + when 'EXPIRED_TRANSACTION' + render json: { message: t('session_timeout') }, status: :bad_request + when 'USER_CANCEL' + render json: { message: t('user_cancelled') }, status: :bad_request + when 'MID_NOT_READY' + render json: { message: t('mid_not_ready') }, status: :bad_request + when 'PHONE_ABSENT' + render json: { message: t('phone_absent') }, status: :bad_request + when 'SENDING_ERROR' + render json: { message: t('sending_error') }, status: :bad_request + when 'SIM_ERROR' + render json: { message: t('sim_error') }, status: :bad_request + when 'INTERNAL_ERROR' + render json: { message: t('internal_error') }, status: :bad_request + else + render json: { message: t('internal_error') }, status: :bad_request + end end def find_user_by_idc(idc) diff --git a/app/views/registrar/sessions/login_mid.haml b/app/views/registrar/sessions/login_mid.haml index 27fba50c3..b43ad8123 100644 --- a/app/views/registrar/sessions/login_mid.haml +++ b/app/views/registrar/sessions/login_mid.haml @@ -10,21 +10,28 @@ %button.btn.btn-lg.btn-primary.btn-block.js-login{:type => 'submit'}= t('log_in') :coffee + $('.js-login').attr('disabled', false) + status_interval = null mid_status = () -> - status_interval = setInterval((-> - $.post('/registrar/login/mid_status').fail((data, bla, asd) -> + $.post('/registrar/login/mid_status').fail((data) -> clearInterval(status_interval) flash_alert(data.responseJSON.message) + ('.js-login').attr('disabled', false) ) ), 1000) $('.js-login').on 'click', (e) -> e.preventDefault(); + $(this).attr('disabled', true) $.post($('form').attr('action'), $('form').serialize()).done((data) -> - flash_notice(data.message) - mid_status() + if data.message + flash_notice(data.message) + mid_status() + ).fail((data) -> + flash_alert(data.responseJSON.message) + $('.js-login').attr('disabled', false) ) diff --git a/config/locales/en.yml b/config/locales/en.yml index 0803dac05..4e4c9a306 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -498,3 +498,11 @@ en: phone_no: 'Phone number' log_in_with_mid: 'Log in with mobile-id' check_your_phone_for_confirmation_code: 'Check your phone for confirmation code' + user_signature_is_invalid: 'User signature is invalid' + session_timeout: 'Session timeout' + user_cancelled: 'User cancelled' + mid_not_ready: 'M-ID is not ready, please try again' + phone_absent: 'User phone is not in coverage area' + sending_error: 'Could not send sms to user' + sim_error: 'SIM application error' + internal_error: 'Internal error'