feat: build the CookieAuthentication class
This commit is contained in:
parent
6343bddd18
commit
203e7c4b27
1 changed files with 38 additions and 0 deletions
38
src/lib/blog/auth/CookieAuthentication.ts
Normal file
38
src/lib/blog/auth/CookieAuthentication.ts
Normal 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: '/'})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue