1import{a as s}from"./chunk-KKAAJAP7.js";import{c as d}from"./chunk-HZIJDWYK.js";import{v as h}from"./chunk-7CDSDVZV.js";import{X as u,aa as c,h as i,o as n,xa as g}from"./chunk-I4W54GEO.js";var m=class a{constructor(t,e){this.http=t;this.platformId=e;this.isBrowser=h(this.platformId),this.loadStoredAuth()}currentUserSubject=new n(null);currentUser$=this.currentUserSubject.asObservable();tokenSubject=new n(null);token$=this.tokenSubject.asObservable();isAuthenticatedSubject=new n(!1);isAuthenticated$=this.isAuthenticatedSubject.asObservable();isBrowser;get currentUser(){return this.currentUserSubject.value}loadStoredAuth(){if(!this.isBrowser)return;let t=localStorage.getItem("auth_token"),e=localStorage.getItem("auth_user");t&&e&&(this.tokenSubject.next(t),this.currentUserSubject.next(JSON.parse(e)),this.isAuthenticatedSubject.next(!0))}initializeGoogleAuth(){return i(this,null,function*(){if(this.isBrowser&&!document.querySelector('script[src*="accounts.google.com/gsi/client"]')){let t=document.createElement("script");t.src="https://accounts.google.com/gsi/client",t.async=!0,t.defer=!0,document.head.appendChild(t),yield new Promise(e=>{t.onload=e})}})}signInWithGoogle(){return i(this,null,function*(){if(this.isBrowser){console.log("\u{1F680} Starting Google Sign-In...");try{yield this.initializeGoogleAuth(),console.log("\u2705 Google Auth initialized");let t=google.accounts.oauth2.initTokenClient({client_id:s.googleClientId,scope:"email profile",callback:e=>{console.log("\u{1F4DE} Google callback received:",e),e.access_token?(console.log("\u2705 Access token received, calling handleGoogleAuth"),this.handleGoogleAuth(e)):(console.error("\u274C No access token in response:",e),e.error&&(console.error("Error:",e.error),console.error("Error description:",e.error_description)))}});console.log("\u{1F4F2} Requesting access token..."),t.requestAccessToken()}catch(t){throw console.error("\u274C Google Sign-In error:",t),t}}})}handleGoogleAuth(t){return i(this,null,function*(){console.log("\u{1F510} Starting Google Auth handler",t);try{console.log("\u{1F4E1} Fetching user info from Google...");let e=yield fetch("https://www.googleapis.com/oauth2/v2/userinfo",{headers:{Authorization:`Bearer ${t.access_token}`}});if(!e.ok)throw new Error(`Failed to fetch user info: ${e.status}`);let r=yield e.json();console.log("\u2705 Google user info received:",r);let l={idToken:t.access_token,id:r.id,name:r.name,email:r.email,photoUrl:r.picture,firstName:r.given_name,lastName:r.family_name,provider:"GOOGLE"};console.log("\u{1F4E4} Sending to our API:",l),this.http.post(`${s.apiUrl}/auth/socialLogin`,l).subscribe({next:o=>{if(console.log("\u{1F4E5} API Response:",o),o&&o.status==="success"&&o.data?.token){console.log("\u2705 Authentication successful, storing data...");let f=o.data.user||l;this.storeAuthData(o.data.token,f),console.log("\u2705 Data stored successfully"),console.log("\u{1F511} Token:",o.data.token),console.log("\u{1F464} User:",this.currentUserSubject.value),console.log("\u{1F513} Is Authenticated:",this.isAuthenticatedSubject.value)}else console.error("\u274C Authentication failed - invalid response:",o),alert("Authentication failed. Please try again.")},error:o=>{console.error("\u274C Social login API error:",o),console.error("Error details:",{status:o.status,message:o.message,error:o.error}),alert("Failed to authenticate. Please try again.")}})}catch(e){console.error("\u274C handleGoogleAuth error:",e),alert("An error occurred during authentication. Please try again.")}})}storeAuthData(t,e){if(!this.isBrowser){console.warn("\u26A0\uFE0F Not in browser environment, skipping storage");return}console.log("\u{1F4BE} Storing auth data..."),console.log("Token:",t),console.log("User data:",e);let r={id:e.id||e._id,_id:e._id||e.id,name:e.name,email:e.email,photoUrl:e.photoUrl||e.picture,firstName:e.firstName||e.given_name,lastName:e.lastName||e.family_name,provider:e.provider};console.log("User object created:",r),localStorage.setItem("auth_token",t),localStorage.setItem("auth_user",JSON.stringify(r)),console.log("\u2705 Stored in localStorage"),console.log("Verify storage - Token:",localStorage.getItem("auth_token")),console.log("Verify storage - User:",localStorage.getItem("auth_user")),this.tokenSubject.next(t),this.currentUserSubject.next(r),this.isAuthenticatedSubject.next(!0),console.log("\u2705 Updated BehaviorSubjects"),console.log("Token Subject:",this.tokenSubject.value),console.log("User Subject:",this.currentUserSubject.value),console.log("IsAuthenticated Subject:",this.isAuthenticatedSubject.value)}logout(){this.isBrowser&&(localStorage.removeItem("auth_token"),localStorage.removeItem("auth_user"),this.tokenSubject.next(null),this.currentUserSubject.next(null),this.isAuthenticatedSubject.next(!1))}getToken(){return this.tokenSubject.value}getCurrentUser(){return this.currentUserSubject.value}isAuthenticated(){return this.isAuthenticatedSubject.value}getWallet(){return i(this,null,function*(){if(!this.isBrowser||!this.isAuthenticated())return 0;try{let t=this.getToken(),e=yield this.http.get(`${s.apiUrl}/auth/me`,{headers:{Authorization:`Bearer ${t}`}}).toPromise();return e?.status==="success"&&e.data?.user?.wallet!==void 0?e.data.user.wallet:0}catch(t){return console.error("Failed to get wallet balance:",t),0}})}static \u0275fac=function(e){return new(e||a)(c(d),c(g))};static \u0275prov=u({token:a,factory:a.\u0275fac,providedIn:"root"})};export{m as a};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.