authera 1.2.8-test-2 → 1.2.8-test-4

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.
@@ -1,5 +1,5 @@
1
1
  import React from "react";
2
- import { type AuthHookSettings } from "..";
2
+ import { type AuthHookSettings } from "./types";
3
3
  export type userBaseData<T extends string> = {
4
4
  id: string;
5
5
  username: string;
@@ -2,11 +2,11 @@
2
2
  import Cookie from "js-cookie";
3
3
  export function name2storage(name) {
4
4
  const cookie = {
5
- get: (key) => JSON.parse(Cookie.get(key) || "{}"),
5
+ get: (key) => JSON.parse(Cookie.get(key) ?? "null"),
6
6
  set: (key, value) => Cookie.set(key, JSON.stringify(value)),
7
7
  };
8
8
  const local = {
9
- get: (key) => JSON.parse(localStorage.getItem(key) || "{}"),
9
+ get: (key) => JSON.parse(localStorage.getItem(key) ?? "null"),
10
10
  set: (key, value) => localStorage.setItem(key, JSON.stringify(value)),
11
11
  };
12
12
  const mapStorage = {
@@ -0,0 +1,10 @@
1
+ import { customeFunc, storagesNames } from "./storage";
2
+ export interface AuthHookSettings<T extends string> {
3
+ backendUrl: string;
4
+ storage: storagesNames | customeFunc;
5
+ tokenType: "jwt";
6
+ refreshStrategy: "silent";
7
+ fallback_401_url: string;
8
+ on_after_login?: (response_data: any) => void;
9
+ on_after_step?: (step_key: string) => void;
10
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -6,7 +6,8 @@ export function useAuth() {
6
6
  if (!ctx)
7
7
  throw new Error("useAuth must be used within an AuthContext");
8
8
  // -------------------------------------------------- data
9
- const { permits: permitsData, ...user } = ctx.userData;
9
+ const { permits: permitsData = [], ...user } = (ctx.userData ||
10
+ {});
10
11
  const prm = (permitsData || []);
11
12
  // -------------------------------------------------- funtions
12
13
  const isPermitted = (perm) => {
package/dist/index.d.ts CHANGED
@@ -1,13 +1,5 @@
1
1
  import { customeFunc, storagesNames } from "./helper/storage";
2
- export interface AuthHookSettings<T extends string> {
3
- backendUrl: string;
4
- storage: storagesNames | customeFunc;
5
- tokenType: "jwt";
6
- refreshStrategy: "silent";
7
- fallback_401_url: string;
8
- on_after_login?: (response_data: any) => void;
9
- on_after_step?: (step_key: string) => void;
10
- }
2
+ import { type AuthHookSettings } from "./helper/types";
11
3
  export default function AuthHook<T extends string>(props: AuthHookSettings<T>): {
12
4
  createAuthProvider: (children: React.ReactNode) => React.ReactNode;
13
5
  useAuth: () => {
@@ -1,6 +1,6 @@
1
1
  import React from "react";
2
2
  import type { customeFunc as storageFunc } from "../helper/storage";
3
- import { type AuthHookSettings } from "..";
3
+ import { type AuthHookSettings } from "../helper/types";
4
4
  interface AuthProviderProps<T extends string> {
5
5
  children: React.ReactNode;
6
6
  storage: storageFunc;
package/dist/web/index.js CHANGED
@@ -1,14 +1,34 @@
1
1
  "use client";
2
2
  import { jsx as _jsx } from "react/jsx-runtime";
3
+ import React from "react";
3
4
  import { AuthContext, } from "../helper/context";
4
5
  export default function AuthProvider({ children, storage, authera_props, }) {
5
6
  /**
6
7
  * Context Provider
7
8
  */
8
9
  const Provider = AuthContext.Provider;
9
- const userData = storage.get("userData");
10
+ const [userData, setUserData] = React.useState(storage.get("userData"));
11
+ const [accessToken, setAccessToken] = React.useState(storage.get("access_token") ?? null);
12
+ const [refreshToken, setRefreshToken] = React.useState(storage.get("refresh_token") ?? null);
10
13
  const set = (key, value) => {
14
+ if (key === "userData")
15
+ setUserData(value);
16
+ if (key === "access_token")
17
+ setAccessToken(value);
18
+ if (key === "refresh_token")
19
+ setRefreshToken(value);
11
20
  storage.set(key, value);
12
21
  };
13
- return (_jsx(Provider, { value: { userData, set, authera_props }, children: children }));
22
+ return (_jsx(Provider, { value: {
23
+ userData: (userData ||
24
+ {
25
+ id: "",
26
+ username: "",
27
+ permits: [],
28
+ }),
29
+ access_token: accessToken,
30
+ refresh_token: refreshToken,
31
+ set,
32
+ authera_props,
33
+ }, children: children }));
14
34
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "authera",
3
- "version": "1.2.8-test-2",
3
+ "version": "1.2.8-test-4",
4
4
  "description": "this project is a simple auth hook for react",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",