feat: email + password login and register
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
9e66209ffd
commit
86eb2f6a8a
|
|
@ -0,0 +1,36 @@
|
||||||
|
<script setup lang="ts">
|
||||||
|
const { t } = useI18n()
|
||||||
|
const { user, logout } = useAuth()
|
||||||
|
const notices = useNotices()
|
||||||
|
|
||||||
|
const connection = computed(() =>
|
||||||
|
isMockServer ? t('app.mock') : ws.is_opened.value ? t('app.connected') : t('app.offline'),
|
||||||
|
)
|
||||||
|
|
||||||
|
useHead({ title: () => t('app.title') })
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<header>
|
||||||
|
<strong>{{ t('app.title') }}</strong>
|
||||||
|
<template v-if="user">
|
||||||
|
· <NuxtLink to="/worlds">{{ t('app.worlds') }}</NuxtLink>
|
||||||
|
· {{ user.name }}
|
||||||
|
· <button type="button" @click="logout()">{{ t('app.logout') }}</button>
|
||||||
|
</template>
|
||||||
|
· <small>{{ connection }}</small>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<ul v-if="notices.list.value.length">
|
||||||
|
<li v-for="n in notices.list.value" :key="n.id" @click="notices.dismiss(n.id)">
|
||||||
|
[{{ n.type }}] {{ n.text }}
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<hr />
|
||||||
|
<main>
|
||||||
|
<NuxtPage />
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
@ -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<AuthUser | null>('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')
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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')
|
||||||
|
})
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
<script setup lang="ts">
|
||||||
|
await navigateTo('/worlds', { replace: true })
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div />
|
||||||
|
</template>
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
<script setup lang="ts">
|
||||||
|
const { login } = useAuth()
|
||||||
|
const errorText = useErrorText()
|
||||||
|
const mock = isMockServer
|
||||||
|
|
||||||
|
const email = ref('')
|
||||||
|
const password = ref('')
|
||||||
|
const error = ref<string | null>(null)
|
||||||
|
const busy = ref(false)
|
||||||
|
|
||||||
|
async function submit() {
|
||||||
|
busy.value = true
|
||||||
|
error.value = null
|
||||||
|
try {
|
||||||
|
await login(email.value, password.value)
|
||||||
|
await navigateTo('/worlds')
|
||||||
|
} catch (e) {
|
||||||
|
error.value = errorText(e)
|
||||||
|
} finally {
|
||||||
|
busy.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<section>
|
||||||
|
<h1>{{ $t('auth.login.title') }}</h1>
|
||||||
|
<p v-if="mock"><small>{{ $t('auth.mock_hint') }}</small></p>
|
||||||
|
<form @submit.prevent="submit">
|
||||||
|
<p>
|
||||||
|
<label>{{ $t('auth.email') }}<br /><input v-model="email" type="email" autocomplete="email" required /></label>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<label>{{ $t('auth.password') }}<br /><input v-model="password" type="password" autocomplete="current-password" required /></label>
|
||||||
|
</p>
|
||||||
|
<p v-if="error" role="alert">{{ error }}</p>
|
||||||
|
<button type="submit" :disabled="busy">{{ $t('auth.login.submit') }}</button>
|
||||||
|
</form>
|
||||||
|
<p><NuxtLink to="/register">{{ $t('auth.login.to_register') }}</NuxtLink></p>
|
||||||
|
</section>
|
||||||
|
</template>
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
<script setup lang="ts">
|
||||||
|
const { register } = useAuth()
|
||||||
|
const errorText = useErrorText()
|
||||||
|
const mock = isMockServer
|
||||||
|
|
||||||
|
const name = ref('')
|
||||||
|
const email = ref('')
|
||||||
|
const password = ref('')
|
||||||
|
const error = ref<string | null>(null)
|
||||||
|
const busy = ref(false)
|
||||||
|
|
||||||
|
async function submit() {
|
||||||
|
busy.value = true
|
||||||
|
error.value = null
|
||||||
|
try {
|
||||||
|
await register(name.value, email.value, password.value)
|
||||||
|
await navigateTo('/worlds')
|
||||||
|
} catch (e) {
|
||||||
|
error.value = errorText(e)
|
||||||
|
} finally {
|
||||||
|
busy.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<section>
|
||||||
|
<h1>{{ $t('auth.register.title') }}</h1>
|
||||||
|
<p v-if="mock"><small>{{ $t('auth.mock_hint') }}</small></p>
|
||||||
|
<form @submit.prevent="submit">
|
||||||
|
<p>
|
||||||
|
<label>{{ $t('auth.name') }}<br /><input v-model="name" type="text" autocomplete="nickname" required /></label>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<label>{{ $t('auth.email') }}<br /><input v-model="email" type="email" autocomplete="email" required /></label>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<label>{{ $t('auth.password') }}<br /><input v-model="password" type="password" autocomplete="new-password" required /></label>
|
||||||
|
</p>
|
||||||
|
<p v-if="error" role="alert">{{ error }}</p>
|
||||||
|
<button type="submit" :disabled="busy">{{ $t('auth.register.submit') }}</button>
|
||||||
|
</form>
|
||||||
|
<p><NuxtLink to="/login">{{ $t('auth.register.to_login') }}</NuxtLink></p>
|
||||||
|
</section>
|
||||||
|
</template>
|
||||||
Loading…
Reference in New Issue