You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
7.9 KiB

  1. import React, {
  2. useEffect, useState} 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. useEffect(() => {
  43. //console.log("POST: " + posts.accessToken);
  44. },[posts]);
  45. const handleMouseDownPassword = (event) => {
  46. event.preventDefault();
  47. };
  48. const tryLogin = () => {
  49. axios.post(`${apiPath}${POST_LOGIN}`,
  50. {
  51. "username": userName,
  52. "password": userPassword
  53. })
  54. .then((response) => {
  55. //setPosts("12354")
  56. // console.log(response.data);
  57. setPosts(response.data);
  58. const userData = {
  59. id: response.data.id,
  60. fullenName: response.data.name,
  61. fullchName: response.data.chName,
  62. email: response.data.email,
  63. type: response.data.type,
  64. role: response.data.role,
  65. abilities: response.data.abilities,
  66. passwordExpiryDate: response.data.passwordExpiryDate,
  67. orgPaymentRecord: response.data.orgPaymentRecord,
  68. orgDnRecord: response.data.orgDnRecord,
  69. //avatar: require('src/assets/images/users/avatar-3.png').default,
  70. }
  71. // const abilities = response.data.abilities
  72. // ability.update(abilities)
  73. const data = {...userData, accessToken: response.data.accessToken, refreshToken: response.data.refreshToken}
  74. dispatch(handleLogin(data))
  75. navigate('/dashboard');
  76. //history.push(getHomeRouteForLoggedInUser("user"))
  77. })
  78. .catch(error => {
  79. console.error(error);
  80. });
  81. }
  82. const onUserNameChange = (event) => {
  83. setUserName(event.target.value);
  84. }
  85. const onPasswordChange = (event) => {
  86. setUserPassword(event.target.value);
  87. }
  88. function getMaxErrStr(num, fieldname){
  89. return intl.formatMessage({ id: 'noMoreThenNWords' },{num:num, fieldname:fieldname?intl.formatMessage({ id: fieldname})+": ":""});
  90. }
  91. return (
  92. <>
  93. <Formik
  94. initialValues={{
  95. email: '',
  96. password: '',
  97. submit: null
  98. }}
  99. validationSchema={Yup.object().shape({
  100. email: Yup.string().max(128,getMaxErrStr(128)).email('Must be a valid email').required('Email is required'),
  101. password: Yup.string().max(60, getMaxErrStr(60)).required('Password is required')
  102. })}
  103. onSubmit={async (values, { setErrors, setStatus, setSubmitting }) => {
  104. try {
  105. setStatus({ success: false });
  106. setSubmitting(false);
  107. } catch (err) {
  108. setStatus({ success: false });
  109. setErrors({ submit: err.message });
  110. setSubmitting(false);
  111. }
  112. }}
  113. >
  114. {({ errors, handleBlur, handleSubmit, isSubmitting, touched }) => (
  115. <form noValidate onSubmit={handleSubmit}>
  116. <Grid container spacing={3}>
  117. <Grid item xs={12}>
  118. <Stack spacing={1}>
  119. <InputLabel htmlFor="email-login">User Name</InputLabel>
  120. <OutlinedInput
  121. id="username"
  122. name="username"
  123. onBlur={handleBlur}
  124. onChange={onUserNameChange}
  125. placeholder="Enter user name"
  126. fullWidth
  127. error={Boolean(touched.email && errors.email)}
  128. />
  129. {touched.email && errors.email && (
  130. <FormHelperText error id="standard-weight-helper-text-email-login">
  131. {errors.email}
  132. </FormHelperText>
  133. )}
  134. </Stack>
  135. </Grid>
  136. <Grid item xs={12}>
  137. <Stack spacing={1}>
  138. <InputLabel htmlFor="password-login">Password</InputLabel>
  139. <OutlinedInput
  140. fullWidth
  141. error={Boolean(touched.password && errors.password)}
  142. id="-password-login"
  143. type={showPassword ? 'text' : 'password'}
  144. name="password"
  145. onBlur={handleBlur}
  146. onChange={onPasswordChange}
  147. endAdornment={
  148. <InputAdornment position="end">
  149. <IconButton
  150. aria-label={intl.formatMessage({
  151. id: showPassword ? "ariaHidePassword" : "ariaShowPassword"
  152. })}
  153. onClick={handleClickShowPassword}
  154. onMouseDown={handleMouseDownPassword}
  155. edge="end"
  156. size="large"
  157. >
  158. {showPassword ? <EyeOutlined /> : <EyeInvisibleOutlined />}
  159. </IconButton>
  160. </InputAdornment>
  161. }
  162. placeholder="Enter password"
  163. />
  164. {touched.password && errors.password && (
  165. <FormHelperText error id="standard-weight-helper-text-password-login">
  166. {errors.password}
  167. </FormHelperText>
  168. )}
  169. </Stack>
  170. </Grid>
  171. <Grid item xs={12} sx={{ mt: -1 }}>
  172. <Stack direction="row" justifyContent="space-between" alignItems="center" spacing={2}>
  173. {/*<FormControlLabel*/}
  174. {/* control={*/}
  175. {/* <Checkbox*/}
  176. {/* checked={checked}*/}
  177. {/* onChange={(event) => setChecked(event.target.checked)}*/}
  178. {/* name="checked"*/}
  179. {/* color="primary"*/}
  180. {/* size="small"*/}
  181. {/* />*/}
  182. {/* }*/}
  183. {/* label={<Typography variant="h6">Keep me sign in</Typography>}*/}
  184. {/*/>*/}
  185. {/*<Link variant="h6" component={RouterLink} to="" color="text.primary">*/}
  186. {/* Forgot Password?*/}
  187. {/*</Link>*/}
  188. </Stack>
  189. </Grid>
  190. {errors.submit && (
  191. <Grid item xs={12}>
  192. <FormHelperText error>{errors.submit}</FormHelperText>
  193. </Grid>
  194. )}
  195. <Grid item xs={12}>
  196. <AnimateButton>
  197. <Button disableElevation onClick={tryLogin}
  198. disabled={isSubmitting} fullWidth size="large" type="submit" variant="contained" color="primary">
  199. <FormattedMessage id="login"/>
  200. </Button>
  201. </AnimateButton>
  202. </Grid>
  203. </Grid>
  204. </form>
  205. )}
  206. </Formik>
  207. </>
  208. );
  209. };
  210. export default AuthLogin;