Login still a bit buggy but working...

This commit is contained in:
Leo Vasanko
2023-11-03 21:19:26 +00:00
parent 119aba2b3c
commit b759d8324c
8 changed files with 66 additions and 64 deletions

View File

@@ -1,9 +1,6 @@
/* Base domain for all request */
export const baseURL = import.meta.env.VITE_URL_DOCUMENT
class ClientClass {
async post(url: string, data?: Record<string, any>): Promise<any> {
const res = await fetch(`${baseURL}/`, {
const res = await fetch(url, {
method: 'POST',
headers: {
accept: 'application/json',
@@ -11,7 +8,12 @@ class ClientClass {
},
body: data !== undefined ? JSON.stringify(data) : undefined
})
const msg = await res.json()
let msg
try {
msg = await res.json()
} catch (e) {
throw new SimpleError(res.status, `HTTP ${res.status} ${res.statusText}`)
}
if ('error' in msg) throw new SimpleError(msg.error.code, msg.error.message)
return msg
}

View File

@@ -1,5 +1,6 @@
import type { DocumentStore } from '@/stores/documents'
import { useDocumentStore } from '@/stores/documents'
import createWebSocket from './WS'
export type FUID = string
@@ -63,6 +64,19 @@ export class DocumentHandler {
handleWebSocketMessage(event: MessageEvent) {
const msg = JSON.parse(event.data)
if ("error" in msg) {
if (msg.error.code === 401) {
this.store.user.isLoggedIn = false
this.store.user.isOpenLoginModal = true
} else {
this.store.error = msg.error.message
}
// The server closes the websocket after errors, so we need to reopen it
setTimeout(
() => { this.store.wsWatch = createWebSocket(url_document_watch_ws, this.handleWebSocketMessage)},
1000
)
}
switch (true) {
case !!msg.root:
this.handleRootMessage(msg)