diff --git a/index.html b/index.html index 99f203f..6c5d402 100644 --- a/index.html +++ b/index.html @@ -3,6 +3,10 @@ + Tauri + Vue + Typescript App diff --git a/src/App.vue b/src/App.vue index 2d4ce92..97e53e7 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,18 +1,22 @@ @@ -30,27 +34,86 @@ function logout() { diff --git a/src/api/client.ts b/src/api/client.ts index d0369cd..51776dc 100644 --- a/src/api/client.ts +++ b/src/api/client.ts @@ -1,5 +1,6 @@ -import { initAuth } from '../stores/auth.ts' +import { initAuth, logout } from '../stores/auth.ts' import { API } from '../main.ts' +import router from '../router' export async function apiFetch( path: string, @@ -16,6 +17,12 @@ export async function apiFetch( }, }) + if (res.status === 401) { + await logout() + router.push('/login') + throw new Error("Session expired") + } + if (!res.ok) { const text = await res.text() throw new Error(text || res.statusText) @@ -23,4 +30,3 @@ export async function apiFetch( return res.json() } - diff --git a/src/components/ChatWindow.vue b/src/components/ChatWindow.vue new file mode 100644 index 0000000..baaca6c --- /dev/null +++ b/src/components/ChatWindow.vue @@ -0,0 +1,139 @@ + + + + + diff --git a/src/components/MessageInput.vue b/src/components/MessageInput.vue index 6712184..f6631a6 100644 --- a/src/components/MessageInput.vue +++ b/src/components/MessageInput.vue @@ -1,11 +1,19 @@ - diff --git a/src/components/RoomList.vue b/src/components/RoomList.vue new file mode 100644 index 0000000..865ea1a --- /dev/null +++ b/src/components/RoomList.vue @@ -0,0 +1,116 @@ + + + + + diff --git a/src/main.ts b/src/main.ts index 1b900d3..66dc075 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,12 +1,19 @@ import { createApp } from 'vue' import router from './router' import App from './App.vue' +import { validateToken } from './stores/auth.ts' import './base.css' -createApp(App) - .use(router) - .mount('#app') +async function init() { + await validateToken() + + const app = createApp(App) + app.use(router) + app.mount('#app') +} + +init() export const API = 'http://127.0.0.1:8080' export const API_WS = 'ws://127.0.0.1:8080/ws' diff --git a/src/pages/ChatPage.vue b/src/pages/ChatPage.vue index b1dc5ae..e2f509b 100644 --- a/src/pages/ChatPage.vue +++ b/src/pages/ChatPage.vue @@ -1,138 +1,50 @@ diff --git a/src/pages/RoomsPage.vue b/src/pages/RoomsPage.vue deleted file mode 100644 index d23fe7e..0000000 --- a/src/pages/RoomsPage.vue +++ /dev/null @@ -1,90 +0,0 @@ - - - - - diff --git a/src/router/index.ts b/src/router/index.ts index 7de8c66..00b2333 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -1,33 +1,30 @@ import { createRouter, createWebHistory } from 'vue-router' -import { initAuth, validateToken } from '../stores/auth.ts' +import { initAuth } from '../stores/auth.ts' import LoginPage from '../pages/LoginPage.vue' -import RoomsPage from '../pages/RoomsPage.vue' import ChatPage from '../pages/ChatPage.vue' import FriendListPage from '../pages/FriendListPage.vue' const router = createRouter({ history: createWebHistory(), routes: [ + { path: '/', redirect: '/rooms/none' }, { path: '/login', component: LoginPage }, - { path: '/', component: RoomsPage }, { path: '/rooms/:uuid', component: ChatPage, props: true }, { path: '/friendlist', component: FriendListPage } ], }) router.beforeEach(async (to) => { + if (to.path === '/login') return true + const auth = await initAuth() - if (!auth.isAuthenticated && to.path !== '/login') { + if (!auth.isAuthenticated) { return '/login' } - if (auth.isAuthenticated) { - const valid = await validateToken() - if (!valid) return '/login' - } + return true }) export default router - diff --git a/src/stores/auth.ts b/src/stores/auth.ts index 51cd583..1f3065d 100644 --- a/src/stores/auth.ts +++ b/src/stores/auth.ts @@ -45,15 +45,14 @@ export async function validateToken(): Promise { if (!auth.token) return false try { - await apiFetch('/validate-token', { - method: 'GET', - }) + await apiFetch('/validate-token') + return true + } catch (e: any) { + // Only logout if token is bad + if (e.message.includes('401')) { + await logout() + return false + } return true - } catch (e) { - console.log(e); - - await logout() - return false } } -