diff --git a/app/app.vue b/app/app.vue new file mode 100644 index 0000000..2615485 --- /dev/null +++ b/app/app.vue @@ -0,0 +1,36 @@ + + + + + + {{ t('app.title') }} + + · {{ t('app.worlds') }} + · {{ user.name }} + · {{ t('app.logout') }} + + · {{ connection }} + + + + + [{{ n.type }}] {{ n.text }} + + + + + + + + + diff --git a/app/composables/useAuth.ts b/app/composables/useAuth.ts new file mode 100644 index 0000000..471a25e --- /dev/null +++ b/app/composables/useAuth.ts @@ -0,0 +1,44 @@ +import type { AuthResult, AuthUser } from '~/types/game' + +const USER_KEY = 'vp.user' + +function readStoredUser(): AuthUser | null { + try { + const raw = localStorage.getItem(USER_KEY) + return raw ? JSON.parse(raw) : null + } catch { + return null + } +} + +function storeUser(user: AuthUser | null) { + try { + if (user) localStorage.setItem(USER_KEY, JSON.stringify(user)) + else localStorage.removeItem(USER_KEY) + } catch { + // the token still works; only the name in the header is lost on reload + } +} + +export function useAuth() { + const user = useState('auth.user', () => (gameAuth.token() ? readStoredUser() : null)) + + function signedIn(result: AuthResult) { + user.value = result.user + storeUser(result.user) + } + + return { + user, + isLoggedIn: () => gameAuth.token() !== null, + login: async (email: string, password: string) => signedIn(await gameAuth.login(email, password)), + register: async (name: string, email: string, password: string) => + signedIn(await gameAuth.register(name, email, password)), + logout() { + gameAuth.logout() + user.value = null + storeUser(null) + return navigateTo('/login') + }, + } +} diff --git a/app/middleware/auth.global.ts b/app/middleware/auth.global.ts new file mode 100644 index 0000000..03a547b --- /dev/null +++ b/app/middleware/auth.global.ts @@ -0,0 +1,8 @@ +const PUBLIC_PATHS = ['/login', '/register'] + +export default defineNuxtRouteMiddleware((to) => { + const loggedIn = gameAuth.token() !== null + const isPublic = PUBLIC_PATHS.includes(to.path) + if (!loggedIn && !isPublic) return navigateTo('/login') + if (loggedIn && isPublic) return navigateTo('/worlds') +}) diff --git a/app/pages/index.vue b/app/pages/index.vue new file mode 100644 index 0000000..39baa9b --- /dev/null +++ b/app/pages/index.vue @@ -0,0 +1,7 @@ + + + + + diff --git a/app/pages/login.vue b/app/pages/login.vue new file mode 100644 index 0000000..b534cd1 --- /dev/null +++ b/app/pages/login.vue @@ -0,0 +1,41 @@ + + + + + {{ $t('auth.login.title') }} + {{ $t('auth.mock_hint') }} + + + {{ $t('auth.email') }} + + + {{ $t('auth.password') }} + + {{ error }} + {{ $t('auth.login.submit') }} + + {{ $t('auth.login.to_register') }} + + diff --git a/app/pages/register.vue b/app/pages/register.vue new file mode 100644 index 0000000..c4fc8b9 --- /dev/null +++ b/app/pages/register.vue @@ -0,0 +1,45 @@ + + + + + {{ $t('auth.register.title') }} + {{ $t('auth.mock_hint') }} + + + {{ $t('auth.name') }} + + + {{ $t('auth.email') }} + + + {{ $t('auth.password') }} + + {{ error }} + {{ $t('auth.register.submit') }} + + {{ $t('auth.register.to_login') }} + +
{{ $t('auth.mock_hint') }}
+ {{ $t('auth.email') }} +
+ {{ $t('auth.password') }} +
{{ error }}
{{ $t('auth.login.to_register') }}
+ {{ $t('auth.name') }} +
{{ $t('auth.register.to_login') }}