1
0
Fork 0
lifeto-shop/src/session_storage.ts

37 lines
897 B
TypeScript
Raw Normal View History

2022-07-06 07:48:11 +00:00
import { Cookies, getCookie, removeCookie, setCookie} from 'typescript-cookie'
2022-07-03 10:25:12 +00:00
import { Session, TokenSession } from './lib/session'
2022-07-01 01:27:18 +00:00
export const LIFETO_COOKIE_PREFIX="LIFETO_PANEL_"
export const nameCookie = (...s:string[]):string=>{
return LIFETO_COOKIE_PREFIX+s.join("_").toUpperCase()
}
export class Storage {
2022-07-03 10:25:12 +00:00
GetSession():Session {
const {user, xsrf, csrf} = {
user: getCookie(nameCookie("user"))!,
xsrf: getCookie(nameCookie("xsrf"))!,
csrf: getCookie(nameCookie("csrf"))!
2022-07-01 01:27:18 +00:00
}
2022-07-03 10:25:12 +00:00
return new TokenSession(user, xsrf, csrf)
2022-07-01 01:27:18 +00:00
}
2022-07-03 10:25:12 +00:00
RemoveSession() {
2022-07-06 07:48:11 +00:00
removeCookie(nameCookie("user"))
removeCookie(nameCookie("xsrf"))
removeCookie(nameCookie("csrf"))
2022-07-01 01:27:18 +00:00
}
AddSession(s:Session) {
2022-07-03 10:25:12 +00:00
setCookie(nameCookie("user"),s.user)
setCookie(nameCookie("xsrf"),s.xsrf)
setCookie(nameCookie("csrf"),s.csrf)
2022-07-01 01:27:18 +00:00
}
}
export const storage = new Storage()
2022-07-06 07:48:11 +00:00