Files
rsu-new-site/frontend/chunk-SMVD5LRJ.js

2 lines
2.2 KiB
JavaScript

import{e as p}from"./chunk-ZEULGWAI.js";import{a as s}from"./chunk-MBEC6LK4.js";import{$b as r,D as u,H as l,Hc as c,L as i,ca as g,i as n,t as a}from"./chunk-TVKQFRWX.js";var m=class o{constructor(t,e){this.http=t;this.router=e}API_URL=s.apiUrl;TOKEN_KEY="rsu_token";USER_KEY="rsu_user";currentUserSignal=g(this.getStoredUser());currentUser=r(()=>this.currentUserSignal());isAuthenticated=r(()=>!!this.currentUserSignal()&&!!this.getToken()&&!this.isTokenExpired(this.getToken()));mustChangePassword=r(()=>this.currentUserSignal()?.mustChangePassword===!0);isAdmin=r(()=>this.currentUserSignal()?.roles?.some(e=>e==="ROLE_ADMIN"||e==="ROLE_SUPER_ADMIN")??!1);isSuperAdmin=r(()=>this.currentUserSignal()?.roles?.includes("ROLE_SUPER_ADMIN")??!1);isModerator=r(()=>this.currentUserSignal()?.roles?.some(e=>e==="ROLE_MODERATOR"||e==="ROLE_ADMIN"||e==="ROLE_SUPER_ADMIN")??!1);login(t){return this.http.post(`${s.authUrl}/login`,t).pipe(u(e=>{e.success&&e.data&&(this.storeToken(e.data.token),this.storeUser(e.data),this.currentUserSignal.set(e.data))}),a(e=>(console.error("Login error:",e),n(()=>e))))}register(t){return this.http.post(`${s.authUrl}/register`,t)}logout(){this.clearSession(),this.router.navigate(["/"])}clearSession(){localStorage.removeItem(this.TOKEN_KEY),localStorage.removeItem(this.USER_KEY),this.currentUserSignal.set(null)}getToken(){return localStorage.getItem(this.TOKEN_KEY)}storeToken(t){localStorage.setItem(this.TOKEN_KEY,t)}storeUser(t){localStorage.setItem(this.USER_KEY,JSON.stringify(t))}getStoredUser(){let t=this.getToken();if(!t||this.isTokenExpired(t))return localStorage.removeItem(this.TOKEN_KEY),localStorage.removeItem(this.USER_KEY),null;let e=localStorage.getItem(this.USER_KEY);if(e)try{return JSON.parse(e)}catch{return null}return null}isTokenExpired(t){if(!t)return!0;try{let e=JSON.parse(atob(t.split(".")[1].replace(/-/g,"+").replace(/_/g,"/")));return typeof e.exp!="number"||e.exp*1e3<=Date.now()}catch{return!0}}hasRole(t){return this.currentUserSignal()?.roles?.includes(t)??!1}hasAnyRole(t){let e=this.currentUserSignal();return t.some(d=>e?.roles?.includes(d))??!1}static \u0275fac=function(e){return new(e||o)(i(c),i(p))};static \u0275prov=l({token:o,factory:o.\u0275fac,providedIn:"root"})};export{m as a};