@ssplib/react-components 0.0.46 → 0.0.48
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,9 +1,7 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
export declare function Login({ imgURL, name, children, loginURL
|
|
2
|
+
export declare function Login({ imgURL, name, children, loginURL }: {
|
|
3
3
|
imgURL?: string;
|
|
4
4
|
loginURL: string;
|
|
5
5
|
children: JSX.Element | JSX.Element[];
|
|
6
6
|
name?: string;
|
|
7
|
-
onSuccess: () => void;
|
|
8
|
-
onFail: () => void;
|
|
9
7
|
}): JSX.Element;
|
package/components/page/Login.js
CHANGED
|
@@ -34,13 +34,14 @@ const Container_1 = __importDefault(require("@mui/material/Container"));
|
|
|
34
34
|
const Typography_1 = __importDefault(require("@mui/material/Typography"));
|
|
35
35
|
const React = __importStar(require("react"));
|
|
36
36
|
const react_1 = require("react");
|
|
37
|
-
const FormProvider_1 = __importDefault(require("../providers/FormProvider"));
|
|
38
37
|
const auth_1 = require("../../context/auth");
|
|
39
|
-
|
|
38
|
+
const FormProvider_1 = __importDefault(require("../providers/FormProvider"));
|
|
39
|
+
function Login({ imgURL = '', name = 'Login', children, loginURL }) {
|
|
40
40
|
const [loading, setLoading] = (0, react_1.useState)(false);
|
|
41
|
+
const [error, setError] = (0, react_1.useState)(false);
|
|
41
42
|
const { adLogin } = (0, react_1.useContext)(auth_1.AuthContext);
|
|
42
43
|
function onLogin(data) {
|
|
43
|
-
adLogin(loginURL, data);
|
|
44
|
+
adLogin(loginURL, data, setLoading, setError);
|
|
44
45
|
}
|
|
45
46
|
return (React.createElement(FormProvider_1.default, { onSubmit: onLogin },
|
|
46
47
|
React.createElement(Container_1.default, { component: 'main', maxWidth: 'xs' },
|
|
@@ -54,6 +55,8 @@ function Login({ imgURL = '', name = 'Login', children, loginURL, onSuccess, onF
|
|
|
54
55
|
React.createElement(Typography_1.default, { component: 'h1', variant: 'h5', paddingY: 3 }, name),
|
|
55
56
|
React.createElement(material_1.Stack, { spacing: 3, width: 300 },
|
|
56
57
|
React.createElement(material_1.Stack, { spacing: 1 }, children),
|
|
57
|
-
React.createElement(lab_1.LoadingButton, { type: 'submit', fullWidth: true, variant: 'contained', loading: loading }, "Login")
|
|
58
|
+
React.createElement(lab_1.LoadingButton, { type: 'submit', fullWidth: true, variant: 'contained', loading: loading }, "Login"),
|
|
59
|
+
error && (React.createElement(Box_1.default, { bgcolor: '#ce4257', padding: 2, borderRadius: 2, color: 'white' },
|
|
60
|
+
React.createElement(Typography_1.default, null, "Dados incorretos. Tente novamente!"))))))));
|
|
58
61
|
}
|
|
59
62
|
exports.Login = Login;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
export declare const cookieName = "nextauth.token";
|
|
3
|
-
export declare function LoginProvider({ children, AUTH_URL, redirectURL, validateTokenRoute,
|
|
3
|
+
export declare function LoginProvider({ children, AUTH_URL, redirectURL, validateTokenRoute, }: {
|
|
4
4
|
children: JSX.Element | JSX.Element[];
|
|
5
5
|
AUTH_URL: string;
|
|
6
6
|
validateTokenRoute: string;
|
|
@@ -43,7 +43,7 @@ const react_1 = __importStar(require("react"));
|
|
|
43
43
|
const auth_1 = require("../../context/auth");
|
|
44
44
|
exports.cookieName = 'nextauth.token';
|
|
45
45
|
const userImgName = 'user-data.img';
|
|
46
|
-
function LoginProvider({ children, AUTH_URL, redirectURL = '/', validateTokenRoute,
|
|
46
|
+
function LoginProvider({ children, AUTH_URL, redirectURL = '/', validateTokenRoute, }) {
|
|
47
47
|
const [user, setUser] = (0, react_1.useState)();
|
|
48
48
|
const [userLoaded, setUserLoaded] = (0, react_1.useState)(false);
|
|
49
49
|
const router = (0, router_1.useRouter)();
|
|
@@ -72,7 +72,8 @@ function LoginProvider({ children, AUTH_URL, redirectURL = '/', validateTokenRou
|
|
|
72
72
|
}
|
|
73
73
|
});
|
|
74
74
|
}, []);
|
|
75
|
-
function adLogin(loginURL, data) {
|
|
75
|
+
function adLogin(loginURL, data, setLoading, setError) {
|
|
76
|
+
setLoading(true);
|
|
76
77
|
fetch(loginURL, {
|
|
77
78
|
method: 'POST',
|
|
78
79
|
body: JSON.stringify(Object.assign(Object.assign({}, data), { cpf: data.cpf.replaceAll(/[.-]/g, '') })),
|
|
@@ -82,6 +83,7 @@ function LoginProvider({ children, AUTH_URL, redirectURL = '/', validateTokenRou
|
|
|
82
83
|
}).then((res) => {
|
|
83
84
|
if (res.ok) {
|
|
84
85
|
res.json().then((j) => {
|
|
86
|
+
setError(false);
|
|
85
87
|
const token = j.accessToken;
|
|
86
88
|
const user = (0, jwt_decode_1.default)(token);
|
|
87
89
|
setUser({
|
|
@@ -94,8 +96,11 @@ function LoginProvider({ children, AUTH_URL, redirectURL = '/', validateTokenRou
|
|
|
94
96
|
router.replace(redirectURL).finally(() => setUserLoaded(true));
|
|
95
97
|
});
|
|
96
98
|
}
|
|
97
|
-
else
|
|
99
|
+
else {
|
|
100
|
+
setLoading(false);
|
|
98
101
|
setUserLoaded(true);
|
|
102
|
+
setError(true);
|
|
103
|
+
}
|
|
99
104
|
});
|
|
100
105
|
}
|
|
101
106
|
// chamado no callback de login
|
package/package.json
CHANGED
package/types/auth.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
1
2
|
export interface User {
|
|
2
3
|
name: string;
|
|
3
4
|
token: string;
|
|
@@ -66,7 +67,7 @@ export interface AuthReturnData {
|
|
|
66
67
|
user: User | null | undefined;
|
|
67
68
|
userLoaded: boolean;
|
|
68
69
|
login: () => void;
|
|
69
|
-
adLogin: (loginURL: string, data: any) => void;
|
|
70
|
+
adLogin: (loginURL: string, data: any, setLoading: React.Dispatch<React.SetStateAction<boolean>>, setError: React.Dispatch<React.SetStateAction<boolean>>) => void;
|
|
70
71
|
saveUserData: (token: AuthReturn) => void;
|
|
71
72
|
logout: () => void;
|
|
72
73
|
}
|