import React, { useEffect, useState, lazy, // useContext } from 'react'; import { Link as RouterLink } from 'react-router-dom'; import { useNavigate } from 'react-router-dom'; import { useForm, } from 'react-hook-form' import { iAmSmartPath, clientId, getBowserType, isAppBowser, iAmSmartCallbackPath } from 'auth/utils' //iAmSmartAppPath // material-ui import { Button, //Checkbox, //Divider, //FormControlLabel, FormHelperText, Grid, Link, IconButton, InputAdornment, InputLabel, OutlinedInput, Stack, Typography } from '@mui/material'; // third party import * as yup from 'yup'; import { useFormik, FormikProvider } from 'formik'; // project import //import FirebaseSocial from './FirebaseSocial'; import AnimateButton from 'components/@extended/AnimateButton'; import Loadable from 'components/Loadable'; const PasswordAlertDialog = Loadable(lazy(() => import('./PasswordAlertDialog'))); // assets import { EyeOutlined, EyeInvisibleOutlined } from '@ant-design/icons'; // import axios from "axios"; import iAmSmartICon from 'assets/images/icons/icon_iAmSmart.png'; import { useDispatch } from "react-redux"; import { handleLogin } from "auth/index"; import useJwt from "auth/jwt/useJwt"; import { handleLogoutFunction } from 'auth/index'; import {FormattedMessage, useIntl} from "react-intl"; // import LocaleContext from "components/I18nProvider"; // ============================|| FIREBASE - LOGIN ||============================ // const AuthLoginCustom = () => { const dispatch = useDispatch() const navigate = useNavigate() const intl = useIntl(); // const { setLocaleFromLogin } = useContext(LocaleContext); const [showPassword, setShowPassword] = useState(false); const handleClickShowPassword = () => { setShowPassword(!showPassword); }; // let [posts, setPosts] = useState([]); const [isValid, setisValid] = useState(false); const [open, setOpen] = React.useState(false); const [isButtonDisabled, setIsButtonDisabled] = useState(true); const [errorMassage, setErrorMassage] = useState(''); const handleMouseDownPassword = (event) => { event.preventDefault(); }; const tryLogin = () => { if (isValid) { dispatch(handleLogoutFunction()); // setSumitting(true) useJwt .login({ username: values.username, password: values.password }) .then((response) => { // console.log(response) const userData = { id: response.data.id, fullenName: response.data.name, fullchName: response.data.chName, email: response.data.email, type: response.data.type, role: response.data.role, abilities: response.data.abilities, creditor: response.data.creditor, locale: response.data.preferLocale, //avatar: require('src/assets/images/users/avatar-3.png').default, } const data = { ...userData, accessToken: response.data.accessToken, refreshToken: response.data.refreshToken } // setSuccess(true) // console.log(response.data); if(response.data.type === "GLD"){ // setLocale("en"); localStorage.setItem('locale','en'); }else{ if (response.data.preferLocale ==="zh_HK"){ // setLocale("zh-HK"); localStorage.setItem('locale','zh-HK'); } if (response.data.preferLocale ==="zh-CN"){ // setLocale("zh-CN"); localStorage.setItem('locale','zh-CN'); } } dispatch(handleLogin(data)) navigate('/dashboard'); location.reload() // setSumitting(false) }) .catch((error) => { // setSuccess(false) console.error(error) console.error(error.response.data.error) setErrorMassage(error.response.data.error) setOpen(true) }); } else { setOpen(true) } } const formik = useFormik({ initialValues: ({ username: '', password: '', submit: null }), validationSchema: yup.object().shape({ // username: yup.string().min(6,'用戶名稱最少6位').required('請輸入用戶名稱'), username: yup.string().required(intl.formatMessage({id: 'requireUsername'})), password: yup.string().min(8, intl.formatMessage({id: 'atLeast8CharPassword'})).required(intl.formatMessage({id: 'requirePassword'})) .matches(/^(?=.*[a-z])/, intl.formatMessage({id: 'atLeastOneSmallLetter'})) .matches(/^(?=.*[A-Z])/, intl.formatMessage({id: 'atLeastOneCapLetter'})) .matches(/^(?=.*[0-9])/, intl.formatMessage({id: 'atLeast1Number'})) .matches(/^(?=.*[!@#%&])/, intl.formatMessage({id: 'atLeast1SpecialChar'})), }), }); const checkDataField = (data) => { if (data.username !== "" && data.password !== "" && handlePassword(data.password) // &&handle6Digi(data.username) ) { setisValid(true) setIsButtonDisabled(false); return isValid } else { setisValid(false) setIsButtonDisabled(true); return isValid } }; function handlePassword(password) { let new_pass = password; // regular expressions to validate password var lowerCase = /[a-z]/g; var upperCase = /[A-Z]/g; var numbers = /[0-9]/g; var symbol = /^(?=.*[!@#%&])/; if (!new_pass.match(lowerCase)) { return false; } else if (!new_pass.match(upperCase)) { return false; } else if (!new_pass.match(numbers)) { return false; } else if (!new_pass.match(symbol)) { return false; } else if (new_pass.length < 8) { return false; } else { return true; } } const handleClose = () => { setOpen(false); }; const { values } = formik useEffect(() => { checkDataField(values) }, [values]) const { handleSubmit } = useForm({}) const getQRWithIAmSmart = () => { if (isAppBowser()) { openApp(); } else { openQR(); } } const openQR = () => { let callbackUrl = "https://" + iAmSmartCallbackPath() + "/iamsmart/authcallback"; let url = iAmSmartPath + "/api/v1/auth/getQR" + "?clientID=" + clientId + "&responseType=code" + "&source=" + getBowserType() + "&redirectURI=" + encodeURIComponent(callbackUrl) + "&scope=" + encodeURIComponent("eidapi_auth eidapi_profiles") + "&lang=zh-HK"//en-US, zh-HK, or zh-CN //+"&state=" + "&brokerPage=false" window.location=url; } const openApp = () => { // setTimeout(function () { // openQR(); // }, 1000); // let callbackUrl = "https://" + iAmSmartCallbackPath() + "/iamsmart/authcallback"; // let source = getBowserType() // console.log(source) // let url = iAmSmartAppPath + "auth" // + "?clientID=" + clientId // + "&responseType=code" // + "&source=" + getBowserType() // + "&redirectURI=" + encodeURIComponent(callbackUrl) // + "&scope=" + encodeURIComponent("eidapi_auth eidapi_profiles") // + "&lang=zh-HK"//en-US, zh-HK, or zh-CN // //+"&state=" // + "&brokerPage=true" // window.location=url; let callbackUrl = "https://" + iAmSmartCallbackPath() + "/iamsmart/authcallback"; let url = iAmSmartPath + "/api/v1/auth/getQR" + "?clientID=" + clientId + "&responseType=code" + "&source=" + getBowserType() + "&redirectURI=" + encodeURIComponent(callbackUrl) + "&scope=" + encodeURIComponent("eidapi_auth eidapi_profiles") + "&lang=zh-HK"//en-US, zh-HK, or zh-CN //+"&state=" + "&brokerPage=true" window.location=url; } return (
{ if (e.key === 'Enter') { e.preventDefault(); } }, }} /> {formik.touched.username && formik.errors.username && ( {formik.errors.username} )} {showPassword ? : } } placeholder="" /> {formik.touched.password && formik.errors.password && ( {formik.errors.password} )} ? | ? { intl.formatMessage({id: 'learnMore'})+" >"}
); }; export default AuthLoginCustom;