-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nicolas Janvier
committed
Sep 3, 2020
1 parent
d55e76e
commit 4269867
Showing
6 changed files
with
53 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import config from './config'; | ||
import { UserSessionPersister } from './userSessionPersister'; | ||
|
||
export class CookiePersister implements UserSessionPersister { | ||
private static createCookie(name: string, value: string, days: number): void { | ||
let expires = ''; | ||
if (days) { | ||
const date = new Date(); | ||
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); | ||
expires = '; expires=' + date.toUTCString(); | ||
} | ||
document.cookie = name + '=' + value + expires + '; path=/'; | ||
} | ||
private static readCookie(name: string): string | null { | ||
const nameEq = name + '='; | ||
const ca = document.cookie.split(';'); | ||
for (let c of ca) { | ||
while (c.charAt(0) === ' ') { | ||
c = c.substring(1, c.length); | ||
} | ||
if (c.indexOf(nameEq) === 0) { | ||
return c.substring(nameEq.length, c.length); | ||
} | ||
} | ||
return null; | ||
} | ||
public loadUserSession() { | ||
return CookiePersister.readCookie(config.cookieName); | ||
} | ||
public saveUserSession(userSession: string, daysToLive: number) { | ||
CookiePersister.createCookie(config.cookieName, userSession, daysToLive); | ||
} | ||
} |
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,13 @@ | ||
import { UserSessionPersister } from './userSessionPersister'; | ||
|
||
export class InMemoryPersister implements UserSessionPersister { | ||
private endOfLife: number = 0; | ||
private storage: string = ''; | ||
public loadUserSession() { | ||
return !this.endOfLife || (Date.now() > this.endOfLife) ? '' : this.storage; | ||
} | ||
public saveUserSession(userSession: string, daysToLive: number) { | ||
this.storage = userSession; | ||
this.endOfLife = Date.now() + (daysToLive * 24 * 60 * 60 * 1000); | ||
} | ||
} |
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