Files
MapMil/centers/analytics/frontend/src/composables/useAuth.ts
T
gitrusprusandCursor 8ab606747f Add public map with JWT admin login and protect admin APIs.
Keep map reads open; gate admin UI/nav and object mutations behind env-based admin credentials, and default parser batch limit to 10.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-16 23:04:21 +03:00

42 lines
938 B
TypeScript

import { computed, ref } from "vue";
import { useRouter } from "vue-router";
import { clearToken, getToken, isAuthenticated, login as doLogin, logout as doLogout } from "../auth";
export function useAuth() {
const router = useRouter();
const busy = ref(false);
const error = ref("");
const authenticated = computed(() => isAuthenticated.value);
async function login(username: string, password: string): Promise<boolean> {
busy.value = true;
error.value = "";
try {
await doLogin(username, password);
return true;
} catch (err) {
clearToken();
error.value = err instanceof Error ? err.message : "Не удалось войти";
return false;
} finally {
busy.value = false;
}
}
function logout(): void {
doLogout();
void router.push({ name: "map" });
}
return {
authenticated,
busy,
error,
getToken,
login,
logout,
};
}