-
Notifications
You must be signed in to change notification settings - Fork 143
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
[v3] Add multi-site suffix to auth token keys #1208
Changes from 7 commits
6edcc16
1d05c36
f3558d9
86349a6
932c9ed
546a8c6
6f420fb
e716539
efe77be
0f5d27f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,25 +9,23 @@ import Cookies from 'js-cookie' | |
export type StorageType = 'cookie' | 'local' | 'memory' | ||
|
||
export interface BaseStorageOptions { | ||
keyPrefix?: string | ||
keyPrefixSeparator?: string | ||
keySuffix?: string | ||
} | ||
|
||
export interface MemoryStorageOptions extends BaseStorageOptions { | ||
sharedContext?: boolean | ||
} | ||
export abstract class BaseStorage { | ||
protected options: Required<BaseStorageOptions> | ||
protected options: BaseStorageOptions = {} | ||
|
||
constructor(options: BaseStorageOptions = {keyPrefixSeparator: '_'}) { | ||
constructor(options?: BaseStorageOptions) { | ||
this.options = { | ||
keyPrefixSeparator: options.keyPrefix ? options.keyPrefixSeparator ?? '_' : '', | ||
keyPrefix: options.keyPrefix ?? '' | ||
keySuffix: options?.keySuffix ?? '' | ||
} | ||
} | ||
|
||
protected getPrefixedKey(key: string): string { | ||
return `${this.options.keyPrefix}${this.options.keyPrefixSeparator}${key}` | ||
protected getSuffixedKey(key: string): string { | ||
return this.options.keySuffix ? `${key}_${this.options.keySuffix}` : key | ||
} | ||
abstract set(key: string, value: string, options?: unknown): void | ||
abstract get(key: string): string | ||
|
@@ -49,16 +47,16 @@ export class CookieStorage extends BaseStorage { | |
} | ||
} | ||
set(key: string, value: string, options?: Cookies.CookieAttributes) { | ||
const prefixedKey = this.getPrefixedKey(key) | ||
Cookies.set(prefixedKey, value, {...options, secure: true}) | ||
const suffixedKey = this.getSuffixedKey(key) | ||
Cookies.set(suffixedKey, value, {...options, secure: true}) | ||
} | ||
get(key: string) { | ||
const prefixedKey = this.getPrefixedKey(key) | ||
return Cookies.get(prefixedKey) || '' | ||
const suffixedKey = this.getSuffixedKey(key) | ||
return Cookies.get(suffixedKey) || '' | ||
} | ||
delete(key: string) { | ||
const prefixedKey = this.getPrefixedKey(key) | ||
Cookies.remove(prefixedKey) | ||
const suffixedKey = this.getSuffixedKey(key) | ||
Cookies.remove(suffixedKey) | ||
} | ||
} | ||
|
||
|
@@ -77,26 +75,26 @@ export class LocalStorage extends BaseStorage { | |
} | ||
} | ||
set(key: string, value: string) { | ||
const prefixedKey = this.getPrefixedKey(key) | ||
const oldValue = this.get(prefixedKey) | ||
window.localStorage.setItem(prefixedKey, value) | ||
const suffixedKey = this.getSuffixedKey(key) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct me if I am wrong, it looks like we are replace prefix with suffix for all the keys in storage? I thought originally we only want to do it for site_id key, not every key. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is only 1 use case for prefix/suffix and it is the siteId. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get that for the side_id use case, but I was talking about if we add another key, with the changes here means it will apply for any key that need to have suffix? 🤔 , so basically we move from using prefix to suffix for any key that uses it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's right... unless there is a use case for prefix, this change moves from using prefix to suffix for all keys. |
||
const oldValue = this.get(suffixedKey) | ||
window.localStorage.setItem(suffixedKey, value) | ||
const event = new StorageEvent('storage', { | ||
key: prefixedKey, | ||
key: suffixedKey, | ||
oldValue: oldValue, | ||
newValue: value | ||
}) | ||
window.dispatchEvent(event) | ||
} | ||
get(key: string) { | ||
const prefixedKey = this.getPrefixedKey(key) | ||
return window.localStorage.getItem(prefixedKey) || '' | ||
const suffixedKey = this.getSuffixedKey(key) | ||
return window.localStorage.getItem(suffixedKey) || '' | ||
} | ||
delete(key: string) { | ||
const prefixedKey = this.getPrefixedKey(key) | ||
const oldValue = this.get(prefixedKey) | ||
window.localStorage.removeItem(prefixedKey) | ||
const suffixedKey = this.getSuffixedKey(key) | ||
const oldValue = this.get(suffixedKey) | ||
window.localStorage.removeItem(suffixedKey) | ||
const event = new StorageEvent('storage', { | ||
key: prefixedKey, | ||
key: suffixedKey, | ||
oldValue: oldValue, | ||
newValue: null | ||
}) | ||
|
@@ -114,15 +112,15 @@ export class MemoryStorage extends BaseStorage { | |
this.map = options?.sharedContext ? globalMap : new Map() | ||
} | ||
set(key: string, value: string) { | ||
const prefixedKey = this.getPrefixedKey(key) | ||
this.map.set(prefixedKey, value) | ||
const suffixedKey = this.getSuffixedKey(key) | ||
this.map.set(suffixedKey, value) | ||
} | ||
get(key: string) { | ||
const prefixedKey = this.getPrefixedKey(key) | ||
return this.map.get(prefixedKey) || '' | ||
const suffixedKey = this.getSuffixedKey(key) | ||
return this.map.get(suffixedKey) || '' | ||
} | ||
delete(key: string) { | ||
const prefixedKey = this.getPrefixedKey(key) | ||
this.map.delete(prefixedKey) | ||
const suffixedKey = this.getSuffixedKey(key) | ||
this.map.delete(suffixedKey) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove the option as changing the separator will break the compatibility with plugin_slas, we don't want to give user the option to do this.