@m4l/layouts 0.1.14 → 0.1.15

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,130 @@
1
+ import { createContext as T, useReducer as y, useEffect as d } from "react";
2
+ import { useHostTools as E, useNetwork as S, useEnvironment as v, useLocalStorageWithListener as N, EmitEvents as O, getLocalStorage as w, setLocalStorage as r } from "@m4l/core";
3
+ import { jsx as D } from "react/jsx-runtime";
4
+ var n = /* @__PURE__ */ ((e) => (e.Initial = "INITIALIZE", e.Login = "LOGIN", e.Logout = "LOGOUT", e))(n || {});
5
+ const _ = {
6
+ isAuthenticated: !1,
7
+ isInitialized: !1,
8
+ user: null
9
+ }, P = (e, t) => {
10
+ switch (t.type) {
11
+ case "INITIALIZE":
12
+ return {
13
+ isAuthenticated: t.payload.isAuthenticated,
14
+ isInitialized: !0,
15
+ user: t.payload.user
16
+ };
17
+ case "LOGIN":
18
+ return {
19
+ ...e,
20
+ isAuthenticated: !0,
21
+ user: t.payload.user
22
+ };
23
+ case "LOGOUT":
24
+ return {
25
+ ...e,
26
+ isAuthenticated: !1,
27
+ user: null
28
+ };
29
+ default:
30
+ return e;
31
+ }
32
+ }, x = T(null);
33
+ function z(e) {
34
+ e({
35
+ type: n.Initial,
36
+ payload: {
37
+ isAuthenticated: !1,
38
+ user: null
39
+ }
40
+ });
41
+ }
42
+ function R(e) {
43
+ const {
44
+ children: t
45
+ } = e, [h, s] = y(P, _), {
46
+ events_add_listener: I
47
+ } = E(), {
48
+ networkOperation: u
49
+ } = S(), {
50
+ domain_token: m
51
+ } = v(), [f, l] = N(
52
+ "vSession",
53
+ new Date().getTime() + ""
54
+ );
55
+ d(() => {
56
+ (async () => {
57
+ u({
58
+ method: "GET",
59
+ endPoint: "auth/login",
60
+ parms: {
61
+ user_data: !0
62
+ },
63
+ checkUnAuthorized: !1
64
+ }).then((a) => {
65
+ s({
66
+ type: n.Initial,
67
+ payload: {
68
+ isAuthenticated: !0,
69
+ user: a.user
70
+ }
71
+ }), w("userData", {
72
+ email: a.user.email,
73
+ remember: !0
74
+ })?.email !== a.user.email && r("userData", {
75
+ email: a.user.email
76
+ }, !0);
77
+ }).catch(() => {
78
+ z(s);
79
+ });
80
+ })();
81
+ }, [f]);
82
+ const g = async (i, a, o) => {
83
+ await u({
84
+ endPoint: "auth/login",
85
+ method: "POST",
86
+ data: {
87
+ email: i,
88
+ password: a,
89
+ domain_token: m
90
+ }
91
+ }).then((A) => {
92
+ const L = A.data;
93
+ o ? r("userData", {
94
+ email: i,
95
+ remember: o
96
+ }) : r("userData", {
97
+ email: "",
98
+ remember: o
99
+ }), s({
100
+ type: n.Login,
101
+ payload: {
102
+ user: L
103
+ }
104
+ }), l(new Date().getTime() + "");
105
+ });
106
+ }, c = async (i) => {
107
+ i && await u({
108
+ endPoint: "auth/logout",
109
+ method: "POST"
110
+ }), s({
111
+ type: n.Logout
112
+ }), l(new Date().getTime() + "");
113
+ }, p = () => {
114
+ c(!1);
115
+ };
116
+ return d(() => {
117
+ I(O.EMMIT_EVENT_NET_SERVICE_UNAUTHORIZED, p);
118
+ }, []), /* @__PURE__ */ D(x.Provider, {
119
+ value: {
120
+ ...h,
121
+ login: g,
122
+ logout: c
123
+ },
124
+ children: t
125
+ });
126
+ }
127
+ export {
128
+ x as A,
129
+ R as a
130
+ };
@@ -0,0 +1,5 @@
1
+ /// <reference types="react" />
2
+ import type { AuthProviderProps, SessionContextType } from './types';
3
+ declare const AuthContext: import("react").Context<SessionContextType | null>;
4
+ declare function AuthProvider(props: AuthProviderProps): JSX.Element;
5
+ export { AuthContext, AuthProvider };
@@ -0,0 +1,54 @@
1
+ import { Maybe } from '@m4l/core';
2
+ import { ReactNode } from 'react';
3
+ export declare enum EnumTypes {
4
+ Initial = "INITIALIZE",
5
+ Login = "LOGIN",
6
+ Logout = "LOGOUT"
7
+ }
8
+ export declare type AuthProviderProps = {
9
+ children: ReactNode;
10
+ };
11
+ export declare type ActionMap<M extends {
12
+ [index: string]: any;
13
+ }> = {
14
+ [Key in keyof M]: M[Key] extends undefined ? {
15
+ type: Key;
16
+ } : {
17
+ type: Key;
18
+ payload: M[Key];
19
+ };
20
+ };
21
+ export declare type SessionAuthPayload = {
22
+ [EnumTypes.Initial]: {
23
+ isAuthenticated: boolean;
24
+ user: AuthUser;
25
+ };
26
+ [EnumTypes.Login]: {
27
+ user: AuthUser;
28
+ };
29
+ [EnumTypes.Logout]: undefined;
30
+ };
31
+ export declare type SessionActions = ActionMap<SessionAuthPayload>[keyof ActionMap<SessionAuthPayload>];
32
+ export declare type AuthStoreState = {
33
+ user: User;
34
+ remember?: boolean;
35
+ };
36
+ export interface User {
37
+ email: string;
38
+ id: number;
39
+ first_name: string;
40
+ last_name: string;
41
+ account_id: number;
42
+ domain_country_id: number;
43
+ avatar_url?: Maybe<string>;
44
+ }
45
+ export declare type AuthUser = Maybe<User>;
46
+ export declare type AuthState = {
47
+ isAuthenticated: boolean;
48
+ isInitialized: boolean;
49
+ user: AuthUser;
50
+ };
51
+ export interface SessionContextType extends AuthState {
52
+ login: (email: string, password: string, remember: boolean) => object | undefined;
53
+ logout: (isAuthenticated: boolean) => Promise<void>;
54
+ }
@@ -0,0 +1 @@
1
+ export * from './AuthContext';
package/hooks/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './useMasterDetail';
2
+ export * from './useAuth';
2
3
  export * from './useModule';
@@ -0,0 +1,11 @@
1
+ import { useContext as o } from "react";
2
+ import { A as e } from "../../contexts/AuthContext/index.59755369.js";
3
+ const n = () => {
4
+ const t = o(e);
5
+ if (!t)
6
+ throw new Error("Auth context must be use inside AuthProvider");
7
+ return t;
8
+ };
9
+ export {
10
+ n as u
11
+ };
@@ -0,0 +1,2 @@
1
+ export declare const useAuth: () => import("../../contexts/AuthContext/types").SessionContextType;
2
+ export default useAuth;
package/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
+ export * from './contexts';
1
2
  export * from './layouts';
2
3
  export * from './hooks';
package/index.js CHANGED
@@ -1,25 +1,30 @@
1
- import { a as p, d as l, g as y } from "./layouts/ModuleLayout/index.842072c5.js";
1
+ import { A as p, a as d } from "./contexts/AuthContext/index.59755369.js";
2
+ import { a as y, d as n, g as M } from "./layouts/ModuleLayout/index.842072c5.js";
2
3
  import "@m4l/components";
3
4
  import "react";
4
5
  import "@m4l/core";
5
- import { a as n, d as M, g as D } from "./layouts/MasterDetailLayout/index.71de0dc7.js";
6
+ import { a as D, d as x, g as L } from "./layouts/MasterDetailLayout/index.71de0dc7.js";
6
7
  import "@m4l/graphics";
7
8
  import "@mui/material";
8
9
  import "react/jsx-runtime";
9
- import { N as L, d as c, g } from "./layouts/NoAuthModuleLayout/index.5d1098ef.js";
10
+ import { N as c, d as g, g as h } from "./layouts/NoAuthModuleLayout/index.5d1098ef.js";
10
11
  import { u as N } from "./hooks/useMasterDetail/index.8e9e900b.js";
11
- import { u as A } from "./hooks/useModule/index.096d7d13.js";
12
+ import { u as P } from "./hooks/useAuth/index.ae68bf71.js";
13
+ import { u as j } from "./hooks/useModule/index.096d7d13.js";
12
14
  import "@mui/material/styles";
13
15
  export {
14
- n as MasterDetailLayout,
15
- p as ModuleLayout,
16
- L as NoAuthModuleLayout,
17
- M as defaultMasterDetailDictionary,
18
- l as defaultModuleLayoutDictionary,
19
- c as defaultNoAuthModuleLayoutDictionary,
20
- D as getMasterDetailLayoutComponentsDictionary,
21
- y as getModuleLayoutComponentsDictionary,
22
- g as getNoAuthModuleLayoutComponentsDictionary,
16
+ p as AuthContext,
17
+ d as AuthProvider,
18
+ D as MasterDetailLayout,
19
+ y as ModuleLayout,
20
+ c as NoAuthModuleLayout,
21
+ x as defaultMasterDetailDictionary,
22
+ n as defaultModuleLayoutDictionary,
23
+ g as defaultNoAuthModuleLayoutDictionary,
24
+ L as getMasterDetailLayoutComponentsDictionary,
25
+ M as getModuleLayoutComponentsDictionary,
26
+ h as getNoAuthModuleLayoutComponentsDictionary,
27
+ P as useAuth,
23
28
  N as useMasterDetail,
24
- A as useModule
29
+ j as useModule
25
30
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@m4l/layouts",
3
- "version": "0.1.14",
3
+ "version": "0.1.15",
4
4
  "license": "UNLICENSED",
5
5
  "dependencies": {
6
6
  "@m4l/components": "*",