muffin-admin 11.5.0 → 11.5.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "muffin-admin",
3
- "version": "11.5.0",
3
+ "version": "11.5.1",
4
4
  "description": "NPM package for https://github.com/klen/muffin-admin",
5
5
  "files": [
6
6
  "src",
@@ -1,92 +1,107 @@
1
- import Cookies from "js-cookie"
2
- import { findBuilder, makeRequest, requestHeaders, setupAdmin } from "."
3
- import { AdminOpts } from "./types"
1
+ import Cookies from "js-cookie";
2
+ import { findBuilder, makeRequest, requestHeaders, setupAdmin } from ".";
3
+ import type { AdminOpts } from "./types";
4
4
 
5
5
  setupAdmin(
6
- ["auth-storage-get"],
7
- ({ storage, storageName }: AdminOpts["auth"]) =>
8
- () =>
9
- storage == "localstorage" ? localStorage.getItem(storageName) : Cookies.get(storageName)
10
- )
11
-
12
- setupAdmin(["auth-storage-set"], ({ storage }) => (name, value) => {
13
- if (storage == "localstorage") {
14
- localStorage.setItem(name, value)
15
- requestHeaders["Authorization"] = value
16
- } else Cookies.set(name, value)
17
- })
6
+ ["auth-storage-get"],
7
+ ({ storage, storageName }: AdminOpts["auth"]) =>
8
+ () =>
9
+ storage == "localstorage"
10
+ ? localStorage.getItem(storageName)
11
+ : Cookies.get(storageName),
12
+ );
18
13
 
19
- export function MuffinAuthProvider(props: AdminOpts["auth"]) {
20
- const { identityURL, authorizeURL, logoutURL, required, storageName } = props
21
-
22
- const authGet = findBuilder(["auth-storage-get"])(props)
23
- const authSet = findBuilder(["auth-storage-set"])(props)
24
-
25
- // Initialize request headers
26
- authSet(storageName, authGet(storageName))
27
-
28
- const getIdentity = async () => {
29
- if (identityURL) {
30
- const { json } = await makeRequest(identityURL)
31
- return json
32
- }
33
- }
34
-
35
- if (required)
36
- return {
37
- login: async (data) => {
38
- if (!authorizeURL) throw { message: "Authorization is not supported" }
39
-
40
- const { json } = await makeRequest(authorizeURL, {
41
- data,
42
- method: "POST",
43
- })
44
- authSet(storageName, json)
45
- },
46
-
47
- checkError: async (error) => {
48
- const { status } = error || {}
49
-
50
- if (status == 401 || status == 403) {
51
- // Clean storage
52
- authSet(storageName, "")
53
-
54
- throw {
55
- message: "Invalid authorization",
56
- redirectTo: logoutURL,
57
- logoutUser: !logoutURL,
58
- }
59
- }
60
- },
61
-
62
- checkAuth: async () => {
63
- const auth = authGet(storageName)
64
- if (!auth) throw { message: "Authorization required" }
65
-
66
- if (!identityURL) return auth
67
-
68
- const user = await getIdentity()
69
- if (!user) throw { message: "Authorization required" }
70
-
71
- return user
72
- },
73
-
74
- getAuthorization: () => authGet(storageName),
75
-
76
- logout: async () => {
77
- // Clean storage
78
- authSet(storageName, "")
79
-
80
- if (logoutURL) window.location.href = logoutURL
81
- },
82
-
83
- getIdentity,
14
+ setupAdmin(
15
+ ["auth-storage-set"],
16
+ ({ storage }) =>
17
+ (name, value, requestOnly) => {
18
+ if (storage == "localstorage") {
19
+ localStorage.setItem(name, value);
20
+ requestHeaders["Authorization"] = value;
21
+ } else Cookies.set(name, value);
22
+ },
23
+ );
84
24
 
85
- getPermissions: async () => {
86
- const role = authGet(storageName + "_role")
87
- return role || ""
88
- },
89
- }
25
+ export function MuffinAuthProvider(props: AdminOpts["auth"]) {
26
+ const {
27
+ identityURL,
28
+ authorizeURL,
29
+ logoutURL,
30
+ required,
31
+ storageName,
32
+ storage,
33
+ } = props;
34
+
35
+ const authGet = findBuilder(["auth-storage-get"])(props);
36
+ const authSet = findBuilder(["auth-storage-set"])(props);
37
+
38
+ // Initialize request headers
39
+ if (storage == "localstorage") {
40
+ const token = authGet(storageName);
41
+ if (token) requestHeaders["Authorization"] = token;
42
+ }
43
+
44
+ const getIdentity = async () => {
45
+ if (identityURL) {
46
+ const { json } = await makeRequest(identityURL);
47
+ return json;
48
+ }
49
+ };
50
+
51
+ if (required)
52
+ return {
53
+ login: async (data) => {
54
+ if (!authorizeURL) throw { message: "Authorization is not supported" };
55
+
56
+ const { json } = await makeRequest(authorizeURL, {
57
+ data,
58
+ method: "POST",
59
+ });
60
+ authSet(storageName, json);
61
+ },
62
+
63
+ checkError: async (error) => {
64
+ const { status } = error || {};
65
+
66
+ if (status == 401 || status == 403) {
67
+ // Clean storage
68
+ authSet(storageName, "");
69
+
70
+ throw {
71
+ message: "Invalid authorization",
72
+ redirectTo: logoutURL,
73
+ logoutUser: !logoutURL,
74
+ };
75
+ }
76
+ },
77
+
78
+ checkAuth: async () => {
79
+ const auth = authGet(storageName);
80
+ if (!auth) throw { message: "Authorization required" };
81
+
82
+ if (!identityURL) return auth;
83
+
84
+ const user = await getIdentity();
85
+ if (!user) throw { message: "Authorization required" };
86
+
87
+ return user;
88
+ },
89
+
90
+ getAuthorization: () => authGet(storageName),
91
+
92
+ logout: async () => {
93
+ // Clean storage
94
+ authSet(storageName, "");
95
+ if (logoutURL) window.location.href = logoutURL;
96
+ },
97
+
98
+ getIdentity,
99
+
100
+ getPermissions: async () => {
101
+ const role = authGet(storageName + "_role");
102
+ return role || "";
103
+ },
104
+ };
90
105
  }
91
106
 
92
- setupAdmin(["auth-provider"], MuffinAuthProvider)
107
+ setupAdmin(["auth-provider"], MuffinAuthProvider);
package/src/index.tsx CHANGED
@@ -37,4 +37,4 @@ export async function initAdmin(prefix = "", containerId: string = "root") {
37
37
  )
38
38
  }
39
39
 
40
- export const VERSION = "11.5.0"
40
+ export const VERSION = "11.5.1"