Handle failed space auth

This commit is contained in:
Jon Staab
2024-11-18 20:31:17 -08:00
parent cbc4c524c4
commit ed738f64c8
4 changed files with 97 additions and 11 deletions

View File

@@ -330,10 +330,15 @@ export const checkRelayAuth = async (url: string, timeout = 3000) => {
}
export const attemptRelayAccess = async (url: string, claim = "") => {
const checks = [checkRelayProfile, checkRelayConnection, checkRelayAccess, checkRelayAuth]
const checks = [
() => checkRelayProfile(url),
() => checkRelayConnection(url),
() => checkRelayAccess(url, claim),
() => checkRelayAuth(url),
]
for (const check of checks) {
const error = await check(url)
const error = await check()
if (error) {
return error

View File

@@ -0,0 +1,73 @@
<script lang="ts">
import Spinner from "@lib/components/Spinner.svelte"
import Button from "@lib/components/Button.svelte"
import Field from "@lib/components/Field.svelte"
import Icon from "@lib/components/Icon.svelte"
import ModalHeader from "@lib/components/ModalHeader.svelte"
import ModalFooter from "@lib/components/ModalFooter.svelte"
import {pushToast} from "@app/toast"
import {clearModals} from "@app/modal"
import {attemptRelayAccess} from "@app/commands"
export let url
export let error
const back = () => history.back()
const joinRelay = async (claim: string) => {
const error = await attemptRelayAccess(url, claim)
if (error) {
return pushToast({theme: "error", message: error})
}
pushToast({
message: "You have successfully joined the space!",
})
clearModals()
}
const join = async () => {
loading = true
try {
await joinRelay(claim)
} finally {
loading = false
}
}
let claim = ""
let loading = false
</script>
<form class="column gap-4" on:submit|preventDefault={join}>
<ModalHeader>
<div slot="title">Access Error</div>
<div slot="info">We couldn't connect you to this space.</div>
</ModalHeader>
<p>We received an error from the relay indicating you don't have access to this space.</p>
<p class="border-l border-solid border-error pl-4 text-error">
{error}
</p>
<p>If you have one, you can try entering an invite code below to request access.</p>
<Field>
<p slot="label">Invite code</p>
<label class="input input-bordered flex w-full items-center gap-2" slot="input">
<Icon icon="link-round" />
<input bind:value={claim} class="grow" type="text" />
</label>
<p slot="info">Enter an invite code provided to you by the admin of the relay.</p>
</Field>
<ModalFooter>
<Button class="btn btn-link" on:click={back}>
<Icon icon="alt-arrow-left" />
Go back
</Button>
<Button type="submit" class="btn btn-primary" disabled={!claim || loading}>
<Spinner {loading}>Request Access</Spinner>
<Icon icon="alt-arrow-right" />
</Button>
</ModalFooter>
</form>

View File

@@ -62,7 +62,7 @@
let url = ""
let loading = false
$: linkIsValid = Boolean(tryCatch(() => isRelayUrl(normalizeRelayUrl(url))))
$: linkIsValid = Boolean(tryCatch(() => isRelayUrl(normalizeRelayUrl(url.split("|")[0]))))
</script>
<form class="column gap-4" on:submit|preventDefault={join}>

View File

@@ -1,15 +1,17 @@
<script lang="ts">
import {onMount} from "svelte"
import {page} from "$app/stores"
import {ifLet, now} from "@welshman/lib"
import {now} from "@welshman/lib"
import {subscribe} from "@welshman/app"
import {DELETE, REACTION} from "@welshman/util"
import Page from "@lib/components/Page.svelte"
import SecondaryNav from "@lib/components/SecondaryNav.svelte"
import MenuSpace from "@app/components/MenuSpace.svelte"
import SpaceAuthError from "@app/components/SpaceAuthError.svelte"
import {pushToast} from "@app/toast"
import {pushModal} from "@app/modal"
import {setChecked} from "@app/notifications"
import {checkRelayConnection, checkRelayAuth} from "@app/commands"
import {checkRelayConnection, checkRelayAuth, checkRelayAccess} from "@app/commands"
import {decodeRelay, MEMBERSHIPS} from "@app/state"
import {deriveNotification, SPACE_FILTERS} from "@app/notifications"
@@ -18,13 +20,19 @@
const notification = deriveNotification($page.url.pathname, SPACE_FILTERS, url)
const checkConnection = async () => {
ifLet(await checkRelayConnection(url), error => {
pushToast({theme: "error", message: error})
})
const connectionError = await checkRelayConnection(url)
ifLet(await checkRelayAuth(url, 30_000), error => {
pushToast({theme: "error", message: error})
})
if (connectionError) {
return pushToast({theme: "error", message: connectionError})
}
const [authError, accessError] = await Promise.all([checkRelayAuth(url), checkRelayAccess(url)])
const error = authError || accessError
if (error) {
pushModal(SpaceAuthError, {url, error})
}
}
// We have to watch this one, since on mobile the badge wil be visible when active