UNPKG

vormiaguardjs

Version:

Vormia Guard JS - A npm package for authentication and guard management with VormiaPHP laravel Backend application

236 lines (235 loc) 6.06 kB
import { getGlobalVormiaClient as $ } from "vormiaqueryjs"; import { useQuery as E, useMutation as y } from "@tanstack/react-query"; import q, { useState as l, useEffect as v } from "react"; import I from "zustand"; import { ref as m, computed as M } from "vue"; import { writable as g, derived as Q } from "svelte/store"; import { useSignal as w, useTask$ as x } from "@builder.io/qwik"; import { createResource as z } from "solid-js"; let c = null; class B { constructor(t) { this.config = { apiBaseUrl: "", mode: "spa", // 'spa' or 'mpa' redirects: { onFail: "/login", onSuccess: "/" }, ...t }, this.vormiaClient = t.vormiaClient || $(), this.user = null, this.isLoading = !1, this.error = null; } async fetchUser() { this.isLoading = !0, this.error = null; try { const t = await this.vormiaClient.get("/api/user", { withCredentials: !0 }); return this.user = t.data, this.isLoading = !1, this.user; } catch (t) { return this.user = null, this.error = t, this.isLoading = !1, null; } } async login(t) { await this.vormiaClient.get("/sanctum/csrf-cookie", { withCredentials: !0 }); const r = await this.vormiaClient.post("/login", t, { withCredentials: !0 }); return await this.fetchUser(), r; } async logout() { await this.vormiaClient.post("/logout", {}, { withCredentials: !0 }), this.user = null; } isAuthenticated() { return !!this.user; } hasRole(t) { return !this.user || !this.user.roles ? !1 : t ? Array.isArray(t) ? t.some((r) => this.user.roles.includes(r)) : this.user.roles.includes(t) : !0; } async canAccess(t, r = null) { try { let s = `/api/can-access?route=${encodeURIComponent(t)}`; return r && (s += `&middleware=${encodeURIComponent(r)}`), (await this.vormiaClient.get(s, { withCredentials: !0 })).data.allowed; } catch { return !1; } } } function W(e) { return c = new B(e), c; } function a() { if (!c) throw new Error( "Guard client not initialized. Call createGuardClient first." ); return c; } function K() { const e = a(), { data: t, isLoading: r, error: s, refetch: n } = E({ queryKey: ["vormiaguard", "user"], queryFn: () => e.fetchUser(), staleTime: 1e3 * 60 * 5, retry: !1 }); return { user: t, isLoading: r, error: s, isAuthenticated: !!t, refetch: n }; } function X({ onSuccess: e, onError: t, redirectTo: r } = {}) { const s = a(), n = y({ mutationFn: (i) => s.login(i), onSuccess: (i) => { e && e(i), r && (s.config.mode === "spa" && window.history.pushState ? window.location.assign(r) : window.location.href = r); }, onError: t }); return { login: n.mutateAsync, isLoading: n.isLoading, error: n.error }; } function Y({ onSuccess: e, onError: t, redirectTo: r } = {}) { const s = a(), n = y({ mutationFn: () => s.logout(), onSuccess: () => { e && e(), r && (s.config.mode === "spa" && window.history.pushState ? window.location.assign(r) : window.location.href = r); }, onError: t }); return { logout: n.mutateAsync, isLoading: n.isLoading, error: n.error }; } function Z({ children: e, roles: t, fallback: r = null, redirectTo: s, mode: n, backendCheck: i = !1, route: d = null, middleware: p = null }) { const { user: u, isLoading: A, isAuthenticated: G } = K(), f = a(), L = n || (window.history.pushState ? "spa" : "mpa"), [V, U] = l(!i), [S, C] = l(i), R = t ? u && u.roles && t.some((o) => u.roles.includes(o)) : !!u; v(() => { let o = !0; return i && (d || window.location.pathname) && (C(!0), f.canAccess(d || window.location.pathname, p).then((F) => { o && U(F); }).finally(() => { o && C(!1); })), () => { o = !1; }; }, [i, d, p, u]); const h = s || f.config.redirects && f.config.redirects.onFail || "/login"; if (A || S) return null; if (!G || !R || i && !V) { if (L === "spa") try { const { Navigate: o } = require("react-router-dom"); return /* @__PURE__ */ q.createElement(o, { to: h, replace: !0 }); } catch { return window.location.href = h, null; } else return window.location.href = h, null; return r; } return e; } const _ = I((e) => ({ user: null, setUser: (t) => e({ user: t }), clearUser: () => e({ user: null }) })); function T() { const e = m(null), t = m(!0), r = m(null); return a().fetchUser().then((n) => { e.value = n, t.value = !1; }).catch((n) => { r.value = n, t.value = !1; }), { user: e, isLoading: t, error: r, isAuthenticated: M(() => !!e.value) }; } function k() { const e = g(null), t = g(!0), r = g(null); a().fetchUser().then((n) => { e.set(n), t.set(!1); }).catch((n) => { r.set(n), t.set(!1); }); const s = Q(e, (n) => !!n); return { user: e, isLoading: t, error: r, isAuthenticated: s }; } function tt() { const e = w(null), t = w(!0), r = w(null); return x(async () => { try { e.value = await a().fetchUser(); } catch (s) { r.value = s; } finally { t.value = !1; } }), { user: e, isLoading: t, error: r, isAuthenticated: () => !!e.value }; } function et() { const [e, t] = l(null), [r, s] = l(!0), [n, i] = l(null); return v(() => { a().fetchUser().then(t).catch(i).finally(() => s(!1)); }, []), { user: e, isLoading: r, error: n, isAuthenticated: !!e }; } function rt() { const [e] = z(() => a().fetchUser()); return [e, { isAuthenticated: () => !!e() }]; } export { B as VormiaGuardClient, Z as VrmGuard, W as createGuardClient, rt as createVormiaGuardResource, k as createVormiaGuardStore, a as getGlobalGuardClient, et as useVormiaGuardAstro, tt as useVormiaGuardQwik, T as useVormiaGuardVue, _ as useVrmAuthStore, K as useVrmGuard, X as useVrmLogin, Y as useVrmLogout };