UNPKG

@userfrosting/sprinkle-account

Version:
51 lines (50 loc) 1.57 kB
import { defineStore as c } from "pinia"; import a from "axios"; import { useConfigStore as u, useTranslator as o } from "@userfrosting/sprinkle-core/stores"; function d(e) { function i(t) { if (s(`==> Checking authorization access for ${e == null ? void 0 : e.user_name}`, { user: e, slug: t }), e === null) return s("No user defined. Access denied."), !1; if (e.is_master) return s("User is the master (root) user. Access granted."), !0; if (e.permissions === null || e.permissions[t] === void 0) return s("No permissions found. Access denied."), !1; const n = e.permissions[t]; return s("Found matching permission conditions", n), n.some((r) => r === "always()") ? (s("User passed conditions 'always()'. Access granted."), !0) : (n.length !== 0 && s("Unsupported conditions found. Only 'always()' is supported."), s("User failed to pass any of the matched permissions. Access denied."), !1); } function s(t, n) { u().get("site.debug.auth", !1) && console.debug(t, n); } return { checkAccess: i }; } const m = c("auth", { persist: !0, state: () => ({ user: null }), getters: { isAuthenticated: (e) => e.user !== null, checkAccess: (e) => (i) => d(e.user).checkAccess(i) }, actions: { setUser(e) { this.user = e, o().load(); }, unsetUser() { this.user = null, o().load(); }, async check() { return a.get("/account/auth").then((e) => (this.setUser(e.data), this.user)).catch(() => { }); } } }); export { d as a, m as u };