Skip to content

Commit

Permalink
Merge pull request #2090 from opengovsg/feat/authservice-logout-ts
Browse files Browse the repository at this point in the history
refactor(auth.client): (4) extract admin logout service function to Typescript
  • Loading branch information
karrui authored Jun 10, 2021
2 parents ad3cf63 + 4b50c69 commit c3b3c22
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,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,11 +1,13 @@
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',
Expand All @@ -19,6 +21,7 @@ angular.module('core').component('avatarDropdownComponent', {
})

function avatarDropdownController(
$q,
$scope,
$state,
$uibModal,
Expand Down Expand Up @@ -92,7 +95,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 @@ -109,7 +125,7 @@ function avatarDropdownController(
// Update success, update user.
if (returnVal) {
vm.user = returnVal
Auth.setUser(returnVal)
UserService.saveUserToLocalStorage(returnVal)
vm.showExclamation = !returnVal.contact
}
})
Expand Down
23 changes: 0 additions & 23 deletions src/public/modules/users/services/auth.client.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,9 @@ function Auth($q, $http, $state, $window) {

let authService = {
getUser,
setUser,
signOut,
}
return authService

/**
* User setter function
* @param {User} user
*/
function setUser(user) {
$window.localStorage.setItem('user', JSON.stringify(user))
}

/**
* User getter function
* @returns {User} user
Expand All @@ -49,17 +39,4 @@ function Auth($q, $http, $state, $window) {
return null
}
}
function signOut() {
$http.get('/api/v3/auth/logout').then(
function () {
$window.localStorage.removeItem('user')
// Clear contact banner on logout
$window.localStorage.removeItem('contactBannerDismissed')
$state.go('landing')
},
function (error) {
console.error('sign out failed:', error)
},
)
}
}
4 changes: 4 additions & 0 deletions src/public/services/AuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ export const verifyLoginOtp = async (params: {
.post<User>(`${AUTH_ENDPOINT}/otp/verify`, params)
.then(({ data }) => data)
}

export const logout = async (): Promise<void> => {
return axios.get(`${AUTH_ENDPOINT}/logout`)
}
3 changes: 2 additions & 1 deletion src/public/services/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export const clearUserFromLocalStorage = (): void => {
/**
* Fetches the user from the server using the current session cookie.
*
* Side effect: Saves the retrieved user to localStorage
* Side effect: On success, save the retrieved user to localStorage.
* Side effect: On error, clear the user (if any) from localStorage.
* @returns the logged in user if session is valid, `null` otherwise
*/
export const fetchUser = async (): Promise<User | null> => {
Expand Down
17 changes: 17 additions & 0 deletions src/public/services/__tests__/AuthService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Opaque } from 'type-fest'
import {
AUTH_ENDPOINT,
checkIsEmailAllowed,
logout,
sendLoginOtp,
verifyLoginOtp,
} from '../AuthService'
Expand Down Expand Up @@ -83,4 +84,20 @@ describe('AuthService', () => {
)
})
})

describe('logout', () => {
const EXPECTED_ENDPOINT = `${AUTH_ENDPOINT}/logout`
it('should call endpoint successfully', async () => {
// Arrange
const mockReturn = { status: 200 }
MockAxios.get.mockResolvedValueOnce(mockReturn)

// Act
const actual = await logout()

// Assert
expect(actual).toEqual(mockReturn)
expect(MockAxios.get).toHaveBeenLastCalledWith(EXPECTED_ENDPOINT)
})
})
})

0 comments on commit c3b3c22

Please sign in to comment.