| 63 | private readonly unsubscribeFromBeforeAuthStateChanged: (() => void) | undefined; |
| 64 | |
| 65 | constructor() { |
| 66 | if (isPlatformBrowser(inject(PLATFORM_ID))) { |
| 67 | |
| 68 | this.unsubscribeFromOnIdTokenChanged = onIdTokenChanged(this.auth, async (user) => { |
| 69 | if (user) { |
| 70 | const idToken = await user.getIdToken(); |
| 71 | cookies.set("__session", idToken); |
| 72 | } else { |
| 73 | cookies.remove("__session"); |
| 74 | } |
| 75 | }); |
| 76 | |
| 77 | let priorCookieValue: string|undefined; |
| 78 | this.unsubscribeFromBeforeAuthStateChanged = beforeAuthStateChanged(this.auth, async (user) => { |
| 79 | priorCookieValue = cookies.get("__session"); |
| 80 | const idToken = await user?.getIdToken(); |
| 81 | if (idToken) { |
| 82 | cookies.set("__session", idToken); |
| 83 | } else { |
| 84 | cookies.remove("__session"); |
| 85 | } |
| 86 | }, async () => { |
| 87 | // If another beforeAuthStateChanged rejects, revert the cookie (best-effort) |
| 88 | if (priorCookieValue) { |
| 89 | cookies.set("__session", priorCookieValue); |
| 90 | } else { |
| 91 | cookies.remove("__session"); |
| 92 | } |
| 93 | }); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | ngOnDestroy(): void { |
| 98 | this.unsubscribeFromBeforeAuthStateChanged?.(); |