-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: transaction manager * refactor: rename to SessionManager * chore: optional * chore: use full name for vars * refactor: use SESSION_EXPIRES_SECONDS * fix: remove property session to keep SSOT * test: remove constructer test
- Loading branch information
Showing
2 changed files
with
61 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import SessionManager from './session-manager'; | ||
import { SessionStorage } from './storage'; | ||
|
||
const SESSION_MANAGER_KEY = 'LOGTO_SESSION_MANAGER'; | ||
|
||
const transaction = { | ||
codeVerifier: 'codeVerifier', | ||
redirectUri: 'redirectUri', | ||
}; | ||
|
||
describe('SessionManager', () => { | ||
test('create and get', () => { | ||
const sessionStorage = new SessionStorage(); | ||
const sessionManager = new SessionManager(sessionStorage); | ||
sessionManager.create(transaction); | ||
expect(sessionManager.get()).toMatchObject(transaction); | ||
}); | ||
|
||
test('`create` should save data in storage', () => { | ||
const sessionStorage = new SessionStorage(); | ||
const sessionManager = new SessionManager(sessionStorage); | ||
sessionManager.create(transaction); | ||
expect(sessionStorage.getItem(SESSION_MANAGER_KEY)).toMatchObject(transaction); | ||
}); | ||
|
||
test('restore data from storage', () => { | ||
const sessionStorage = new SessionStorage(); | ||
sessionStorage.setItem(SESSION_MANAGER_KEY, transaction); | ||
const sessionManager = new SessionManager(sessionStorage); | ||
expect(sessionManager.get()).toMatchObject(transaction); | ||
}); | ||
}); |
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,29 @@ | ||
import { Optional } from '@silverhand/essentials'; | ||
|
||
import { ClientStorage } from './storage'; | ||
|
||
const SESSEION_MANAGER_KEY = 'LOGTO_SESSION_MANAGER'; | ||
const SESSION_EXPIRES_SECONDS = 86_400; | ||
|
||
interface Session { | ||
codeVerifier: string; | ||
redirectUri: string; | ||
} | ||
|
||
export default class SessionManager { | ||
constructor(private readonly storage: ClientStorage) {} | ||
|
||
public create(session: Session) { | ||
this.storage.setItem(SESSEION_MANAGER_KEY, session, { | ||
secondsUntilExpire: SESSION_EXPIRES_SECONDS, | ||
}); | ||
} | ||
|
||
public get(): Optional<Session> { | ||
return this.storage.getItem(SESSEION_MANAGER_KEY); | ||
} | ||
|
||
public remove() { | ||
this.storage.removeItem(SESSEION_MANAGER_KEY); | ||
} | ||
} |