authera 1.0.0

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.
@@ -0,0 +1,12 @@
1
+ import React from "react";
2
+ export type userBaseData<T extends string> = {
3
+ id: string;
4
+ username: string;
5
+ permits: T[];
6
+ };
7
+ export interface AuthContextValueType<T extends string> {
8
+ fallback_401_url: string;
9
+ userData: userBaseData<T>;
10
+ setUserData: (value: userBaseData<T>) => void;
11
+ }
12
+ export declare const AuthContext: React.Context<AuthContextValueType<any> | undefined>;
@@ -0,0 +1,2 @@
1
+ import React from "react";
2
+ export const AuthContext = React.createContext(undefined);
@@ -0,0 +1,6 @@
1
+ export type storagesNames = "cookie" | "local";
2
+ export type customeFunc = {
3
+ get: (key: string) => any | undefined;
4
+ set: (key: string, value: any) => void;
5
+ };
6
+ export declare function name2storage(name: storagesNames): customeFunc;
@@ -0,0 +1,17 @@
1
+ "use client";
2
+ import Cookie from "js-cookie";
3
+ export function name2storage(name) {
4
+ const cookie = {
5
+ get: (key) => JSON.parse(Cookie.get(key) || "{}"),
6
+ set: (key, value) => Cookie.set(key, JSON.stringify(value)),
7
+ };
8
+ const local = {
9
+ get: (key) => JSON.parse(localStorage.getItem(key) || "{}"),
10
+ set: (key, value) => localStorage.setItem(key, JSON.stringify(value)),
11
+ };
12
+ const mapStorage = {
13
+ cookie,
14
+ local,
15
+ };
16
+ return mapStorage[name];
17
+ }
@@ -0,0 +1,20 @@
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
+ }
8
+ export declare function AuthHook<T extends string[]>(props: AuthHookSettings<T>): {
9
+ AuthProvider: ({ children }: {
10
+ children: React.ReactNode;
11
+ }) => import("react/jsx-runtime").JSX.Element;
12
+ useAuth: () => {
13
+ permits: T[];
14
+ isPermitted: (perm: T) => boolean;
15
+ on: (perm: T, callback: () => void, fallback: () => void) => void;
16
+ setPermits: (permits: T[]) => void;
17
+ addPermits: (permits: T[]) => void;
18
+ removePermits: (permits: T[]) => void;
19
+ };
20
+ };
@@ -0,0 +1,14 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { name2storage } from "../helper/storage";
3
+ import AuthProvider from "../web";
4
+ import { useAuth } from "./useAuth";
5
+ export function AuthHook(props) {
6
+ // set storage functions
7
+ let storage = props.storage;
8
+ if (typeof storage === "string")
9
+ storage = name2storage(storage);
10
+ return {
11
+ AuthProvider: ({ children }) => (_jsx(AuthProvider, { storage: storage, children: children })),
12
+ useAuth: () => useAuth(),
13
+ };
14
+ }
@@ -0,0 +1,12 @@
1
+ import { userBaseData } from "../helper/context";
2
+ export declare function useAuth<T extends string>(): {
3
+ permits: T[];
4
+ isPermitted: (perm: T) => boolean;
5
+ on: (perm: T, callback: () => void, fallback?: () => void) => void;
6
+ setPermits: (permits: T[]) => void;
7
+ addPermits: (permits: T[]) => void;
8
+ removePermits: (permits: T[]) => void;
9
+ setUserData: (userData: userBaseData<T>) => void;
10
+ fallback_401_url: string;
11
+ isPermittedAll: (perms: T[]) => boolean;
12
+ };
@@ -0,0 +1,56 @@
1
+ import { AuthContext, } from "../helper/context";
2
+ import React from "react";
3
+ export function useAuth() {
4
+ // -------------------------------------------------- context
5
+ const ctx = React.useContext(AuthContext);
6
+ if (!ctx)
7
+ throw new Error("useAuth must be used within an AuthContext");
8
+ // -------------------------------------------------- data
9
+ const { permits: permitsData } = ctx.userData;
10
+ const prm = (permitsData || []);
11
+ const fallback_401_url = ctx.fallback_401_url;
12
+ // -------------------------------------------------- funtions
13
+ const isPermitted = (perm) => {
14
+ return prm.includes(perm);
15
+ };
16
+ const isPermittedAll = (perms) => {
17
+ return perms.every((p) => prm.includes(p));
18
+ };
19
+ const on = (perm, callback, fallback) => {
20
+ if (isPermitted(perm))
21
+ callback();
22
+ else if (fallback)
23
+ fallback();
24
+ };
25
+ const setPermits = (permits) => {
26
+ ctx.setUserData({ ...ctx.userData, permits });
27
+ };
28
+ const addPermits = (permits) => {
29
+ if (typeof permits !== "object")
30
+ return;
31
+ ctx.setUserData({
32
+ ...ctx.userData,
33
+ permits: [...prm.filter((p) => !permits.includes(p)), ...permits],
34
+ });
35
+ };
36
+ const removePermits = (permits) => {
37
+ ctx.setUserData({
38
+ ...ctx.userData,
39
+ permits: prm.filter((p) => !permits.includes(p)),
40
+ });
41
+ };
42
+ const setUserData = (userData) => {
43
+ ctx.setUserData(userData);
44
+ };
45
+ return {
46
+ permits: prm,
47
+ isPermitted,
48
+ on,
49
+ setPermits,
50
+ addPermits,
51
+ removePermits,
52
+ setUserData,
53
+ fallback_401_url,
54
+ isPermittedAll,
55
+ };
56
+ }
@@ -0,0 +1,23 @@
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
+ }
9
+ export default function AuthHook<T extends string>(props: AuthHookSettings<T>): {
10
+ createAuthProvider: (children: React.ReactNode) => React.ReactNode;
11
+ useAuth: () => {
12
+ permits: T[];
13
+ isPermitted: (perm: T) => boolean;
14
+ on: (perm: T, callback: () => void, fallback?: (() => void) | undefined) => void;
15
+ setPermits: (permits: T[]) => void;
16
+ addPermits: (permits: T[]) => void;
17
+ removePermits: (permits: T[]) => void;
18
+ setUserData: (userData: import("./helper/context").userBaseData<T>) => void;
19
+ fallback_401_url: string;
20
+ isPermittedAll: (perms: T[]) => boolean;
21
+ };
22
+ };
23
+ export { default as AuthGuard } from "./web/guard";
package/dist/index.js ADDED
@@ -0,0 +1,16 @@
1
+ "use client";
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { name2storage } from "./helper/storage";
4
+ import AuthProvider from "./web";
5
+ import { useAuth } from "./hooks/useAuth";
6
+ export default function AuthHook(props) {
7
+ // set storage functions
8
+ let storage = props.storage;
9
+ if (typeof storage === "string")
10
+ storage = name2storage(storage);
11
+ return {
12
+ createAuthProvider: (children) => (_jsx(AuthProvider, { storage: storage, fallback_401_url: props.fallback_401_url, children: children })),
13
+ useAuth: () => useAuth(),
14
+ };
15
+ }
16
+ export { default as AuthGuard } from "./web/guard";
@@ -0,0 +1,7 @@
1
+ import { ReactNode } from "react";
2
+ export interface AuthGuardProps {
3
+ children: ReactNode;
4
+ permits: string[];
5
+ action: "show" | "hide" | "redirect";
6
+ }
7
+ export default function AuthGuard({ children, permits, action, }: AuthGuardProps): import("react/jsx-runtime").JSX.Element | null | undefined;
@@ -0,0 +1,23 @@
1
+ "use client";
2
+ import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
3
+ import { useAuth } from "../hooks/useAuth";
4
+ export default function AuthGuard({ children, permits, action, }) {
5
+ const { isPermittedAll: isPermittedHook, fallback_401_url } = useAuth();
6
+ const isPermitted = isPermittedHook(permits);
7
+ if (action === "redirect" && !isPermitted) {
8
+ window.location.href = fallback_401_url;
9
+ return null;
10
+ }
11
+ if (action === "show") {
12
+ if (isPermitted)
13
+ return _jsx(_Fragment, { children: children });
14
+ else
15
+ return null;
16
+ }
17
+ if (action === "hide") {
18
+ if (isPermitted)
19
+ return null;
20
+ else
21
+ return _jsx(_Fragment, { children: children });
22
+ }
23
+ }
@@ -0,0 +1,9 @@
1
+ import React from "react";
2
+ import type { customeFunc as storageFunc } from "../helper/storage";
3
+ interface AuthProviderProps<T extends string> {
4
+ children: React.ReactNode;
5
+ storage: storageFunc;
6
+ fallback_401_url: string;
7
+ }
8
+ export default function AuthProvider<T extends string>({ children, storage, fallback_401_url, }: AuthProviderProps<T>): import("react/jsx-runtime").JSX.Element;
9
+ export {};
@@ -0,0 +1,14 @@
1
+ "use client";
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { AuthContext } from "../helper/context";
4
+ export default function AuthProvider({ children, storage, fallback_401_url, }) {
5
+ /**
6
+ * Context Provider
7
+ */
8
+ const Provider = AuthContext.Provider;
9
+ const userData = storage.get("userData");
10
+ const setUserData = (value) => {
11
+ storage.set("userData", value);
12
+ };
13
+ return (_jsx(Provider, { value: { userData, setUserData, fallback_401_url }, children: children }));
14
+ }
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "authera",
3
+ "version": "1.0.0",
4
+ "description": "this project is a simple auth hook for react",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "module": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsc -p tsconfig.json",
20
+ "build:watch": "tsc -p tsconfig.json -w",
21
+ "prepublishOnly": "npm run build",
22
+ "pu": "npm run build && npm publish --access public",
23
+ "link": "npm run build && npm link",
24
+ "test": "vitest"
25
+ },
26
+ "author": "",
27
+ "license": "ISC",
28
+ "devDependencies": {
29
+ "@testing-library/react": "^16.3.0",
30
+ "@types/js-cookie": "^3.0.6",
31
+ "@types/react": "^19.2.2",
32
+ "@vitest/coverage-c8": "^0.33.0",
33
+ "jsdom": "^27.0.1",
34
+ "typescript": "^5.6.3",
35
+ "vitest": "^4.0.5"
36
+ },
37
+ "peerDependencies": {
38
+ "react": "^18 || ^19"
39
+ },
40
+ "dependencies": {
41
+ "js-cookie": "^3.0.5"
42
+ }
43
+ }