| | |
| | | */ |
| | | async init() { |
| | | if (this.isAuthenticating) { |
| | | // Wait for existing auth to complete |
| | | return new Promise(resolve => { |
| | | const checkAuth = setInterval(() => { |
| | | if (this.initialized) { |
| | | clearInterval(checkAuth); |
| | | resolve(); |
| | | } |
| | | }, 50); |
| | | }); |
| | | return this.ready(); |
| | | } |
| | | |
| | | this.isAuthenticating = true; |
| | | |
| | | try { |
| | | // Check if we have cached auth and cookie hasn't changed |
| | | const cached = this.getCachedAuth(); |
| | | if (cached) { |
| | | this.setAuthData(cached); |
| | |
| | | return; |
| | | } |
| | | |
| | | // Fetch fresh auth data |
| | | await this.fetchAuth(); |
| | | |
| | | } catch (error) { |
| | |
| | | // If auth failed and we haven't retried yet, refresh and try once more |
| | | if ((response.status === 403 || response.status === 401) && retryCount === 0) { |
| | | const result = await response.clone().json(); |
| | | if (result.code === 'rest_cookie_invalid_nonce' || result.message?.includes('Cookie check')) { |
| | | console.log('Nonce invalid, refreshing auth...'); |
| | | if (resconsole.logult.code === 'rest_cookie_invalid_nonce' || result.message?.includes('Cookie check')) { |
| | | ('Nonce invalid, refreshing auth...'); |
| | | await this.refresh(); |
| | | return attempt(1); // Retry once |
| | | } |