-
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.
Merge pull request #78 from trustpilot/add-session-persister-to-config
Add session persister to config
- Loading branch information
Showing
4 changed files
with
51 additions
and
33 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,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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,35 @@ | ||
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(); | ||
} | ||
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); | ||
} | ||
if (c.indexOf(nameEq) === 0) { | ||
return c.substring(nameEq.length, c.length); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
export class CookiePersister implements UserSessionPersister { | ||
public loadUserSession() { | ||
return readCookie(config.cookieName); | ||
} | ||
|
||
public saveUserSession(userSession: string, daysToLive: number) { | ||
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(); | ||
} | ||
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); | ||
} | ||
} |