add username in use stuff
This commit is contained in:
parent
1bf37fb564
commit
71c635cd52
1 changed files with 41 additions and 0 deletions
|
|
@ -39,6 +39,47 @@ import "../styles/page.css"
|
|||
<a href="../code/">Code of Conduct</a>.
|
||||
</label>
|
||||
|
||||
<span id="error" class="error"></span>
|
||||
<button type="submit"><span>Submit</span></button>
|
||||
</form>
|
||||
</StarlightPage>
|
||||
|
||||
<script>
|
||||
const form = document.querySelector("form")!
|
||||
const username = form.querySelector<HTMLInputElement>('[name="username"]')!
|
||||
|
||||
const error = document.querySelector<HTMLSpanElement>("#error")!
|
||||
error.textContent = new URLSearchParams(location.search).get("error") ?? ""
|
||||
if (!error.textContent) error.style.display = "none"
|
||||
|
||||
const check = async () => {
|
||||
username.setCustomValidity("")
|
||||
if (!username.checkValidity()) return
|
||||
|
||||
const name = username.value.toLowerCase()
|
||||
try {
|
||||
const res = await fetch(
|
||||
`https://matrix.federated.nexus/_matrix/client/v3/register/available?username=${encodeURIComponent(name)}`,
|
||||
)
|
||||
const { errcode } = await res.json()
|
||||
if (
|
||||
errcode === "M_USER_IN_USE" &&
|
||||
username.value.toLowerCase() === name
|
||||
) {
|
||||
username.setCustomValidity("That username is already taken.")
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
|
||||
username.addEventListener("input", () => username.setCustomValidity(""))
|
||||
username.addEventListener("change", async () => {
|
||||
await check()
|
||||
username.reportValidity()
|
||||
})
|
||||
|
||||
form.addEventListener("submit", async (e) => {
|
||||
e.preventDefault()
|
||||
await check()
|
||||
if (form.reportValidity()) form.submit()
|
||||
})
|
||||
</script>
|
||||
|
|
|
|||
Loading…
Reference in a new issue