@dyrected/admin 2.5.46 → 2.5.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.mjs +31 -5
  2. package/package.json +3 -3
package/dist/index.mjs CHANGED
@@ -75,7 +75,8 @@ function DyrectedProvider({ children, apiKey: initialApiKey, baseUrl: initialBas
75
75
  const [apiKey, setApiKey] = useState(() => initialApiKey || (typeof window !== "undefined" ? localStorage.getItem("dyrected_key") : null) || void 0);
76
76
  const [siteId, setSiteId] = useState(() => initialSiteId || (typeof window !== "undefined" ? localStorage.getItem("dyrected_site_id") : null) || void 0);
77
77
  const [schemas, setSchemas] = useState(null);
78
- const [user, setUser] = useState(null);
78
+ const initialTokenUser = useMemo(() => initialToken ? decodeTokenPayload(initialToken) : null, [initialToken]);
79
+ const [user, setUser] = useState(() => initialTokenUser);
79
80
  const [authCollectionSlug, setAuthCollectionSlug] = useState(() => (typeof window !== "undefined" ? localStorage.getItem("dyrected_admin_auth_collection") : null) || null);
80
81
  const client = useMemo(() => {
81
82
  if (!baseUrl) return null;
@@ -106,6 +107,7 @@ function DyrectedProvider({ children, apiKey: initialApiKey, baseUrl: initialBas
106
107
  cancelled = true;
107
108
  };
108
109
  }, [client]);
110
+ const activeUser = initialTokenUser ?? user;
109
111
  useEffect(() => {
110
112
  if (initialToken && client) client.setToken(initialToken);
111
113
  }, [initialToken, client]);
@@ -143,18 +145,18 @@ function DyrectedProvider({ children, apiKey: initialApiKey, baseUrl: initialBas
143
145
  schemas
144
146
  ]);
145
147
  useEffect(() => {
146
- if (initialToken || !client || !schemas || user) return;
148
+ if (initialToken || !client || !schemas || activeUser) return;
147
149
  const token = localStorage.getItem("dyrected_token");
148
150
  const resolvedCollectionSlug = authCollectionSlug || getAdminCollectionSlug(schemas);
149
151
  if (!token || !resolvedCollectionSlug) return;
150
152
  client.setToken(token);
151
153
  client.collection(resolvedCollectionSlug).me().then((nextUser) => setUser(nextUser), () => setUser(null));
152
154
  }, [
155
+ activeUser,
153
156
  authCollectionSlug,
154
157
  client,
155
158
  initialToken,
156
- schemas,
157
- user
159
+ schemas
158
160
  ]);
159
161
  const logout = useCallback(() => {
160
162
  localStorage.removeItem("dyrected_url");
@@ -183,13 +185,37 @@ function DyrectedProvider({ children, apiKey: initialApiKey, baseUrl: initialBas
183
185
  logout,
184
186
  isAuthenticated: !!baseUrl && !!apiKey,
185
187
  schemas,
186
- user,
188
+ user: activeUser,
187
189
  initialToken,
188
190
  components
189
191
  },
190
192
  children
191
193
  });
192
194
  }
195
+ function decodeTokenPayload(token) {
196
+ const payload = token.split(".")[1];
197
+ if (!payload) return null;
198
+ try {
199
+ const normalized = payload.replace(/-/g, "+").replace(/_/g, "/");
200
+ const padded = normalized.padEnd(Math.ceil(normalized.length / 4) * 4, "=");
201
+ const json = decodeURIComponent(atob(padded).split("").map((char) => `%${char.charCodeAt(0).toString(16).padStart(2, "0")}`).join(""));
202
+ const parsed = JSON.parse(json);
203
+ if (!parsed || typeof parsed !== "object") return null;
204
+ const user = parsed;
205
+ const roles = Array.isArray(user.roles) ? user.roles.filter((role) => typeof role === "string").map(normalizeCloudRole) : [];
206
+ const role = typeof user.role === "string" ? normalizeCloudRole(user.role) : void 0;
207
+ return {
208
+ ...user,
209
+ ...role ? { role } : {},
210
+ roles: roles.length > 0 ? roles : role ? [role] : roles
211
+ };
212
+ } catch {
213
+ return null;
214
+ }
215
+ }
216
+ function normalizeCloudRole(role) {
217
+ return role === "owner" ? "admin" : role;
218
+ }
193
219
  //#endregion
194
220
  //#region src/providers/query-provider.tsx
195
221
  function QueryProvider({ children }) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dyrected/admin",
3
- "version": "2.5.46",
3
+ "version": "2.5.47",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"
@@ -64,8 +64,8 @@
64
64
  "tailwind-merge": "^3.5.0",
65
65
  "tailwindcss-animate": "^1.0.7",
66
66
  "zod": "^3.25.76",
67
- "@dyrected/core": "^2.5.46",
68
- "@dyrected/sdk": "^2.5.46"
67
+ "@dyrected/core": "^2.5.47",
68
+ "@dyrected/sdk": "^2.5.47"
69
69
  },
70
70
  "peerDependencies": {
71
71
  "@tanstack/react-query": "^5.0.0",