core-app-api: make AuthSessionStore implement MutableSessionManager

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2021-08-20 15:44:58 +02:00
parent 7a6373e080
commit eae4656cb8
2 changed files with 19 additions and 2 deletions
@@ -128,6 +128,19 @@ describe('GheAuth AuthSessionStore', () => {
expect(manager.removeSession).toHaveBeenCalled();
});
it('should set session', async () => {
const manager = new MockManager();
const store = new AuthSessionStore({ manager, ...defaultOptions });
await expect(store.getSession({ optional: true })).resolves.toBe(undefined);
expect(localStorage.getItem('my-key')).toBe(null);
expect(manager.setSession).not.toHaveBeenCalled();
store.setSession('123');
expect(manager.setSession).toHaveBeenCalled();
expect(localStorage.getItem('my-key')).toBe('"123"');
await expect(store.getSession({ optional: true })).resolves.toBe('123');
});
it('should forward sessionState calls', () => {
const manager = new MockManager();
const store = new AuthSessionStore({ manager, ...defaultOptions });
@@ -15,7 +15,6 @@
*/
import {
SessionManager,
MutableSessionManager,
SessionScopesFunc,
SessionShouldRefreshFunc,
@@ -40,7 +39,7 @@ type Options<T> = {
*
* Session is serialized to JSON with special support for following types: Set.
*/
export class AuthSessionStore<T> implements SessionManager<T> {
export class AuthSessionStore<T> implements MutableSessionManager<T> {
private readonly manager: MutableSessionManager<T>;
private readonly storageKey: string;
private readonly sessionShouldRefreshFunc: SessionShouldRefreshFunc<T>;
@@ -63,6 +62,11 @@ export class AuthSessionStore<T> implements SessionManager<T> {
});
}
setSession(session: T | undefined): void {
this.manager.setSession(session);
this.saveSession(session);
}
async getSession(options: GetSessionOptions): Promise<T | undefined> {
const { scopes } = options;
const session = this.loadSession();