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
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
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
|
||||
};
|
||||
Reference in New Issue
Block a user