| 46 | }; |
| 47 | |
| 48 | class AuthService { |
| 49 | #loginCallbacks = new Set(); |
| 50 | #loginTimeout = null; |
| 51 | |
| 52 | constructor() { |
| 53 | addIntentHandler(this.onIntentReceiver.bind(this)); |
| 54 | loginEvents.addListener(() => { |
| 55 | clearTimeout(this.#loginTimeout); |
| 56 | for (const callback of this.#loginCallbacks) { |
| 57 | callback.resolve(); |
| 58 | } |
| 59 | this.#loginCallbacks.clear(); |
| 60 | }); |
| 61 | document.addEventListener("resume", () => { |
| 62 | clearTimeout(this.#loginTimeout); |
| 63 | this.#loginTimeout = setTimeout(() => { |
| 64 | for (const callback of this.#loginCallbacks) { |
| 65 | callback.reject("Login timed out"); |
| 66 | } |
| 67 | |
| 68 | this.#loginCallbacks.clear(); |
| 69 | }, 1000); |
| 70 | }); |
| 71 | } |
| 72 | |
| 73 | async onIntentReceiver(event) { |
| 74 | try { |
| 75 | if (event?.module === "user" && event?.action === "login") { |
| 76 | if (event?.value) { |
| 77 | this.#exec("saveToken", [event.value]); |
| 78 | toast("Logged in successfully"); |
| 79 | |
| 80 | setTimeout(() => { |
| 81 | loginEvents.emit(); |
| 82 | }, 500); |
| 83 | } |
| 84 | } |
| 85 | return null; |
| 86 | } catch (error) { |
| 87 | console.error("Failed to parse intent token.", error); |
| 88 | return null; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Helper to wrap cordova.exec in a Promise |
| 94 | */ |
| 95 | #exec(action, args = []) { |
| 96 | return new Promise((resolve, reject) => { |
| 97 | cordova.exec(resolve, reject, "Authenticator", action, args); |
| 98 | }); |
| 99 | } |
| 100 | |
| 101 | async logout() { |
| 102 | try { |
| 103 | const res = await fetch(`${config.API_BASE}/login`, { |
| 104 | method: "DELETE", |
| 105 | }); |
nothing calls this directly
no outgoing calls
no test coverage detected