Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(auth.client.service): refactor to Typescript #2132

Merged
merged 33 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
35767f0
feat(AuthService): add verifyLoginOtp fn
karrui Jun 3, 2021
5e9897c
ref: use AdminAuthService.verifyLoginOtp
karrui Jun 3, 2021
2cfa8c4
ref: rename Admin to User
karrui Jun 3, 2021
64b350c
ref(AuthService): move User related fns to UserService
karrui Jun 3, 2021
427b6ac
test(AuthService): add unit tests for verifyLoginOtp
karrui Jun 7, 2021
6ab3771
chore: add jest-localstorage-mock package
karrui Jun 7, 2021
c7fadc8
test(UserService): add unit tests for saveUserToLocalStorage
karrui Jun 7, 2021
fe4f5aa
chore: move date-fns from devDeps to deps
karrui Jun 7, 2021
442be13
feat: add base typings for Agency and User
karrui Jun 7, 2021
82723dc
feat: remove unused verifyOtp function
karrui Jun 7, 2021
4fc4570
chore: set VSCode default TS version to follow node_modules
karrui Jun 7, 2021
aa596c9
ref: move saveUserToLocalStorage responsibility to calling controller
karrui Jun 8, 2021
48dbe66
feat(UserService): add fetchUser function
karrui Jun 3, 2021
e1d3677
ref(avatar-dropdown.client): use UserService.fetchUser()
karrui Jun 3, 2021
0421682
test(UserService): add unit tests for fetchUser
karrui Jun 7, 2021
150b6ed
feat: remove unused refreshUser function
karrui Jun 7, 2021
ee8fce5
feat(UserService): add clearUserFromLocalStorage function (and tests)
karrui Jun 8, 2021
86ba290
feat: move responsibility of clearing user from localStorage to ctl
karrui Jun 8, 2021
6a90837
fix: correctly set truser when fetching user from server
karrui Jun 8, 2021
e2e9425
ref: use UserService.saveUserToLocalStorage
karrui Jun 3, 2021
501c9b5
feat(AuthService): add and use logout function
karrui Jun 3, 2021
ead3fcc
feat: remove unused Auth.signOut and setUser functions
karrui Jun 7, 2021
4b50c69
test(AuthService): add unit test for logout
karrui Jun 7, 2021
f7a5dda
Merge pull request #2086 from opengovsg/feat/authservice-verify-users…
karrui Jun 10, 2021
ad3cf63
Merge pull request #2089 from opengovsg/feat/authservice-fetch-user-ts
karrui Jun 10, 2021
c3b3c22
Merge pull request #2090 from opengovsg/feat/authservice-logout-ts
karrui Jun 10, 2021
352368f
feat(UserService): add getUserFromLocalStorage function
karrui Jun 4, 2021
aea0048
ref: replace all instances of Auth.getUser with UserService equivalent
karrui Jun 4, 2021
f881aa9
feat: delete unused auth.client.service.js
karrui Jun 4, 2021
42c9e36
test(UserService): add unit tests for getUserFromLocalStorage
karrui Jun 7, 2021
62b4e23
chore: run npm run lint
karrui Jun 7, 2021
279bce5
Merge pull request #2091 from opengovsg/feat/authservice-getuser-ts
karrui Jun 10, 2021
215598a
Merge branch 'develop' into feat/authsvc-ts-base
karrui Jun 10, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
"source.fixAll.eslint": false
},
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
},
"typescript.tsdk": "node_modules/typescript/lib"
}
9 changes: 7 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"cookie-parser": "~1.4.0",
"css-toggle-switch": "^4.1.0",
"csv-string": "^4.0.1",
"date-fns": "^2.22.1",
"dedent-js": "~1.0.1",
"ejs": "^3.1.6",
"express": "^4.16.4",
Expand Down Expand Up @@ -205,7 +206,6 @@
"coveralls": "^3.1.0",
"css-loader": "^2.1.1",
"csv-parse": "^4.16.0",
"date-fns": "^2.22.1",
"env-cmd": "^10.1.0",
"eslint": "^7.28.0",
"eslint-config-prettier": "^8.3.0",
Expand All @@ -222,6 +222,7 @@
"husky": "^6.0.0",
"jest": "^26.6.3",
"jest-extended": "^0.11.5",
"jest-localstorage-mock": "^2.4.14",
"jest-mock-axios": "^4.4.0",
"lint-staged": "^11.0.0",
"maildev": "^1.1.0",
Expand Down
3 changes: 0 additions & 3 deletions src/public/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,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 @@ -64,7 +64,7 @@
<!-- Logout block -->
<li
class="navbar__dropdown--clickable navbar__dropdown__logout"
ng-click="vm.signOut()"
ng-click="vm.logout()"
>
<span class="navbar__dropdown__logout--text">Logout</span>
<i class="bx bx-log-out bx-md"></i>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
const get = require('lodash/get')

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

angular.module('core').component('avatarDropdownComponent', {
templateUrl: 'modules/core/componentViews/avatar-dropdown.html',
bindings: {},
controller: [
'$q',
'$scope',
'$state',
'$uibModal',
'$window',
'Auth',
'Toastr',
avatarDropdownController,
],
controllerAs: 'vm',
})

function avatarDropdownController(
$q,
$scope,
$state,
$uibModal,
$window,
Auth,
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 All @@ -37,7 +40,16 @@ function avatarDropdownController(

async function retrieveUser() {
try {
const trueUser = await Auth.refreshUser()
const trueUser = await UserService.fetchUser()
.then((user) => {
UserService.saveUserToLocalStorage(user)
return user
})
.catch(() => {
UserService.clearUserFromLocalStorage()
return null
})

if (!trueUser) {
$state.go('signin')
return
Expand Down Expand Up @@ -74,7 +86,20 @@ function avatarDropdownController(
},
)

vm.signOut = () => Auth.signOut()
vm.logout = () => {
return $q
.when(AuthService.logout())
.then(() => {
// Clear user and contact banner on logout
UserService.clearUserFromLocalStorage()
$window.localStorage.removeItem('contactBannerDismissed')
// Redirect to landing page
$state.go('landing')
})
.catch((error) => {
console.error('sign out failed:', error)
})
}

vm.openContactNumberModal = () => {
$uibModal
Expand All @@ -91,7 +116,7 @@ function avatarDropdownController(
// Update success, update user.
if (returnVal) {
vm.user = returnVal
Auth.setUser(returnVal)
UserService.saveUserToLocalStorage(returnVal)
vm.showExclamation = !returnVal.contact
}
})
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
Loading