forked from a/lifeto-shop
64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
|
import { getCookie, setCookie} from 'typescript-cookie'
|
||
|
import { Session } from './lib/session'
|
||
|
|
||
|
|
||
|
|
||
|
export const LIFETO_COOKIE_PREFIX="LIFETO_PANEL_"
|
||
|
|
||
|
export const nameCookie = (...s:string[]):string=>{
|
||
|
return LIFETO_COOKIE_PREFIX+s.join("_").toUpperCase()
|
||
|
}
|
||
|
|
||
|
export class Storage {
|
||
|
GetSessions():{[key:string]:Session} {
|
||
|
const all_accounts = getCookie(nameCookie("all_accounts"))
|
||
|
const accounts = all_accounts?.split(",")
|
||
|
let out:{[key:string]:Session} = {};
|
||
|
for (const account in accounts) {
|
||
|
let tmp = {
|
||
|
user: account,
|
||
|
xsrf: getCookie(nameCookie("xsrf",account))!,
|
||
|
lto: getCookie(nameCookie("lto",account))!,
|
||
|
csrf: getCookie(nameCookie("csrf",account))!
|
||
|
}
|
||
|
out[account] = tmp
|
||
|
}
|
||
|
return out
|
||
|
}
|
||
|
RemoveSessions(...s:Session[]) {
|
||
|
for(const v of s) {
|
||
|
this.RemoveSession(v)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
RemoveSession(s:Session) {
|
||
|
const all_accounts = getCookie(nameCookie("all_accounts"))
|
||
|
let accounts = all_accounts?.split(",")
|
||
|
accounts = accounts ? accounts : []
|
||
|
accounts = [...new Set(accounts)]
|
||
|
accounts = accounts.filter(x=>x!=s.user)
|
||
|
|
||
|
setCookie(nameCookie("all_accounts"),accounts.join(","))
|
||
|
setCookie(nameCookie("lto",s.user), "")
|
||
|
setCookie(nameCookie("xsrf",s.user),"")
|
||
|
}
|
||
|
|
||
|
AddSessions(...s:Session[]) {
|
||
|
for(const v of s) {
|
||
|
this.AddSession(v)
|
||
|
}
|
||
|
}
|
||
|
AddSession(s:Session) {
|
||
|
const all_accounts = getCookie(nameCookie("all_accounts"))
|
||
|
let accounts = all_accounts?.split(",")
|
||
|
accounts = accounts ? accounts : []
|
||
|
accounts.push(s.user)
|
||
|
accounts = [...new Set(accounts)]
|
||
|
setCookie(nameCookie("lto",s.user), s.lto)
|
||
|
setCookie(nameCookie("xsrf",s.user),s.xsrf)
|
||
|
setCookie(nameCookie("csrf",s.user),s.csrf)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export const storage = new Storage()
|