|
13 | 13 | </div> |
14 | 14 |
|
15 | 15 | <script type="module" th:inline="javascript"> |
16 | | - import { showErrorMessage } from "/js/errors.js"; |
| 16 | + import {showErrorMessage, checkHttpError} from "/js/errors.js"; |
17 | 17 |
|
18 | 18 | // Using an async IIFE for mobile WebView compatibility: |
19 | 19 | // top-level await is not supported in some mobile browsers/WebViews. |
20 | 20 | (async function () { |
21 | | - const frag = location.hash ? location.hash.substring(1) : ""; |
22 | | - if (!frag) { |
23 | | - showErrorMessage({ code: "UNKNOWN_ERROR", message: "Missing authentication payload" }); |
24 | | - return; |
| 21 | + const fragment = window.location.hash.slice(1); |
| 22 | + if (!fragment) { |
| 23 | + throw new Error("Missing authentication payload"); |
25 | 24 | } |
26 | 25 |
|
27 | 26 | let payload; |
28 | 27 | try { |
29 | | - payload = JSON.parse(atob(frag)); |
| 28 | + payload = JSON.parse(atob(fragment)); |
30 | 29 | } catch (e) { |
31 | | - console.error("Failed to parse payload", e); |
32 | | - showErrorMessage({ code: "UNKNOWN_ERROR", message: "Failed to parse authentication payload" }); |
33 | | - return; |
| 30 | + console.error(e) |
| 31 | + throw new Error("Failed to parse the authentication response"); |
34 | 32 | } |
35 | 33 |
|
36 | 34 | if (payload.error) { |
37 | | - showErrorMessage({ |
38 | | - code: payload.code ?? "UNKNOWN_ERROR", |
39 | | - message: payload.message ?? "Authentication failed" |
40 | | - }); |
41 | | - return; |
| 35 | + const error = new Error(payload.message ?? "Authentication failed"); |
| 36 | + error.code = payload.code; |
| 37 | + throw error; |
42 | 38 | } |
43 | 39 |
|
44 | | - const authToken = payload["auth-token"]; |
45 | | - |
46 | | - try { |
47 | | - const response = await fetch(/*[[${loginProcessingPath}]]*/, { |
48 | | - method: "POST", |
49 | | - headers: { |
50 | | - "Content-Type": "application/json", |
51 | | - "X-CSRF-TOKEN": /*[[${csrfToken}]]*/ |
52 | | - }, |
53 | | - body: JSON.stringify(authToken), |
54 | | - credentials: "include" |
55 | | - }); |
56 | | - |
57 | | - if (!response.ok) { |
58 | | - throw new Error("HTTP " + response.status); |
59 | | - } |
60 | | - |
61 | | - window.location.replace("/welcome"); |
62 | | - } catch (error) { |
63 | | - console.error(error); |
64 | | - showErrorMessage(error); |
65 | | - } |
66 | | - })(); |
| 40 | + const authToken = payload["auth_token"]; |
| 41 | + const response = await fetch(/*[[${loginProcessingPath}]]*/, { |
| 42 | + method: "POST", |
| 43 | + headers: { |
| 44 | + "Content-Type": "application/json", |
| 45 | + "X-CSRF-TOKEN": /*[[${csrfToken}]]*/ |
| 46 | + }, |
| 47 | + body: JSON.stringify(authToken), |
| 48 | + credentials: "include" |
| 49 | + }); |
| 50 | + await checkHttpError(response); |
| 51 | + |
| 52 | + window.location.replace("/welcome"); |
| 53 | + })().catch((error) => { |
| 54 | + console.error(error); |
| 55 | + showErrorMessage(error); |
| 56 | + }); |
67 | 57 | </script> |
68 | 58 | </body> |
69 | 59 | </html> |
0 commit comments