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

[WIP] Improve logout #227

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"start": "NODE_ENV=production next start --port 3001",
"test": "yarn run relay && yarn run lint && yarn run jest --testPathIgnorePatterns=e2e",
"test:e2e": "dotenv-extended --defaults=./.env --path=./.env.local yarn run jest e2e-tests.test.js",
"test:watch": "yarn run relay && jest --watch",
"test:watch": "yarn run test --watch",
"test:coverage": "yarn run test --coverage",
"format": "prettier \"**/*.{js,jsx,ts,tsx,json,css,scss,md}\"",
"format:fix": "yarn run format --write",
Expand Down
23 changes: 23 additions & 0 deletions src/utils/__mocks__/localstorage-mgr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isNil } from 'lodash/lang'

let mockStorage = {}

// eslint-disable-next-line no-underscore-dangle
export const __mockClear = () => {
mockStorage = {}
}

export default {
getItem: jest.fn((key) => mockStorage[key]),
setItem: jest.fn((key, val) => {
if (!isNil(val)) {
mockStorage[key] = String(val)
}
}),
removeItem: jest.fn((key) => {
delete mockStorage[key]
}),
clear: jest.fn(() => {
__mockClear()
}),
}
14 changes: 7 additions & 7 deletions src/utils/auth/__tests__/logout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,34 @@ import getMockAuthUser from 'src/utils/testHelpers/getMockAuthUser'
import { clearAllServiceWorkerCaches } from 'src/utils/caching'
import logger from 'src/utils/logger'
import localStorageMgr from 'src/utils/localstorage-mgr'
import { STORAGE_KEY_USERNAME } from 'src/utils/constants'

jest.mock('src/utils/caching')
jest.mock('src/utils/logger')
jest.mock('src/utils/localstorage-mgr', () => ({ removeItem: jest.fn() }))
jest.mock('src/utils/localstorage-mgr')
afterEach(() => {
jest.clearAllMocks()
})

describe('logout.js', () => {
it('calls AuthUser.signOut', async () => {
expect.assertions(1)
clearAllServiceWorkerCaches.mockResolvedValueOnce()
const mockAuthUser = getMockAuthUser()
const logout = require('src/utils/auth/logout').default
await logout(mockAuthUser)
expect(mockAuthUser.signOut).toHaveBeenCalled()
})

it('calls clearAllServiceWorkerCaches', async () => {
it('clears local storage', async () => {
expect.assertions(1)
clearAllServiceWorkerCaches.mockResolvedValueOnce()
const mockAuthUser = getMockAuthUser()
const logout = require('src/utils/auth/logout').default
await logout(mockAuthUser)
expect(localStorageMgr.removeItem).toHaveBeenCalledWith(
STORAGE_KEY_USERNAME
)
expect(localStorageMgr.clear).toHaveBeenCalled()
})

it('calls local storage remove item', async () => {
it('calls clearAllServiceWorkerCaches', async () => {
expect.assertions(1)
const mockAuthUser = getMockAuthUser()
const logout = require('src/utils/auth/logout').default
Expand All @@ -52,6 +51,7 @@ describe('logout.js', () => {

it('resolves to `true` on success', async () => {
expect.assertions(1)
clearAllServiceWorkerCaches.mockResolvedValueOnce()
const mockAuthUser = getMockAuthUser()
const logout = require('src/utils/auth/logout').default
const result = await logout(mockAuthUser)
Expand Down
12 changes: 7 additions & 5 deletions src/utils/auth/logout.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { clearAllServiceWorkerCaches } from 'src/utils/caching'
import logger from 'src/utils/logger'
import localStorageMgr from 'src/utils/localstorage-mgr'
import { STORAGE_KEY_USERNAME } from 'src/utils/constants'

const logout = async (AuthUser) => {
try {
await AuthUser.signOut()

// Clear the cache so it does not contain any authed content.
// Clear the cache and local data so it does not contain any
// authed content.
await clearAllServiceWorkerCaches()
await localStorageMgr.removeItem(STORAGE_KEY_USERNAME)
localStorageMgr.clear()

// Do the above first, because signing out will trigger a redirect
// to the auth page via next-firebase-auth.
await AuthUser.signOut()
return true
} catch (e) {
logger.error(e)
Expand Down