Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AuthLogin.js 8.2 KiB

před 2 roky
před 2 roky
před 2 roky
před 2 roky
před 2 roky
před 2 roky
před 2 roky
před 1 rokem
před 2 roky
před 2 roky
před 2 roky
před 1 rokem
před 7 měsíci
před 2 roky
před 2 roky
před 2 roky
před 2 roky
před 2 roky
před 2 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import React, {
  2. useEffect, useState, useRef} from 'react';
  3. import {useNavigate} from 'react-router-dom';
  4. // material-ui
  5. import {
  6. Button,
  7. FormHelperText,
  8. Grid,
  9. IconButton,
  10. InputAdornment,
  11. InputLabel,
  12. OutlinedInput,
  13. Stack,
  14. //Typography
  15. } from '@mui/material';
  16. // third party
  17. import * as Yup from 'yup';
  18. import { Formik } from 'formik';
  19. // project import
  20. import AnimateButton from 'components/@extended/AnimateButton';
  21. import {apiPath} from "auth/utils";
  22. import {POST_LOGIN} from "utils/ApiPathConst";
  23. // assets
  24. import { EyeOutlined, EyeInvisibleOutlined } from '@ant-design/icons';
  25. import axios from "axios";
  26. import {useDispatch} from "react-redux";
  27. import {handleLogin} from "auth/index";
  28. import {FormattedMessage} from "react-intl";
  29. // ============================|| FIREBASE - LOGIN ||============================ //
  30. const AuthLogin = () => {
  31. //const ability = useContext(AbilityContext)
  32. const dispatch = useDispatch()
  33. const navigate = useNavigate()
  34. //const [checked, setChecked] = useState(false);
  35. const [showPassword, setShowPassword] = useState(false);
  36. const handleClickShowPassword = () => {
  37. setShowPassword(!showPassword);
  38. };
  39. let [posts, setPosts] = useState([]);
  40. let [userName, setUserName] = useState("");
  41. let [userPassword, setUserPassword] = useState("");
  42. const [loginRequestPending, setLoginRequestPending] = useState(false);
  43. const loginInFlightRef = useRef(false);
  44. useEffect(() => {
  45. //console.log("POST: " + posts.accessToken);
  46. },[posts]);
  47. const handleMouseDownPassword = (event) => {
  48. event.preventDefault();
  49. };
  50. const tryLogin = () => {
  51. if (loginInFlightRef.current) {
  52. return;
  53. }
  54. loginInFlightRef.current = true;
  55. setLoginRequestPending(true);
  56. axios.post(`${apiPath}${POST_LOGIN}`,
  57. {
  58. "username": userName,
  59. "password": userPassword
  60. })
  61. .then((response) => {
  62. //setPosts("12354")
  63. // console.log(response.data);
  64. setPosts(response.data);
  65. const userData = {
  66. id: response.data.id,
  67. fullenName: response.data.name,
  68. fullchName: response.data.chName,
  69. email: response.data.email,
  70. type: response.data.type,
  71. role: response.data.role,
  72. abilities: response.data.abilities,
  73. passwordExpiryDate: response.data.passwordExpiryDate,
  74. orgPaymentRecord: response.data.orgPaymentRecord,
  75. orgDnRecord: response.data.orgDnRecord,
  76. //avatar: require('src/assets/images/users/avatar-3.png').default,
  77. }
  78. // const abilities = response.data.abilities
  79. // ability.update(abilities)
  80. const data = {...userData, accessToken: response.data.accessToken, refreshToken: response.data.refreshToken}
  81. dispatch(handleLogin(data))
  82. navigate('/dashboard');
  83. //history.push(getHomeRouteForLoggedInUser("user"))
  84. })
  85. .catch(error => {
  86. console.error(error);
  87. loginInFlightRef.current = false;
  88. setLoginRequestPending(false);
  89. });
  90. }
  91. const onUserNameChange = (event) => {
  92. setUserName(event.target.value);
  93. }
  94. const onPasswordChange = (event) => {
  95. setUserPassword(event.target.value);
  96. }
  97. function getMaxErrStr(num, fieldname){
  98. return intl.formatMessage({ id: 'noMoreThenNWords' },{num:num, fieldname:fieldname?intl.formatMessage({ id: fieldname})+": ":""});
  99. }
  100. return (
  101. <>
  102. <Formik
  103. initialValues={{
  104. email: '',
  105. password: '',
  106. submit: null
  107. }}
  108. validationSchema={Yup.object().shape({
  109. email: Yup.string().max(128,getMaxErrStr(128)).email('Must be a valid email').required('Email is required'),
  110. password: Yup.string().max(60, getMaxErrStr(60)).required('Password is required')
  111. })}
  112. onSubmit={async (values, { setErrors, setStatus, setSubmitting }) => {
  113. try {
  114. setStatus({ success: false });
  115. setSubmitting(false);
  116. } catch (err) {
  117. setStatus({ success: false });
  118. setErrors({ submit: err.message });
  119. setSubmitting(false);
  120. }
  121. }}
  122. >
  123. {({ errors, handleBlur, handleSubmit, isSubmitting, touched }) => (
  124. <form noValidate onSubmit={handleSubmit}>
  125. <Grid container spacing={3}>
  126. <Grid item xs={12}>
  127. <Stack spacing={1}>
  128. <InputLabel htmlFor="email-login">User Name</InputLabel>
  129. <OutlinedInput
  130. id="username"
  131. name="username"
  132. onBlur={handleBlur}
  133. onChange={onUserNameChange}
  134. placeholder="Enter user name"
  135. fullWidth
  136. error={Boolean(touched.email && errors.email)}
  137. />
  138. {touched.email && errors.email && (
  139. <FormHelperText error id="standard-weight-helper-text-email-login">
  140. {errors.email}
  141. </FormHelperText>
  142. )}
  143. </Stack>
  144. </Grid>
  145. <Grid item xs={12}>
  146. <Stack spacing={1}>
  147. <InputLabel htmlFor="password-login">Password</InputLabel>
  148. <OutlinedInput
  149. fullWidth
  150. error={Boolean(touched.password && errors.password)}
  151. id="-password-login"
  152. type={showPassword ? 'text' : 'password'}
  153. name="password"
  154. onBlur={handleBlur}
  155. onChange={onPasswordChange}
  156. endAdornment={
  157. <InputAdornment position="end">
  158. <IconButton
  159. aria-label={intl.formatMessage({
  160. id: showPassword ? "ariaHidePassword" : "ariaShowPassword"
  161. })}
  162. onClick={handleClickShowPassword}
  163. onMouseDown={handleMouseDownPassword}
  164. edge="end"
  165. size="large"
  166. >
  167. {showPassword ? <EyeOutlined /> : <EyeInvisibleOutlined />}
  168. </IconButton>
  169. </InputAdornment>
  170. }
  171. placeholder="Enter password"
  172. />
  173. {touched.password && errors.password && (
  174. <FormHelperText error id="standard-weight-helper-text-password-login">
  175. {errors.password}
  176. </FormHelperText>
  177. )}
  178. </Stack>
  179. </Grid>
  180. <Grid item xs={12} sx={{ mt: -1 }}>
  181. <Stack direction="row" justifyContent="space-between" alignItems="center" spacing={2}>
  182. {/*<FormControlLabel*/}
  183. {/* control={*/}
  184. {/* <Checkbox*/}
  185. {/* checked={checked}*/}
  186. {/* onChange={(event) => setChecked(event.target.checked)}*/}
  187. {/* name="checked"*/}
  188. {/* color="primary"*/}
  189. {/* size="small"*/}
  190. {/* />*/}
  191. {/* }*/}
  192. {/* label={<Typography variant="h6">Keep me sign in</Typography>}*/}
  193. {/*/>*/}
  194. {/*<Link variant="h6" component={RouterLink} to="" color="text.primary">*/}
  195. {/* Forgot Password?*/}
  196. {/*</Link>*/}
  197. </Stack>
  198. </Grid>
  199. {errors.submit && (
  200. <Grid item xs={12}>
  201. <FormHelperText error>{errors.submit}</FormHelperText>
  202. </Grid>
  203. )}
  204. <Grid item xs={12}>
  205. <AnimateButton>
  206. <Button disableElevation onClick={tryLogin}
  207. disabled={isSubmitting || loginRequestPending} fullWidth size="large" type="submit" variant="contained" color="primary">
  208. <FormattedMessage id="login"/>
  209. </Button>
  210. </AnimateButton>
  211. </Grid>
  212. </Grid>
  213. </form>
  214. )}
  215. </Formik>
  216. </>
  217. );
  218. };
  219. export default AuthLogin;