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.
 
 

242 lines
8.2 KiB

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