Skip to main content
Go to Latest
File
import { Cookie, deleteCookie, getCookies, setCookie,} from "https://deno.land/std@0.173.0/http/cookie.ts";interface Active_role { updated_at: Date, user_id: number, role_id: number}interface Role { name: string, enable: number, id: number, created_at: Date, updated_at: Date}interface User { name: string, email: string, phone: string | null, google_id: string | null, facebook_id: string | null, password: string | null, enable: number, id: number, created_at: Date, updated_at: Date}import { Login } from "./type.ts";interface LoginSession { session_id: string; name: string; expire: Date; email: string; id: number; roles: string[]; ip: string | null; agent: string | null;}
let sessions: LoginSession[] = [];const sessionCookie = ( session_id: string, date: Date, domain: string,): Cookie => { return { name: "PHPSESSID", value: session_id, httpOnly: true, path: "/", domain: "." + domain, sameSite: "Strict", secure: true, expires: date, };};export class Session { Session!: string; expirein = 30; time: Date; ActiveLoginSession: LoginSession | undefined; domain: string; Login!: Login; constructor(public req: Request, public cookie?: Record<string, string>) { this.domain = req.headers.get("host") || ""; this.time = new Date(); this.expire(); if (this.cookie) { this.Session = this.cookie.PHPSESSID; this.ActiveLoginSession = sessions.find((i) => i.session_id == this.Session ); } } expireSet(miniutes: number) { this.expirein = miniutes; return this; } startnew(User: User, Role: Role[] = [], Active_Role: Active_role[] = []) { if (this.Session) { this.removeSession(); } this.newSession(User, Role, Active_Role); return this; } newSession(User: User, Role: Role[], Active_Role: Active_role[]) { this.Session = crypto.randomUUID(); return this.addSession({ session_id: this.Session, name: User.name, email: User.email, expire: this.time, id: User.id, roles: this.SessionRoles(Role, Active_Role), ip: this.req.headers.get("x-forwarded-for"), agent: this.req.headers.get("user-agent"), }); }
validSession(session_id: string) { return sessions.find((i) => { i.session_id == session_id && i.ip == this.req.headers.get("x-forwarded-for") && i.agent == this.req.headers.get("user-agent"); }); } expire() { this.time.setTime(this.time.getTime() + this.expirein * 60 * 1000); } SessionRoles(Role: Role[], Active_Role: Active_role[]) { return Role.filter((i) => Active_Role.filter((a) => a.role_id == i.id)).map( (i) => i.name, ); } getSessionRoles(session_id: string) { return sessions.find((i) => i.session_id == session_id) || false; } addSession(LoginSession: LoginSession) { sessions = [...sessions, LoginSession]; } removeSession() { sessions = sessions.filter((i) => i.session_id != this.Session); }
removeCookie() { this.removeSession(); const headers = new Headers(); deleteCookie(headers, "PHPSESSID"); return { "set-cookie": headers.get("set-cookie"), }; }
returnCookie(){ const headers = new Headers(); setCookie(headers, sessionCookie(this.Session, this.time, this.domain)); const cookie = headers.get("set-cookie"); if (cookie != null) { return { "set-cookie":cookie, }; } } reactiveSession() { sessions = sessions.filter((i) => i.session_id != this.Session); if (this.ActiveLoginSession) { this.Session = crypto.randomUUID(); sessions = [...sessions, { ...this.ActiveLoginSession, ...{ session_id: this.Session }, }]; } return this; } getSession() {} setLogin() { const p = sessions.find((i) => i.session_id == this.Session); if (p) { this.Login = { name: p?.name, email: p?.email, id: p?.id, roles: p?.roles, }; } return this; }}