Skip to content

Commit

Permalink
Merge pull request #2091 from opengovsg/feat/authservice-getuser-ts
Browse files Browse the repository at this point in the history
refactor(auth.client): (5, final) extract get current logged in user service function to Typescript
  • Loading branch information
karrui authored Jun 10, 2021
2 parents c3b3c22 + 62b4e23 commit 279bce5
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 104 deletions.
3 changes: 0 additions & 3 deletions src/public/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,6 @@ require('./modules/forms/services/mailto.client.factory.js')
require('./modules/users/config/users.client.config.js')
require('./modules/users/config/users.client.routes.js')

// User services
require('./modules/users/services/auth.client.service.js')

// User controllers
require('./modules/users/controllers/authentication.client.controller.js')
require('./modules/users/controllers/billing.client.controller.js')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ angular.module('core').component('avatarDropdownComponent', {
'$state',
'$uibModal',
'$window',
'Auth',
'Features',
'Toastr',
avatarDropdownController,
Expand All @@ -26,14 +25,13 @@ function avatarDropdownController(
$state,
$uibModal,
$window,
Auth,
Features,
Toastr,
) {
const vm = this

// Preload user with current details, redirect to signin if unable to get user
vm.user = Auth.getUser() || $state.go('signin')
vm.user = UserService.getUserFromLocalStorage() || $state.go('signin')
vm.avatarText = generateAvatarText()

vm.isDropdownHover = false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const UserService = require('../../../services/UserService')

angular
.module('core')
.controller('EditContactNumberModalController', [
Expand All @@ -8,7 +10,6 @@ angular
'$scope',
'$uibModalInstance',
'$window',
'Auth',
'Toastr',
EditContactNumberModalController,
])
Expand All @@ -21,7 +22,6 @@ function EditContactNumberModalController(
$scope,
$uibModalInstance,
$window,
Auth,
Toastr,
) {
const vm = this
Expand All @@ -47,7 +47,7 @@ function EditContactNumberModalController(
vm.VERIFY_STATE = VERIFY_STATE

// Redirect to signin if unable to get user
vm.user = Auth.getUser() || $state.go('signin')
vm.user = UserService.getUserFromLocalStorage() || $state.go('signin')

vm.vfnState = vm.user.contact ? VERIFY_STATE.SUCCESS : VERIFY_STATE.IDLE

Expand Down
8 changes: 5 additions & 3 deletions src/public/modules/core/services/gtag.client.service.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
angular.module('core').factory('GTag', ['Auth', '$rootScope', '$window', GTag])
const UserService = require('../../../services/UserService')

function GTag(Auth, $rootScope, $window) {
angular.module('core').factory('GTag', ['$rootScope', '$window', GTag])

function GTag($rootScope, $window) {
// Google Analytics tracking ID provided on signup.
const GATrackingID = $window.GATrackingID
let gtagService = {}

const getUserEmail = () => {
const user = Auth.getUser()
const user = UserService.getUserFromLocalStorage()
return user && user.email
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'
let dedent = require('dedent-js')
const dedent = require('dedent-js')

const UserService = require('../../../../services/UserService')

angular.module('forms').component('shareFormComponent', {
templateUrl: 'modules/forms/admin/componentViews/share-form.client.view.html',
Expand All @@ -9,11 +11,11 @@ angular.module('forms').component('shareFormComponent', {
status: '<',
userCanEdit: '<',
},
controller: ['$state', '$translate', 'Auth', shareFormController],
controller: ['$state', '$translate', shareFormController],
controllerAs: 'vm',
})

function shareFormController($state, $translate, Auth) {
function shareFormController($state, $translate) {
const vm = this

vm.$onInit = () => {
Expand Down Expand Up @@ -54,6 +56,6 @@ function shareFormController($state, $translate, Auth) {
}

// Show different pro tip for user depending on their email
const { email } = Auth.getUser()
vm.isGovOfficer = String(email).endsWith('.gov.sg')
const user = UserService.getUserFromLocalStorage()
vm.isGovOfficer = user && String(user.email).endsWith('.gov.sg')
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { StatusCodes } = require('http-status-codes')
const get = require('lodash/get')
const { LogicType } = require('../../../../../types')
const UpdateFormService = require('../../../../services/UpdateFormService')
const UserService = require('../../../../services/UserService')
const FieldFactory = require('../../helpers/field-factory')
const { UPDATE_FORM_TYPES } = require('../constants/update-form-types')

Expand Down Expand Up @@ -42,7 +43,6 @@ angular
'$uibModal',
'FormData',
'FormFields',
'Auth',
'moment',
'Toastr',
'$state',
Expand All @@ -58,7 +58,6 @@ function AdminFormController(
$uibModal,
FormData,
FormFields,
Auth,
moment,
Toastr,
$state,
Expand All @@ -71,7 +70,7 @@ function AdminFormController(

// Redirect to signin if unable to get user
$scope.user =
Auth.getUser() ||
UserService.getUserFromLocalStorage() ||
$state.go(
'signin',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const axios = require('axios').default
const values = require('lodash/values')
const cloneDeep = require('lodash/cloneDeep')

const UserService = require('../../../../services/UserService')

const {
VALID_UPLOAD_FILE_TYPES,
MAX_UPLOAD_FILE_SIZE,
Expand All @@ -28,7 +30,6 @@ angular
'Attachment',
'FormFields',
'$q',
'Auth',
'$state',
'Toastr',
EditFieldsModalController,
Expand All @@ -42,7 +43,6 @@ function EditFieldsModalController(
Attachment,
FormFields,
$q,
Auth,
$state,
Toastr,
) {
Expand All @@ -66,7 +66,7 @@ function EditFieldsModalController(
}

// Serialize allowed email domains
vm.user = Auth.getUser() || $state.go('signin')
vm.user = UserService.getUserFromLocalStorage() || $state.go('signin')
if (vm.field.fieldType === 'email') {
const userEmailDomain = '@' + vm.user.email.split('@').pop()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
const get = require('lodash/get')
const BetaService = require('../../../../services/BetaService')

const UserService = require('../../../../services/UserService')

// Forms controller
angular
.module('forms')
.controller('ListFormsController', [
'$scope',
'FormApi',
'$uibModal',
'Auth',
'moment',
'$state',
'$timeout',
Expand All @@ -24,7 +25,6 @@ function ListFormsController(
$scope,
FormApi,
$uibModal,
Auth,
moment,
$state,
$timeout,
Expand All @@ -41,7 +41,7 @@ function ListFormsController(
// Duplicated form outline on newly dup forms
vm.duplicatedForms = []
// Redirect to signin if unable to get user
vm.user = Auth.getUser() || $state.go('signin')
vm.user = UserService.getUserFromLocalStorage() || $state.go('signin')

// Brings user to edit form page
vm.editForm = function (form) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const dedent = require('dedent-js')
const { get, set, isEqual } = require('lodash')
const AdminSubmissionsService = require('../../../../services/AdminSubmissionsService')

const UserService = require('../../../../services/UserService')

const SETTINGS_PATH = [
'title',
'emails',
Expand Down Expand Up @@ -35,8 +37,6 @@ angular
'$timeout',
'responseModeEnum',
'$uibModal',
'Auth',
'Submissions',
settingsFormDirective,
])

Expand All @@ -46,7 +46,6 @@ function settingsFormDirective(
$timeout,
responseModeEnum,
$uibModal,
Auth,
) {
return {
templateUrl:
Expand All @@ -59,7 +58,7 @@ function settingsFormDirective(
controller: [
'$scope',
function ($scope) {
$scope.user = Auth.getUser()
$scope.user = UserService.getUserFromLocalStorage()

$scope.responseModeEnum = responseModeEnum
$scope.tempForm = createTempSettings($scope.myform)
Expand Down
7 changes: 3 additions & 4 deletions src/public/modules/forms/config/forms.client.routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const ExamplesService = require('../../../services/ExamplesService')
const UserService = require('../../../services/UserService')

// Setting up route
angular.module('forms').config([
Expand Down Expand Up @@ -74,16 +75,14 @@ angular.module('forms').config([
url: '/{formId:[0-9a-fA-F]{24}}/use-template',
templateUrl: 'modules/users/views/examples.client.view.html',
resolve: {
Auth: 'Auth',
FormErrorService: 'FormErrorService',
// If the user is logged in, this field will contain the form data of the provided formId,
// otherwise it will only contain the formId itself.
FormData: [
'Auth',
'FormErrorService',
'$stateParams',
function (Auth, FormErrorService, $stateParams) {
if (!Auth.getUser()) {
function (FormErrorService, $stateParams) {
if (!UserService.getUserFromLocalStorage()) {
return $stateParams.formId
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,11 @@ angular
'$state',
'$timeout',
'$window',
'Auth',
'GTag',
AuthenticationController,
])

function AuthenticationController(
$q,
$scope,
$state,
$timeout,
$window,
Auth,
GTag,
) {
function AuthenticationController($q, $scope, $state, $timeout, $window, GTag) {
const vm = this

let notifDelayTimeout
Expand Down Expand Up @@ -73,7 +64,7 @@ function AuthenticationController(
* Redirects user with active session to target page
*/
vm.redirectIfActiveSession = function () {
if (Auth.getUser()) {
if (UserService.getUserFromLocalStorage()) {
$state.go($state.params.targetState)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

const { CsvGenerator } = require('../../forms/helpers/CsvGenerator')
const BillingService = require('../../../services/BillingService')
const UserService = require('../../../services/UserService')

angular
.module('users')
.controller('BillingController', [
'$q',
'$state',
'$timeout',
'Auth',
'NgTableParams',
BillingController,
])

function BillingController($q, $state, $timeout, Auth, NgTableParams) {
function BillingController($q, $state, $timeout, NgTableParams) {
const vm = this

vm.user = Auth.getUser()
vm.user = UserService.getUserFromLocalStorage()

// Send non-logged in personnel to sign in page
if (!vm.user) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict'
const BetaService = require('../../../services/BetaService')

const UserService = require('../../../services/UserService')

angular.module('users').directive('examplesCard', [examplesCard])

function examplesCard() {
Expand All @@ -20,7 +22,6 @@ function examplesCard() {
'$uibModal',
'$state',
'GTag',
'Auth',
'$location',
'Toastr',
'$q',
Expand All @@ -36,12 +37,11 @@ function examplesCardController(
$uibModal,
$state,
GTag,
Auth,
$location,
Toastr,
$q,
) {
$scope.user = Auth.getUser()
$scope.user = UserService.getUserFromLocalStorage()

$scope.openTemplateModal = () => {
$scope.templateUrl = $state.href(
Expand Down
Loading

0 comments on commit 279bce5

Please sign in to comment.