|
|
|
@ -26,7 +26,6 @@ class AuthBridge { |
|
|
|
private readyCallbacks: Array<() => void> = []; |
|
|
|
private pendingResolvers: Array<(token: string | null) => void> = []; |
|
|
|
private flutterResponseUnsubscribe?: () => void; |
|
|
|
private loginTimeoutId?: number; |
|
|
|
|
|
|
|
constructor() { |
|
|
|
this.init(); |
|
|
|
@ -70,45 +69,63 @@ class AuthBridge { |
|
|
|
) => () => void; |
|
|
|
}; |
|
|
|
|
|
|
|
if (typeof win.addFlutterResponseListener === "function") { |
|
|
|
const attach = () => { |
|
|
|
if (typeof win.addFlutterResponseListener !== "function") { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
this.flutterResponseUnsubscribe = win.addFlutterResponseListener((event) => { |
|
|
|
if (event.action === "login") { |
|
|
|
this.handleLoginResponse(event.success); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
return true; |
|
|
|
}; |
|
|
|
|
|
|
|
if (attach()) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
const fallbackInterval = window.setInterval(() => { |
|
|
|
if (typeof win.addFlutterResponseListener === "function") { |
|
|
|
if (attach()) { |
|
|
|
window.clearInterval(fallbackInterval); |
|
|
|
this.flutterResponseUnsubscribe = win.addFlutterResponseListener((event) => { |
|
|
|
if (event.action === "login") { |
|
|
|
this.handleLoginResponse(event.success); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
}, 50); |
|
|
|
} |
|
|
|
|
|
|
|
private handleLoginResponse(success: boolean) { |
|
|
|
if (success && this.syncFromStorage()) { |
|
|
|
if (success) { |
|
|
|
if (this.syncFromStorage()) { |
|
|
|
this.loginRequested = false; |
|
|
|
this.markReady(this.token); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if (!success) { |
|
|
|
window.setTimeout(() => { |
|
|
|
if (this.syncFromStorage()) { |
|
|
|
this.loginRequested = false; |
|
|
|
this.markReady(this.token); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
console.warn("⚠️ Flutter login response arrived before token"); |
|
|
|
this.loginRequested = false; |
|
|
|
console.warn("⚠️ Flutter login failed"); |
|
|
|
this.resolvePending(null); |
|
|
|
this.markReady(null); |
|
|
|
}, 50); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
this.loginRequested = false; |
|
|
|
console.warn("⚠️ Flutter login failed"); |
|
|
|
this.resolvePending(null); |
|
|
|
this.markReady(null); |
|
|
|
} |
|
|
|
|
|
|
|
private requestLogin() { |
|
|
|
if (this.loginRequested || this.token) { |
|
|
|
return; |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
this.loginRequested = true; |
|
|
|
@ -116,28 +133,21 @@ class AuthBridge { |
|
|
|
|
|
|
|
if (!sent) { |
|
|
|
this.loginRequested = false; |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if (this.loginTimeoutId) { |
|
|
|
window.clearTimeout(this.loginTimeoutId); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
this.loginTimeoutId = window.setTimeout(() => { |
|
|
|
window.setTimeout(() => { |
|
|
|
if (!this.token) { |
|
|
|
console.warn("⚠️ Flutter login timeout"); |
|
|
|
this.loginRequested = false; |
|
|
|
this.resolvePending(null); |
|
|
|
} |
|
|
|
}, LOGIN_TIMEOUT_MS); |
|
|
|
} |
|
|
|
|
|
|
|
private markReady(token: string | null) { |
|
|
|
if (this.loginTimeoutId) { |
|
|
|
window.clearTimeout(this.loginTimeoutId); |
|
|
|
this.loginTimeoutId = undefined; |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
private markReady(token: string | null) { |
|
|
|
this.isReady = true; |
|
|
|
this.resolvePending(token); |
|
|
|
this.notifyReady(); |
|
|
|
@ -162,13 +172,15 @@ class AuthBridge { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
this.setupFlutterResponseListener(); |
|
|
|
|
|
|
|
if (this.syncFromStorage()) { |
|
|
|
this.markReady(this.token); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
this.setupFlutterResponseListener(); |
|
|
|
this.requestLogin(); |
|
|
|
this.isReady = true; |
|
|
|
this.notifyReady(); |
|
|
|
} |
|
|
|
|
|
|
|
public onReady(callback: () => void) { |
|
|
|
@ -190,10 +202,12 @@ class AuthBridge { |
|
|
|
} |
|
|
|
|
|
|
|
this.setupFlutterResponseListener(); |
|
|
|
this.requestLogin(); |
|
|
|
|
|
|
|
return await new Promise<string | null>((resolve) => { |
|
|
|
this.pendingResolvers.push(resolve); |
|
|
|
if (!this.requestLogin()) { |
|
|
|
this.resolvePending(null); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
|