-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove auth token reissue and inter-tab synchronization logic
Also remove the in-memory caching of the auth tokens. We need to rethink how authorization works in multiple open tabs, see #1271
- Loading branch information
Showing
6 changed files
with
11 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,22 @@ | ||
/* global CONFIG */ | ||
import storage from 'local-storage-fallback'; | ||
import { nanoid } from 'nanoid'; | ||
import { reissueAuthSession } from '../redux/action-creators'; | ||
|
||
import { authDebug } from '../utils/debug'; | ||
import { Lock } from '../utils/storage-lock'; | ||
|
||
const { tokenPrefix } = CONFIG.auth; | ||
const NAME = `${tokenPrefix}authToken`; | ||
|
||
let token; | ||
|
||
export function getToken(force = false) { | ||
if (token === undefined || force) { | ||
authDebug('loading token from the storage'); | ||
token = storage.getItem(NAME); | ||
} | ||
return token; | ||
export function getToken() { | ||
authDebug('loading token from the storage'); | ||
return storage.getItem(NAME); | ||
} | ||
|
||
export function setToken(newToken = null, save = true) { | ||
if (save) { | ||
if (!newToken) { | ||
authDebug('cleaning token up'); | ||
storage.removeItem(NAME); | ||
} else { | ||
authDebug('saving token to the storage'); | ||
storage.setItem(NAME, newToken); | ||
} | ||
export function setToken(newToken = null) { | ||
if (!newToken) { | ||
authDebug('cleaning token up'); | ||
storage.removeItem(NAME); | ||
} else { | ||
authDebug('saving token to the storage'); | ||
storage.setItem(NAME, newToken); | ||
} | ||
authDebug('updating token in memory'); | ||
token = newToken; | ||
} | ||
|
||
export function onStorageChange(callback) { | ||
window?.addEventListener('storage', (e) => e.key === NAME && callback(e.newValue)); | ||
} | ||
|
||
const lock = new Lock( | ||
storage, | ||
`${tokenPrefix}lock`, | ||
nanoid(), | ||
CONFIG.authSessions.lockIntervalSec * 1000, | ||
); | ||
|
||
let tokenReissueTimer = 0; | ||
|
||
export function scheduleTokenReissue(store) { | ||
const interval = CONFIG.authSessions.reissueIntervalSec * 1000 * (1 + Math.random() / 3); | ||
authDebug(`scheduling token reissue at ${interval / 1000} seconds`); | ||
clearTimeout(tokenReissueTimer); | ||
tokenReissueTimer = setTimeout(() => { | ||
if (store.getState().authenticated) { | ||
authDebug('trying to reissue token'); | ||
if (lock.try()) { | ||
authDebug('start token reissue'); | ||
store.dispatch(reissueAuthSession()); | ||
} else { | ||
authDebug('lock detecting, skipping reissue'); | ||
} | ||
} | ||
scheduleTokenReissue(store); | ||
}, interval); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters