-
-
Notifications
You must be signed in to change notification settings - Fork 737
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: store redirect to localStorage to avoid loss of redirect (#5929)
Stores `redirect` param in localStorage in the Authentication component. Retrieves the `redirect` param from localStorage at the Login screen if it is not there in the url. This will solve losing the redirect information all provider logins Closes # [1-1890](https://linear.app/unleash/issue/1-1890/capture-path-before-logging-in-and-redirect-to-it-if-there-and-custom) --------- Signed-off-by: andreas-unleash <[email protected]>
- Loading branch information
1 parent
4ee2acb
commit a096b2a
Showing
4 changed files
with
201 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { | ||
setLocalStorageItem, | ||
getLocalStorageItem, | ||
setSessionStorageItem, | ||
getSessionStorageItem, | ||
} from './storage'; | ||
import { vi } from 'vitest'; | ||
|
||
// Mocking the global localStorage | ||
const localStorageMock = (() => { | ||
let store: Record<string, string> = {}; | ||
|
||
return { | ||
getItem(key: string) { | ||
return store[key] || null; | ||
}, | ||
setItem(key: string, value: string) { | ||
store[key] = value.toString(); | ||
}, | ||
removeItem(key: string) { | ||
delete store[key]; | ||
}, | ||
clear() { | ||
store = {}; | ||
}, | ||
}; | ||
})(); | ||
|
||
const sessionStorageMock = (() => { | ||
let store: Record<string, string> = {}; | ||
|
||
return { | ||
getItem(key: string) { | ||
return store[key] || null; | ||
}, | ||
setItem(key: string, value: string) { | ||
store[key] = value.toString(); | ||
}, | ||
removeItem(key: string) { | ||
delete store[key]; | ||
}, | ||
clear() { | ||
store = {}; | ||
}, | ||
}; | ||
})(); | ||
|
||
Object.defineProperty(window, 'localStorage', { | ||
value: localStorageMock, | ||
}); | ||
|
||
Object.defineProperty(window, 'sessionStorage', { | ||
value: sessionStorageMock, | ||
}); | ||
|
||
// Test suite | ||
describe('localStorage with TTL', () => { | ||
beforeEach(() => { | ||
localStorage.clear(); | ||
vi.useFakeTimers(); | ||
}); | ||
|
||
test('item should be retrievable before TTL expires', () => { | ||
setLocalStorageItem('testKey', 'testValue', 600000); | ||
expect(getLocalStorageItem('testKey')).toBe('testValue'); | ||
}); | ||
|
||
test('item should not be retrievable after TTL expires', () => { | ||
setLocalStorageItem('testKey', 'testValue', 500000); | ||
|
||
vi.advanceTimersByTime(600000); | ||
|
||
expect(getLocalStorageItem('testKey')).toBeUndefined(); | ||
}); | ||
test('object should be retrievable before TTL expires', () => { | ||
const testObject = { name: 'Test', number: 123 }; | ||
setLocalStorageItem('testObjectKey', testObject, 600000); | ||
|
||
const retrievedObject = getLocalStorageItem<{ | ||
name: string; | ||
number: number; | ||
}>('testObjectKey'); | ||
expect(retrievedObject).toEqual(testObject); | ||
}); | ||
|
||
test('object should not be retrievable after TTL expires', () => { | ||
const testObject = { name: 'Test', number: 123 }; | ||
setLocalStorageItem('testObjectKey', testObject, 500000); | ||
|
||
vi.advanceTimersByTime(600000); | ||
|
||
const retrievedObject = getLocalStorageItem<{ | ||
name: string; | ||
number: number; | ||
}>('testObjectKey'); | ||
expect(retrievedObject).toBeUndefined(); | ||
}); | ||
|
||
test('object should be retrievable before TTL expires in sessionStorage', () => { | ||
const testObject = { name: 'TestSession', number: 456 }; | ||
setSessionStorageItem('testObjectKeySession', testObject, 500000); | ||
|
||
const retrievedObject = getSessionStorageItem<typeof testObject>( | ||
'testObjectKeySession', | ||
); | ||
expect(retrievedObject).toEqual(testObject); | ||
}); | ||
|
||
test('object should not be retrievable after TTL expires in sessionStorage', () => { | ||
const testObject = { name: 'TestSession', number: 456 }; | ||
setSessionStorageItem('testObjectKeySession', testObject, 500000); // 10 minutes TTL | ||
|
||
vi.advanceTimersByTime(600000); | ||
|
||
const retrievedObject = getSessionStorageItem<typeof testObject>( | ||
'testObjectKeySession', | ||
); | ||
expect(retrievedObject).toBeUndefined(); | ||
}); | ||
}); |
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