MID response handling

This commit is contained in:
Martin Lensment 2015-03-23 16:25:18 +02:00
parent 418bd2046c
commit 5f2e0619e5
3 changed files with 52 additions and 20 deletions

View file

@ -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)

View file

@ -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)
)

View file

@ -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'