|
1 | | -import { Reroute } from "@sveltejs/kit"; |
2 | | -import { WEBAUTHN_IFRAME_PATH } from "$lib/legacy/flows/iframeWebAuthn"; |
3 | | -import { getAddDeviceAnchor } from "$lib/utils/addDeviceLink"; |
4 | | -import { nonNullish } from "@dfinity/utils"; |
5 | | -import { DISCOVERABLE_PASSKEY_FLOW } from "$lib/state/featureFlags"; |
6 | | -import { get } from "svelte/store"; |
7 | | -import { building } from "$app/environment"; |
8 | | -import { goto, replaceState } from "$app/navigation"; |
9 | | - |
10 | | -export const reroute: Reroute = ({ url }) => { |
11 | | - if (nonNullish(getAddDeviceAnchor(url))) { |
12 | | - return "/register/device"; |
13 | | - } |
14 | | - if (url.hash === WEBAUTHN_IFRAME_PATH) { |
15 | | - return "/iframe/webauthn"; |
16 | | - } |
17 | | - if (url.pathname.startsWith("/vc-flow")) { |
18 | | - return "/vc-flow/index"; |
19 | | - } |
20 | | - if (url.hash === "#authorize") { |
21 | | - return get(DISCOVERABLE_PASSKEY_FLOW) |
22 | | - ? "/legacy-authorize-protocol" |
23 | | - : "/legacy/authorize"; |
24 | | - } |
25 | | - // Load direct authorization page instead if openid param is present |
26 | | - if (url.pathname === "/authorize" && url.searchParams.has("openid")) { |
27 | | - return `/direct-authorize-protocol`; |
28 | | - } |
29 | | - if (url.pathname === "/") { |
30 | | - return get(DISCOVERABLE_PASSKEY_FLOW) || building ? "/" : "/legacy"; |
31 | | - } |
32 | | -}; |
33 | | - |
34 | | -// SSG routes don't reroute by default anymore, so this method is used in |
35 | | -// these routes to manually add the rerouting back as early as possible, |
36 | | -// before hydration has even started (so intentionally outside onMount). |
37 | | -export const manuallyReroute = async () => { |
38 | | - if (!building) { |
39 | | - const next = await reroute({ |
40 | | - url: new URL(window.location.href), |
41 | | - fetch: window.fetch, |
42 | | - }); |
43 | | - if (nonNullish(next)) { |
44 | | - // Capture current URL |
45 | | - const currentURL = new URL(window.location.href); |
46 | | - // Cast to string since `nonNullish` doesn't exclude `void` type |
47 | | - const nextURL = new URL(next as string, window.location.origin); |
48 | | - // Copy over the current query params |
49 | | - nextURL.search = currentURL.search; |
50 | | - // Reroute to destination |
51 | | - await goto(nextURL, { replaceState: true }); |
52 | | - // After rerouting, change the URL back to what the user expects to see |
53 | | - replaceState(currentURL, {}); |
54 | | - } |
55 | | - } |
56 | | -}; |
| 1 | +export { reroute } from "$lib/utils/reroute"; |
0 commit comments