Files
stpweb/vue/src/views/admin/Login.vue
T
adamf 46f06a033b Login: follow a redirect back to priv.<domain>
stppriv's admin gate sends a signed-out visitor here with an absolute
https://priv.<domain>/... redirect. Only that exact host is allowed, so
it is not an open redirect.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit c8c34d15483722cae145c9501823dbff74cf0c02)
2026-11-26 13:53:16 +00:00

191 lines
4.5 KiB
Vue

<script setup>
/**
* Route `/admin/login` for every account. Redirects to `?redirect=`, else
* /admin or /account. Caddy's gates also set `?redirect=`, often to a non-Vue
* path (see goTo).
*/
import { ref } from "vue";
import { useRouter, useRoute } from "vue-router";
import { useAuthStore } from "@/stores/auth";
import Button from "@/components/input/Button.vue";
const auth = useAuthStore();
// Nothing else here resolves the session, so a signed-in visitor would see the form.
auth.ensureLoaded();
const router = useRouter();
const route = useRoute();
const username = ref("");
const password = ref("");
const submitting = ref(false);
const errorMsg = ref("");
// stppriv's gate sends people here with an absolute priv.<domain> URL.
function isPrivUrl(dest) {
try {
const url = new URL(dest);
return url.protocol === "https:" && url.host === `priv.${window.location.hostname.replace(/^www\./, "")}`;
} catch {
return false;
}
}
// Caddy-gated paths aren't Vue routes, so landing on "404" means a full page
// load. Only site-relative paths (and stppriv) are followed, or it's an open
// redirect.
function goTo(dest) {
if (isPrivUrl(dest)) {
window.location.assign(dest);
return;
}
if (typeof dest !== "string" || !dest.startsWith("/") || dest.startsWith("//")) {
router.push(home());
return;
}
if (router.resolve(dest).name !== "404") {
router.push(dest);
} else {
window.location.assign(dest);
}
}
// Where a signed-in user lands when nothing asked for anywhere in particular.
function home() {
return auth.user.admin ? "/admin" : "/account";
}
async function handleLogin() {
if (submitting.value) return;
submitting.value = true;
errorMsg.value = "";
try {
// logIn() swallows errors, so re-read `loggedIn`.
await auth.logIn(username.value, password.value);
if (auth.loggedIn) {
goTo(route.query.redirect || home());
} else {
errorMsg.value = "Invalid username or password";
}
} finally {
submitting.value = false;
}
}
function handleLogout() {
auth.logOut();
}
</script>
<template>
<main class="login-page">
<div v-if="auth.loggedIn" class="login-card bdr-1">
<h1>Already signed in</h1>
<p>You're logged in as <code>{{ auth.user.username }}</code>.</p>
<div class="login-actions">
<Button @click="router.push(home())">
{{ auth.user.admin ? "Open Admin" : "Your Services" }}
</Button>
<Button @click="handleLogout">Log Out</Button>
</div>
</div>
<!-- A real <form>, so Enter submits from either field and password
managers recognise it as a sign-in. -->
<form v-else class="login-card bdr-1" @submit.prevent="handleLogin">
<h1>Login</h1>
<small class="login-hint">
Sign in to continue.
</small>
<label class="login-field">
<span>Username</span>
<input
type="text"
v-model="username"
autocomplete="username"
placeholder="Username"
required
/>
</label>
<label class="login-field">
<span>Password</span>
<input
type="password"
v-model="password"
autocomplete="current-password"
placeholder="Password"
required
/>
</label>
<small v-if="errorMsg" class="login-error" role="alert">
{{ errorMsg }}
</small>
<Button type="submit" class="mt-1 py-2" :disabled="submitting">
{{ submitting ? "Signing in…" : "Log In" }}
</Button>
</form>
</main>
</template>
<style scoped>
.login-page {
display: flex;
align-items: center;
justify-content: center;
min-height: 70vh;
padding: 16px;
}
.login-card {
width: 100%;
max-width: 380px;
background-color: var(--color-surface);
padding: 18px 20px 12px;
display: flex;
flex-direction: column;
gap: 8px;
}
.login-hint {
margin-bottom: 6px;
}
.login-field {
display: flex;
flex-direction: column;
gap: 2px;
}
.login-field span {
font-family: var(--font-heading);
color: var(--color-primary);
letter-spacing: 0.05em;
padding-left: 2px;
}
.login-field input {
transition:
border-color 120ms ease,
box-shadow 120ms ease,
background-color 120ms ease;
}
.login-field input:focus {
outline: none;
border-color: var(--color-tertiary);
box-shadow: 0 0 0 2px var(--color-quaternary);
background-color: var(--color-surface-tint);
}
.login-error {
color: var(--color-tertiary);
border: 1px dashed var(--color-tertiary);
padding: 4px 8px;
}
.login-actions {
display: flex;
gap: 6px;
}
</style>