Files
Job-Info/ModeTemplate/frontend/.svelte-kit/output/server/chunks/auth.svelte.js
T
aewing 05f8e2e3b6 Add ModeTemplate with Svelte 5 frontend and components
- Create reusable components: auth, api-client, shared-types
- ModeTemplate with Hono backend and SvelteKit frontend
- Auth store refactored to class-based Svelte 5 pattern
- SSR-safe hydrate() pattern for localStorage access
- Frontend builds successfully with Svelte 5 runes
2026-01-21 05:12:25 +00:00

71 lines
1.8 KiB
JavaScript

import { x as derived } from "./index2.js";
const STORAGE_KEYS = { PB_TOKEN: "pb_token", SESSION: "auth_session" };
class AuthStore {
_session = null;
_loading = true;
#isAuthenticated = derived(() => this._session !== null);
get isAuthenticated() {
return this.#isAuthenticated();
}
set isAuthenticated($$value) {
return this.#isAuthenticated($$value);
}
get session() {
return this._session;
}
get loading() {
return this._loading;
}
#user = derived(() => this._session ? {
email: this._session.email,
displayName: this._session.displayName
} : null);
get user() {
return this.#user();
}
set user($$value) {
return this.#user($$value);
}
constructor() {
}
/** Call this in onMount to hydrate from localStorage */
hydrate() {
if (typeof localStorage === "undefined") return;
const stored = localStorage.getItem(STORAGE_KEYS.SESSION);
if (stored) {
try {
this._session = JSON.parse(stored);
} catch {
localStorage.removeItem(STORAGE_KEYS.SESSION);
localStorage.removeItem(STORAGE_KEYS.PB_TOKEN);
}
}
this._loading = false;
}
login(session) {
if (typeof localStorage !== "undefined") {
localStorage.setItem(STORAGE_KEYS.SESSION, JSON.stringify(session));
localStorage.setItem(STORAGE_KEYS.PB_TOKEN, session.pbToken);
}
this._session = session;
this._loading = false;
}
logout() {
if (typeof localStorage !== "undefined") {
localStorage.removeItem(STORAGE_KEYS.SESSION);
localStorage.removeItem(STORAGE_KEYS.PB_TOKEN);
}
this._session = null;
}
/** Redirect to backend auth login */
redirectToLogin() {
if (typeof window !== "undefined") {
window.location.href = "/auth/login";
}
}
}
const auth = new AuthStore();
export {
auth as a
};