|
@@ -0,0 +1,167 @@
|
|
|
|
+import React, { useState } from "react";
|
|
|
|
+import {
|
|
|
|
+ Avatar,
|
|
|
|
+ Button,
|
|
|
|
+ Container,
|
|
|
|
+ Grid,
|
|
|
|
+ Paper,
|
|
|
|
+ Typography,
|
|
|
|
+} from "@material-ui/core";
|
|
|
|
+import { GoogleLogin } from "react-google-login";
|
|
|
|
+import LockOutlinedIcon from "@material-ui/icons/LockOutlined";
|
|
|
|
+import useStyles from "./styles";
|
|
|
|
+import Input from "./Input";
|
|
|
|
+import Icon from "./Icon";
|
|
|
|
+import { useDispatch } from "react-redux";
|
|
|
|
+import { AUTH } from "../../redux/reducers/authReducer";
|
|
|
|
+import { useHistory } from "react-router";
|
|
|
|
+import { signIn, signUp } from "../../redux/actions/authThunks";
|
|
|
|
+
|
|
|
|
+const initialState = {
|
|
|
|
+ firstName: "",
|
|
|
|
+ lastName: "",
|
|
|
|
+ email: "",
|
|
|
|
+ password: "",
|
|
|
|
+ confirmPassword: "",
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+const Auth = () => {
|
|
|
|
+ const classes = useStyles();
|
|
|
|
+ const [showPassword, setShowPassword] = useState(false);
|
|
|
|
+ const [isSignup, setIsSignup] = useState(false);
|
|
|
|
+ const [formData, setFormData] = useState(initialState);
|
|
|
|
+ const dispatch = useDispatch();
|
|
|
|
+ const history = useHistory();
|
|
|
|
+
|
|
|
|
+ const handleSubmit = (e) => {
|
|
|
|
+ e.preventDefault();
|
|
|
|
+ if (isSignup) {
|
|
|
|
+ dispatch(signUp(formData, history))
|
|
|
|
+ }else {
|
|
|
|
+ dispatch(signIn(formData, history))
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ console.log(formData);
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ const handleChange = (e) => {
|
|
|
|
+
|
|
|
|
+ setFormData({ ...formData, [e.target.name]: e.target.value });
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ const handleShowPassword = () => {
|
|
|
|
+ setShowPassword((prevShowPassword) => !prevShowPassword);
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ const switchMode = () => {
|
|
|
|
+ setIsSignup((prevIsSignUp) => !prevIsSignUp);
|
|
|
|
+ setShowPassword(false);
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ const googleSuccess = async (res) => {
|
|
|
|
+ const result = res?.profileObj;
|
|
|
|
+ const token = res?.tokenId;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ dispatch({ type: AUTH, data: { result, token } });
|
|
|
|
+ history.push("/");
|
|
|
|
+ } catch (error) {
|
|
|
|
+ console.log(error);
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+ const googleFailure = () => {
|
|
|
|
+ console.log("failed");
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ return (
|
|
|
|
+ <Container component="main" maxWidth="xs">
|
|
|
|
+ <Paper className={classes.paper} elevation={3}>
|
|
|
|
+ <Avatar className={classes.avatar}>
|
|
|
|
+ <LockOutlinedIcon />
|
|
|
|
+ </Avatar>
|
|
|
|
+ <Typography variant="h5">{isSignup ? "Sign Up" : "Sign In"}</Typography>
|
|
|
|
+ <form className={classes.form} onSubmit={handleSubmit}>
|
|
|
|
+ <Grid container spacing={2}>
|
|
|
|
+ {isSignup && (
|
|
|
|
+ <>
|
|
|
|
+ <Input
|
|
|
|
+ name="firstName"
|
|
|
|
+ label="First Name"
|
|
|
|
+ handleChange={handleChange}
|
|
|
|
+ autoFocus
|
|
|
|
+ half
|
|
|
|
+ />
|
|
|
|
+ <Input
|
|
|
|
+ name="lastName"
|
|
|
|
+ label="Last Name"
|
|
|
|
+ handleChange={handleChange}
|
|
|
|
+ half
|
|
|
|
+ />
|
|
|
|
+ </>
|
|
|
|
+ )}
|
|
|
|
+ <Input
|
|
|
|
+ name="email"
|
|
|
|
+ label="Email Address"
|
|
|
|
+ handleChange={handleChange}
|
|
|
|
+ type="email"
|
|
|
|
+ />
|
|
|
|
+ <Input
|
|
|
|
+ name="password"
|
|
|
|
+ label="Password"
|
|
|
|
+ handleChange={handleChange}
|
|
|
|
+ type={showPassword ? "text" : "password"}
|
|
|
|
+ handleShowPassword={handleShowPassword}
|
|
|
|
+ />
|
|
|
|
+ {isSignup && (
|
|
|
|
+ <Input
|
|
|
|
+ name="confirmPassword"
|
|
|
|
+ label="Repeat Password"
|
|
|
|
+ handleChange={handleChange}
|
|
|
|
+ type="password"
|
|
|
|
+ />
|
|
|
|
+ )}
|
|
|
|
+ </Grid>
|
|
|
|
+ <Button
|
|
|
|
+ type="submit"
|
|
|
|
+ fullWidth
|
|
|
|
+ variant="contained"
|
|
|
|
+ color="primary"
|
|
|
|
+ className={classes.submit}
|
|
|
|
+ >
|
|
|
|
+ {isSignup ? "Sign Up" : "Sign In"}
|
|
|
|
+ </Button>
|
|
|
|
+ <GoogleLogin
|
|
|
|
+ clientId="1095914572545-h16ak2hlqg1v8tadmjc82diueg4t6oir.apps.googleusercontent.com"
|
|
|
|
+ render={(renderProps) => (
|
|
|
|
+ <Button
|
|
|
|
+ className={classes.googleButton}
|
|
|
|
+ color="primary"
|
|
|
|
+ fullWidth
|
|
|
|
+ onClick={renderProps.onClick}
|
|
|
|
+ disabled={renderProps.disabled}
|
|
|
|
+ startIcon={<Icon />}
|
|
|
|
+ variant="contained"
|
|
|
|
+ >
|
|
|
|
+ Google Sign In
|
|
|
|
+ </Button>
|
|
|
|
+ )}
|
|
|
|
+ onSuccess={googleSuccess}
|
|
|
|
+ onFailure={googleFailure}
|
|
|
|
+ cookiePolicy="single_host_origin"
|
|
|
|
+ />
|
|
|
|
+ <Grid container justify="flex-end">
|
|
|
|
+ <Grid item>
|
|
|
|
+ <Button onClick={switchMode}>
|
|
|
|
+ {isSignup
|
|
|
|
+ ? "Already have an account? Sign In"
|
|
|
|
+ : "Dont have an account? Sign Up"}
|
|
|
|
+ </Button>
|
|
|
|
+ </Grid>
|
|
|
|
+ </Grid>
|
|
|
|
+ </form>
|
|
|
|
+ </Paper>
|
|
|
|
+ </Container>
|
|
|
|
+ );
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+export default Auth;
|