import{ba as c,c as h,d as s,e as u,f as a,i as r,j as n,k as l,l as p,ta as f}from"./chunk-KKCCFLOX.js";var g=class i{constructor(t){this.http=t}base=p(f);key="zenbrox_auth";user$=new h(this.loadUser());userChanges$=this.user$.asObservable();get accessToken(){return this.load().accessToken??null}get refreshToken(){return this.load().refreshToken??null}get currentUser(){return this.user$.value}get isPro(){return this.user$.value?.isPro===!0}register(t,e,o){return this.http.post(`${this.base}/auth/register`,{email:t,password:e,displayName:o}).pipe(r(A=>this.saveAuth(A)))}login(t,e){return this.http.post(`${this.base}/auth/login`,{email:t,password:e}).pipe(r(o=>this.saveAuth(o)))}logout(){let t=this.refreshToken;return this.clearAuth(),t?this.http.post(`${this.base}/auth/logout`,{refreshToken:t}).pipe(a(()=>s({ok:!0}))):s({ok:!0})}refresh(){let t=this.refreshToken;return t?this.http.post(`${this.base}/auth/refresh`,{refreshToken:t}).pipe(r(e=>this.saveAuth(e))):u(()=>new Error("No refresh token"))}forceLogout(){this.clearAuth()}verifySession(){!this.accessToken&&!this.refreshToken||this.http.get(`${this.base}/auth/me`).pipe(r(t=>{t?.user&&this.user$.next(t.user)}),a(()=>s(null))).subscribe()}saveAuth(t){localStorage.setItem(this.key,JSON.stringify(t)),this.user$.next(t.user)}clearAuth(){localStorage.removeItem(this.key),this.user$.next(null)}load(){try{return JSON.parse(localStorage.getItem(this.key)||"{}")}catch{return{}}}loadUser(){return this.load().user??null}static \u0275fac=function(e){return new(e||i)(l(c))};static \u0275prov=n({token:i,factory:i.\u0275fac,providedIn:"root"})};export{g as a};
