feat: build the CookieAuthentication class

This commit is contained in:
wilson 2026-03-09 22:16:56 +00:00
parent 6343bddd18
commit 203e7c4b27

View file

@ -0,0 +1,38 @@
import type { Cookies } from "@sveltejs/kit";
export class CookieAuthentication {
private readonly cookieValue: string;
private readonly cookieValueArray: string[];
public static cookieName = 'auth'
public static adminAuthRole = 'admin'
constructor(
private readonly cookies: Cookies,
){
this.cookieValue = this.cookies.get(CookieAuthentication.cookieName) ?? ''
this.cookieValueArray = this.cookieValue.split(',')
}
public get isAuthdAsAdmin(): boolean {
let isAuthdAsAdmin = false;
if (this.cookieValueArray.includes(CookieAuthentication.adminAuthRole)) {
isAuthdAsAdmin = true
}
return isAuthdAsAdmin
}
public setAdminAuthentication(isAuthd: boolean) {
let value = this.cookieValue
if (isAuthd) {
value = Array.from(new Set([...this.cookieValueArray, CookieAuthentication.adminAuthRole])).join(',')
} else {
value = this.cookieValueArray.filter((i) => i !== CookieAuthentication.adminAuthRole).join(',')
}
this.cookies.set(CookieAuthentication.cookieName, value, { path: '/'})
}
}