-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create sessionstore and use it in sessionprovider
- Loading branch information
1 parent
33f7c21
commit afb6b70
Showing
17 changed files
with
155 additions
and
143 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 was deleted.
Oops, something went wrong.
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,64 +1,75 @@ | ||
import { SessionProvider } from "../abstracts/session_provider"; | ||
import { promiseResolve } from "../utils"; | ||
import { FORT_GLOBAL } from "../constants"; | ||
import { ISessonStore } from "../interfaces"; | ||
import { CookieManager } from "../models"; | ||
import * as getUniqId from "uniqid"; | ||
import { TSessionStore } from "../types"; | ||
|
||
const sessionValues: Map<string, { [key: string]: any }> = new Map(); | ||
|
||
export class MemorySessionProvider extends SessionProvider { | ||
export class MemorySessionProvider { | ||
|
||
private getSessionValue_() { | ||
return sessionValues.get(this.sessionId); | ||
sessionId: string; | ||
protected cookie: CookieManager; | ||
sessionStore: ISessonStore; | ||
|
||
constructor(cookie: CookieManager, sessionStore: TSessionStore) { | ||
this.sessionId = cookie.cookieCollection[FORT_GLOBAL.appSessionIdentifier]; | ||
this.sessionStore = new sessionStore(this.sessionId); | ||
this.cookie = cookie; | ||
} | ||
|
||
protected createSession(sessionId?) { | ||
const now = new Date(); | ||
this.sessionId = sessionId != null ? sessionId : getUniqId(); | ||
this.cookie.addCookie({ | ||
name: FORT_GLOBAL.appSessionIdentifier, | ||
value: this.sessionId, | ||
httpOnly: true, | ||
path: "/", | ||
expires: new Date(now.setMinutes(now.getMinutes() + FORT_GLOBAL.sessionTimeOut)), | ||
maxAge: FORT_GLOBAL.sessionTimeOut * 60 | ||
}); | ||
} | ||
|
||
protected destroySession() { | ||
const cookie = this.cookie.getCookie(FORT_GLOBAL.appSessionIdentifier); | ||
cookie.httpOnly = true; | ||
cookie.path = "/"; | ||
this.cookie.removeCookie(cookie); | ||
} | ||
|
||
get(key: string) { | ||
const savedValue = this.getSessionValue_() | ||
return promiseResolve(savedValue != null ? savedValue[key] : null); | ||
return this.sessionStore.get(key); | ||
} | ||
|
||
isExist(key: string) { | ||
const savedValue = this.getSessionValue_() | ||
return promiseResolve<boolean>(savedValue == null ? false : savedValue[key] != null); | ||
return this.sessionStore.isExist(key); | ||
} | ||
|
||
getAll() { | ||
const savedValue = this.getSessionValue_(); | ||
return promiseResolve(savedValue || {}); | ||
return this.sessionStore.getAll(); | ||
} | ||
|
||
set(key: string, val: any) { | ||
const savedValue = this.getSessionValue_(); | ||
if (savedValue == null) { | ||
async set(key: string, val: any) { | ||
const savedValue = await this.sessionStore.isAnyExist(); | ||
if (savedValue === false) { | ||
this.createSession(); | ||
sessionValues.set(this.sessionId, { | ||
[key]: val | ||
}); | ||
} | ||
else { | ||
savedValue[key] = val; | ||
this.sessionStore.sessionId = this.sessionId; | ||
} | ||
return promiseResolve<void>(null); | ||
await this.sessionStore.set(key, val); | ||
} | ||
|
||
setMany(values: { [key: string]: any }) { | ||
return Promise.all( | ||
Object.keys(values).map((key) => { | ||
return this.set(key, values[key]); | ||
}) | ||
); | ||
return this.sessionStore.setMany(values); | ||
} | ||
|
||
remove(key: string) { | ||
const savedValue = this.getSessionValue_(); | ||
if (savedValue != null) { | ||
savedValue[key] = null; | ||
} | ||
return promiseResolve<void>(null); | ||
return this.sessionStore.remove(key); | ||
} | ||
|
||
clear() { | ||
async clear() { | ||
// remove session values | ||
sessionValues.delete(this.sessionId); | ||
await this.sessionStore.clear(); | ||
// expire cookie in browser | ||
this.destroySession(); | ||
return promiseResolve<void>(null); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,6 +1,5 @@ | ||
export * from './generic_guard'; | ||
export * from './generic_shield'; | ||
export * from './generic_session_provider'; | ||
export * from "./generic_wall"; | ||
export * from "./generic_controller"; | ||
export * from './generic_xml_parser'; |
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
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,11 @@ | ||
export interface ISessonStore { | ||
sessionId: string; | ||
get(key: string): Promise<any>; | ||
isAnyExist(): Promise<boolean>; | ||
isExist(key: string): Promise<boolean>; | ||
getAll(): Promise<{ [key: string]: any }>; | ||
set(key: string, val: any): Promise<void>; | ||
setMany(values: { [key: string]: any }): Promise<void[]>; | ||
remove(key: string): Promise<void>; | ||
clear(): Promise<void>; | ||
} |
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,2 +1,3 @@ | ||
export * from "./cookie_wall"; | ||
export * from "./post_data_evaluator_guard"; | ||
export * from "./post_data_evaluator_guard"; | ||
export * from "./memory_session_store"; |
Oops, something went wrong.