371 lines
9.1 KiB
Vue
371 lines
9.1 KiB
Vue
<script setup lang="ts">
|
|
import { authHeaders, hasAuth, setAuthToken } from '~/utils/auth'
|
|
import { saveDeck, type SavedDeck } from '~/utils/decks'
|
|
import { loadPlaymatChoice, savePlaymatChoice, type PlaymatChoice, type PlaymatSelection } from '~/utils/playmat'
|
|
|
|
const { public: { apiBase } } = useRuntimeConfig()
|
|
const route = useRoute()
|
|
|
|
const mode = ref<'login' | 'register'>('login')
|
|
const username = ref('')
|
|
const password = ref('')
|
|
const signedIn = ref('')
|
|
const busy = ref(false)
|
|
const error = ref('')
|
|
|
|
const playmat = ref<PlaymatChoice>()
|
|
const playmatError = ref('')
|
|
const playmatPreview = computed(() => {
|
|
if (playmat.value?.kind === 'card') return playmat.value
|
|
if (playmat.value?.kind === 'upload') return { kind: 'upload' as const, url: playmat.value.data }
|
|
return null
|
|
})
|
|
|
|
async function choosePlaymat(selection: PlaymatSelection) {
|
|
playmatError.value = ''
|
|
if (selection.kind === 'none') {
|
|
if (!(await savePlaymatChoice(apiBase, undefined))) {
|
|
playmatError.value = 'Could not save that playmat to your account.'
|
|
return
|
|
}
|
|
playmat.value = undefined
|
|
return
|
|
}
|
|
const choice: PlaymatChoice = selection.kind === 'card'
|
|
? { kind: 'card', oracleId: selection.oracleId }
|
|
: { kind: 'upload', data: selection.data }
|
|
if (!(await savePlaymatChoice(apiBase, choice))) {
|
|
playmatError.value = 'Could not save that playmat to your account. Try a smaller image.'
|
|
return
|
|
}
|
|
playmat.value = choice
|
|
}
|
|
|
|
function nextPath(): string {
|
|
const next = route.query.next
|
|
return typeof next === 'string' && next.startsWith('/') && !next.startsWith('//') ? next : '/'
|
|
}
|
|
|
|
function message(caught: unknown): string {
|
|
const data = (caught as { data?: { error?: string } })?.data
|
|
return data?.error ?? 'Could not reach the server.'
|
|
}
|
|
|
|
async function refresh() {
|
|
if (!hasAuth()) return
|
|
try {
|
|
const data = await $fetch<{ username: string }>(`${apiBase}/auth/me`, { headers: authHeaders() })
|
|
signedIn.value = data.username
|
|
} catch {
|
|
setAuthToken(undefined)
|
|
}
|
|
}
|
|
|
|
/** Move decks and cosmetic preferences saved by older browser-only versions. */
|
|
async function migrateLocalData() {
|
|
if (typeof localStorage === 'undefined') return
|
|
|
|
const decks = localStorage.getItem('commander-decks-v1')
|
|
if (decks !== null) {
|
|
try {
|
|
const parsed = JSON.parse(decks) as SavedDeck[]
|
|
if (Array.isArray(parsed)) {
|
|
for (const deck of parsed) await saveDeck(apiBase, deck)
|
|
localStorage.removeItem('commander-decks-v1')
|
|
}
|
|
} catch {
|
|
// Keep corrupt data in place rather than deleting it.
|
|
}
|
|
}
|
|
|
|
const playmat = localStorage.getItem('commander-playmat-v1')
|
|
if (playmat !== null) {
|
|
try {
|
|
const choice = JSON.parse(playmat) as PlaymatChoice | null
|
|
if (await savePlaymatChoice(apiBase, choice ?? undefined)) {
|
|
localStorage.removeItem('commander-playmat-v1')
|
|
}
|
|
} catch {
|
|
// Keep corrupt data in place rather than deleting it.
|
|
}
|
|
}
|
|
}
|
|
|
|
async function submit() {
|
|
busy.value = true
|
|
error.value = ''
|
|
try {
|
|
const data = await $fetch<{ token: string; username: string }>(`${apiBase}/auth/${mode.value}`, {
|
|
method: 'POST',
|
|
body: { username: username.value, password: password.value },
|
|
})
|
|
setAuthToken(data.token)
|
|
signedIn.value = data.username
|
|
try {
|
|
await migrateLocalData()
|
|
} catch {
|
|
// A failed migration must not block signing in.
|
|
}
|
|
await navigateTo(nextPath())
|
|
} catch (caught) {
|
|
error.value = message(caught)
|
|
} finally {
|
|
busy.value = false
|
|
}
|
|
}
|
|
|
|
async function logout() {
|
|
try {
|
|
await $fetch(`${apiBase}/auth/logout`, { method: 'POST', headers: authHeaders() })
|
|
} catch {
|
|
// Clearing the token locally is enough.
|
|
}
|
|
setAuthToken(undefined)
|
|
password.value = ''
|
|
signedIn.value = ''
|
|
}
|
|
|
|
onMounted(async () => {
|
|
await refresh()
|
|
if (!signedIn.value) return
|
|
playmat.value = await loadPlaymatChoice(apiBase)
|
|
})
|
|
</script>
|
|
|
|
<template>
|
|
<main class="page account-page">
|
|
<section class="auth-card" :class="{ wide: signedIn }">
|
|
<header class="auth-head">
|
|
<span class="auth-mark" aria-hidden="true">◆</span>
|
|
<p v-if="signedIn" class="muted">
|
|
Signed in as <strong>{{ signedIn }}</strong>
|
|
</p>
|
|
</header>
|
|
|
|
<template v-if="signedIn">
|
|
<div class="signed-actions">
|
|
<NuxtLink class="auth-link" to="/">Play</NuxtLink>
|
|
<NuxtLink class="auth-link" to="/decks">Decks</NuxtLink>
|
|
<button type="button" class="mini" @click="logout">Sign out</button>
|
|
</div>
|
|
|
|
<section class="prefs">
|
|
<h2>Playmat</h2>
|
|
<p class="muted">
|
|
Your mat for new games. Pick card art or upload an image; you can still change it in-game.
|
|
</p>
|
|
<p v-if="playmatError" class="error">{{ playmatError }}</p>
|
|
<PlaymatPicker :current="playmatPreview" @select="choosePlaymat" />
|
|
</section>
|
|
</template>
|
|
|
|
<template v-else>
|
|
<div class="auth-tabs" role="tablist" aria-label="Account mode">
|
|
<button
|
|
type="button"
|
|
role="tab"
|
|
:aria-selected="mode === 'login'"
|
|
:class="{ active: mode === 'login' }"
|
|
@click="mode = 'login'"
|
|
>Sign in</button>
|
|
<button
|
|
type="button"
|
|
role="tab"
|
|
:aria-selected="mode === 'register'"
|
|
:class="{ active: mode === 'register' }"
|
|
@click="mode = 'register'"
|
|
>Create account</button>
|
|
</div>
|
|
|
|
<form @submit.prevent="submit">
|
|
<label class="field">
|
|
<span>Username</span>
|
|
<input
|
|
v-model="username"
|
|
autocomplete="username"
|
|
minlength="3"
|
|
maxlength="32"
|
|
placeholder="your name"
|
|
required
|
|
>
|
|
</label>
|
|
<label class="field">
|
|
<span>Password</span>
|
|
<input
|
|
v-model="password"
|
|
type="password"
|
|
:autocomplete="mode === 'login' ? 'current-password' : 'new-password'"
|
|
minlength="8"
|
|
maxlength="200"
|
|
placeholder="••••••••"
|
|
required
|
|
>
|
|
</label>
|
|
<p v-if="error" class="error">{{ error }}</p>
|
|
<button type="submit" class="primary" :disabled="busy">
|
|
{{ busy ? 'Working…' : mode === 'login' ? 'Sign in' : 'Create account' }}
|
|
</button>
|
|
</form>
|
|
</template>
|
|
</section>
|
|
</main>
|
|
</template>
|
|
|
|
<style scoped>
|
|
main.account-page {
|
|
min-height: calc(100vh - 3rem);
|
|
max-width: none;
|
|
display: grid;
|
|
place-items: center;
|
|
padding: 2rem 1.25rem;
|
|
background: radial-gradient(60rem 32rem at 50% -12%, rgba(122, 162, 247, 0.13), transparent 70%);
|
|
}
|
|
|
|
.auth-card {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 1.25rem;
|
|
width: 100%;
|
|
max-width: 26rem;
|
|
padding: 1.75rem;
|
|
border: 1px solid var(--line);
|
|
border-radius: 14px;
|
|
background: linear-gradient(180deg, #1b1f2b, var(--panel));
|
|
box-shadow: 0 24px 60px rgba(0, 0, 0, 0.35);
|
|
}
|
|
|
|
.auth-card.wide {
|
|
max-width: 42rem;
|
|
}
|
|
|
|
.prefs {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 0.6rem;
|
|
padding-top: 1.25rem;
|
|
border-top: 1px solid var(--line);
|
|
}
|
|
|
|
.prefs h2 {
|
|
font-size: 1.1rem;
|
|
}
|
|
|
|
.auth-head {
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
gap: 0.45rem;
|
|
text-align: center;
|
|
}
|
|
|
|
.auth-head h1 {
|
|
font-size: 1.5rem;
|
|
}
|
|
|
|
.auth-head p {
|
|
font-size: 0.9rem;
|
|
}
|
|
|
|
.auth-mark {
|
|
display: grid;
|
|
place-items: center;
|
|
width: 2.5rem;
|
|
height: 2.5rem;
|
|
border: 1px solid var(--line);
|
|
border-radius: 12px;
|
|
background: linear-gradient(135deg, rgba(122, 162, 247, 0.35), rgba(122, 162, 247, 0.08));
|
|
color: var(--accent);
|
|
font-size: 1rem;
|
|
}
|
|
|
|
.auth-tabs {
|
|
display: grid;
|
|
grid-template-columns: 1fr 1fr;
|
|
gap: 0.25rem;
|
|
padding: 0.25rem;
|
|
border: 1px solid var(--line);
|
|
border-radius: 10px;
|
|
background: var(--panel-2);
|
|
}
|
|
|
|
.auth-tabs button {
|
|
padding: 0.5rem;
|
|
border: 0;
|
|
border-radius: 7px;
|
|
background: transparent;
|
|
color: var(--muted);
|
|
}
|
|
|
|
.auth-tabs button:hover {
|
|
border: 0;
|
|
color: var(--text);
|
|
}
|
|
|
|
.auth-tabs button.active {
|
|
color: var(--text);
|
|
background: rgba(122, 162, 247, 0.16);
|
|
box-shadow: inset 0 0 0 1px rgba(122, 162, 247, 0.35);
|
|
}
|
|
|
|
.auth-card form {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 0.85rem;
|
|
}
|
|
|
|
.auth-card .field {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 0.35rem;
|
|
}
|
|
|
|
.auth-card .field span {
|
|
color: var(--muted);
|
|
font-size: 0.8rem;
|
|
}
|
|
|
|
.auth-card .field input {
|
|
width: 100%;
|
|
padding: 0.65rem 0.75rem;
|
|
border-radius: 8px;
|
|
}
|
|
|
|
.auth-card .field input:focus {
|
|
border-color: var(--accent);
|
|
outline: none;
|
|
box-shadow: 0 0 0 3px rgba(122, 162, 247, 0.16);
|
|
}
|
|
|
|
.auth-card button[type="submit"] {
|
|
margin-top: 0.25rem;
|
|
padding: 0.65rem;
|
|
border-radius: 8px;
|
|
font-weight: 600;
|
|
}
|
|
|
|
.auth-card .error {
|
|
text-align: center;
|
|
}
|
|
|
|
.signed-actions {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
justify-content: center;
|
|
gap: 0.6rem;
|
|
}
|
|
|
|
.auth-link {
|
|
padding: 0.45rem 0.7rem;
|
|
border: 1px solid var(--line);
|
|
border-radius: 8px;
|
|
color: var(--muted);
|
|
font-size: 0.85rem;
|
|
text-decoration: none;
|
|
}
|
|
|
|
.auth-link:hover {
|
|
border-color: var(--accent);
|
|
color: var(--text);
|
|
}
|
|
</style>
|