forked from Mirrors/gomuks
web/main: move authentication to happen after react init
This commit is contained in:
parent
e2f0ba61ac
commit
a70c16f0f3
3 changed files with 42 additions and 20 deletions
|
@ -29,16 +29,11 @@ function App() {
|
|||
window.client = client
|
||||
const connState = useEventAsState(client.rpc.connect)
|
||||
const clientState = useEventAsState(client.state)
|
||||
useEffect(() => {
|
||||
Notification.requestPermission()
|
||||
.then(permission => console.log("Notification permission:", permission))
|
||||
client.rpc.start()
|
||||
return () => client.rpc.stop()
|
||||
}, [client])
|
||||
useEffect(() => client.start(), [client])
|
||||
|
||||
if (connState?.error) {
|
||||
return <div>
|
||||
error {`${connState.error}`} :(
|
||||
{`${connState.error} \u{1F63F}`}
|
||||
</div>
|
||||
} else if (!connState?.connected || !clientState) {
|
||||
const msg = connState?.connected ?
|
||||
|
|
|
@ -38,6 +38,41 @@ export default class Client {
|
|||
queueMicrotask(() => this.#handleEmoteRoomsChange()))
|
||||
}
|
||||
|
||||
async #reallyStart(signal: AbortSignal) {
|
||||
try {
|
||||
const resp = await fetch("_gomuks/auth", {
|
||||
method: "POST",
|
||||
signal,
|
||||
})
|
||||
if (!resp.ok) {
|
||||
this.rpc.connect.emit({
|
||||
connected: false,
|
||||
error: new Error(`Authentication failed: ${resp.statusText}`),
|
||||
})
|
||||
return
|
||||
}
|
||||
} catch (err) {
|
||||
const error = err instanceof Error ? err : new Error(`${err}`)
|
||||
this.rpc.connect.emit({ connected: false, error })
|
||||
}
|
||||
if (signal.aborted) {
|
||||
return
|
||||
}
|
||||
console.log("Successfully authenticated, connecting to websocket")
|
||||
this.rpc.start()
|
||||
Notification.requestPermission()
|
||||
.then(permission => console.log("Notification permission:", permission))
|
||||
}
|
||||
|
||||
start(): () => void {
|
||||
const abort = new AbortController()
|
||||
this.#reallyStart(abort.signal)
|
||||
return () => {
|
||||
abort.abort()
|
||||
this.rpc.stop()
|
||||
}
|
||||
}
|
||||
|
||||
get userID(): UserID {
|
||||
return this.state.current?.is_logged_in ? this.state.current.user_id : ""
|
||||
}
|
||||
|
|
|
@ -18,16 +18,8 @@ import { createRoot } from "react-dom/client"
|
|||
import App from "./App.tsx"
|
||||
import "./index.css"
|
||||
|
||||
fetch("_gomuks/auth", { method: "POST" }).then(resp => {
|
||||
if (resp.ok) {
|
||||
createRoot(document.getElementById("root")!).render(
|
||||
<StrictMode>
|
||||
<App/>
|
||||
</StrictMode>,
|
||||
)
|
||||
} else {
|
||||
window.alert("Authentication failed: " + resp.statusText)
|
||||
}
|
||||
}, err => {
|
||||
window.alert("Authentication failed: " + err)
|
||||
})
|
||||
|
|
Loading…
Add table
Reference in a new issue