()
| 190 | } |
| 191 | |
| 192 | async ensureAuthenticated() { |
| 193 | if (new Date().getTime() < this.refreshDeadline.getTime()) { |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | const authUrl = buildFronteggUrl("/identity/resources/auth/v1/user"); |
| 198 | const response = await retry( |
| 199 | () => |
| 200 | this.request.post(authUrl, { |
| 201 | data: { |
| 202 | email: EMAIL, |
| 203 | password: PASSWORD, |
| 204 | }, |
| 205 | timeout: 10 * 1000, |
| 206 | }), |
| 207 | // Sometimes we get ECONNRESET or requests hang on these frontegg calls |
| 208 | { retries: 4 }, |
| 209 | ); |
| 210 | |
| 211 | const text = await response.text(); |
| 212 | let auth: FronteggAuthResponse; |
| 213 | try { |
| 214 | auth = JSON.parse(text); |
| 215 | } catch (e: unknown) { |
| 216 | console.error(`Invalid json from ${authUrl}:\n${text}`); |
| 217 | throw e as SyntaxError; |
| 218 | } |
| 219 | |
| 220 | this.accessToken = auth.accessToken; |
| 221 | this.refreshToken = auth.refreshToken; |
| 222 | // Use the expiresIn instead of expires, since expires is a hard to work |
| 223 | // with string. |
| 224 | this.refreshDeadline = new Date(); |
| 225 | this.refreshDeadline.setUTCSeconds( |
| 226 | this.refreshDeadline.getUTCSeconds() + auth.expiresIn / 2, |
| 227 | ); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Because frontegg is really slow, we explicitly wait to see our page layout, which |
no test coverage detected