From 58a61da1209109496b0785f4ebfc87a565b23289 Mon Sep 17 00:00:00 2001 From: Kar Rui Lau Date: Thu, 3 Jun 2021 12:44:29 +0800 Subject: [PATCH 01/11] feat(AdminAuthService): add checkIsEmailAllowed service function --- src/public/services/AdminAuthService.ts | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/public/services/AdminAuthService.ts diff --git a/src/public/services/AdminAuthService.ts b/src/public/services/AdminAuthService.ts new file mode 100644 index 0000000000..aa9359f23a --- /dev/null +++ b/src/public/services/AdminAuthService.ts @@ -0,0 +1,29 @@ +import axios from 'axios' +import validator from 'validator' + +const ADMIN_AUTH_ENDPOINT = '/api/v3/auth' + +/** + * Check whether the given email string is from an email domain. + * @param email the email to check + * @returns original email if email is valid + * @throws Error on invalid email + * @throws Error on non 2xx response + */ +export const checkIsEmailAllowed = async (email = ''): Promise => { + if (!validator.isEmail(email)) { + throw new Error('Please enter a valid email address') + } + + return axios + .post(`${ADMIN_AUTH_ENDPOINT}/email/validate`, { email }) + .then(() => email) + .catch((error) => { + if (axios.isAxiosError(error) && error.response?.data) { + throw new Error(error.response.data) + } + throw new Error( + 'Something went wrong while validating your email. Please refresh and try again', + ) + }) +} From 78e1523de8bc7c63fb91b26ecf341cff86ceba7b Mon Sep 17 00:00:00 2001 From: Kar Rui Lau Date: Thu, 3 Jun 2021 12:45:16 +0800 Subject: [PATCH 02/11] ref: condense vm.checkEmail and vm.checkUser into single vm.login fn --- .../authentication.client.controller.js | 43 ++++++------------- .../authentication/signin.client.view.html | 2 +- 2 files changed, 13 insertions(+), 32 deletions(-) diff --git a/src/public/modules/users/controllers/authentication.client.controller.js b/src/public/modules/users/controllers/authentication.client.controller.js index 403c9b2861..5dcc7450cb 100755 --- a/src/public/modules/users/controllers/authentication.client.controller.js +++ b/src/public/modules/users/controllers/authentication.client.controller.js @@ -1,10 +1,12 @@ 'use strict' const HttpStatus = require('http-status-codes') +const AdminAuthService = require('../../../services/AdminAuthService') angular .module('users') .controller('AuthenticationController', [ + '$q', '$scope', '$state', '$timeout', @@ -15,6 +17,7 @@ angular ]) function AuthenticationController( + $q, $scope, $state, $timeout, @@ -54,7 +57,7 @@ function AuthenticationController( vm.handleEmailKeyUp = function (e) { if (e.keyCode == 13) { - vm.isSubmitEmailDisabled === false && vm.checkEmail() + vm.isSubmitEmailDisabled === false && vm.login() // condition vm.isSubmitEmailDisabled == false is necessary to prevent retries using enter key // when submit button is disabled } else { @@ -122,45 +125,23 @@ function AuthenticationController( // 4 - Verify OTP /** - * Conducts front-end check of user email format + * Checks validity of email domain (i.e. agency) and sends login OTP if email + * is valid. */ - vm.checkEmail = function () { + vm.login = function () { vm.buttonClicked = true - if (/\S+@\S+\.\S+/.test(vm.credentials.email)) { - vm.credentials.email = vm.credentials.email.toLowerCase() - vm.checkUser() - } else { - // Invalid email - vm.buttonClicked = false - vm.signInMsg = { - isMsg: true, - isError: true, - msg: 'Please enter a valid email', - } - vm.isSubmitEmailDisabled = true - } - } - - /** - * Checks validity of email domain (i.e. agency) - * Directs user to otp input page - */ - vm.checkUser = function () { - Auth.checkUser(vm.credentials).then( - function () { - vm.sendOtp() - }, - function (error) { + $q.when(AdminAuthService.checkIsEmailAllowed(vm.credentials.email)) + .then(() => vm.sendOtp()) + .catch((error) => { // Invalid agency vm.buttonClicked = false vm.signInMsg = { isMsg: true, isError: true, - msg: error, + msg: error.message, } vm.isSubmitEmailDisabled = true - }, - ) + }) } /** diff --git a/src/public/modules/users/views/authentication/signin.client.view.html b/src/public/modules/users/views/authentication/signin.client.view.html index 48d6f7fb90..189d2e3c89 100755 --- a/src/public/modules/users/views/authentication/signin.client.view.html +++ b/src/public/modules/users/views/authentication/signin.client.view.html @@ -40,7 +40,7 @@