-
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
8b8651d
commit d55e76e
Showing
3 changed files
with
37 additions
and
27 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 |
---|---|---|
@@ -1,40 +1,46 @@ | ||
import config from './config'; | ||
|
||
export interface UserSessionPersister { | ||
loadUserSession(): string | null; | ||
saveUserSession(userSession: string, daysToLive: number): void; | ||
} | ||
|
||
function 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(); | ||
export class CookiePersister implements UserSessionPersister { | ||
public loadUserSession() { | ||
return CookiePersister.readCookie(config.cookieName); | ||
} | ||
document.cookie = name + '=' + value + expires + '; path=/'; | ||
} | ||
|
||
function 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); | ||
public saveUserSession(userSession: string, daysToLive: number) { | ||
CookiePersister.createCookie(config.cookieName, userSession, daysToLive); | ||
} | ||
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(); | ||
} | ||
if (c.indexOf(nameEq) === 0) { | ||
return c.substring(nameEq.length, c.length); | ||
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; | ||
} | ||
return null; | ||
} | ||
|
||
export class CookiePersister implements UserSessionPersister { | ||
export class InMemoryPersister implements UserSessionPersister { | ||
private endOfLife: number = 0; | ||
private storage: string = ''; | ||
public loadUserSession() { | ||
return readCookie(config.cookieName); | ||
return !this.endOfLife || (Date.now() > this.endOfLife) ? '' : this.storage; | ||
} | ||
|
||
public saveUserSession(userSession: string, daysToLive: number) { | ||
createCookie(config.cookieName, userSession, daysToLive); | ||
this.storage = userSession; | ||
this.endOfLife = Date.now() + (daysToLive*24*60*60*1000); | ||
} | ||
} | ||
} |