Skip to content

Commit e70a644

Browse files
committed
fix signin & signup
1 parent 69c7faf commit e70a644

File tree

14 files changed

+350
-252
lines changed

14 files changed

+350
-252
lines changed

client/src/App.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Invoices from './components/Invoices/Invoices';
77
import InvoiceDetails from './components/InvoiceDetails/InvoiceDetails'
88
import ClientList from './components/Clients/ClientList'
99
import NavBar from './components/NavBar/NavBar';
10-
import Login from './components/Login/Login'
10+
import Auth from './components/Auth'
1111
import Dashboard from './components/Dashboard/Dashboard';
1212
import Footer from './components/Footer/Footer';
1313
import Header from './components/Header/Header';
@@ -31,7 +31,7 @@ function App() {
3131
<Route path="/edit/invoice/:id" exact component={Invoice} />
3232
<Route path="/invoice/:id" exact component={InvoiceDetails} />
3333
<Route path="/invoices" exact component={Invoices} />
34-
<Route path="/login" exact component={Login} />
34+
<Route path={['/signup', '/login']} exact component={Auth} />
3535
<Route path="/settings" exact component={Settings} />
3636
<Route path="/dashboard" exact component={Dashboard} />
3737
<Route path="/customers" exact component={ClientList} />

client/src/components/Login/Field.js renamed to client/src/components/Auth/Field.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,3 @@
1-
// import React from 'react'
2-
// import styles from './Login.module.css'
3-
4-
// const Field = ({ name, placeholder, type, handleChange }) => {
5-
6-
// return (
7-
// <div>
8-
// <input
9-
// className={styles.inputField}
10-
// type={type}
11-
// name={name}
12-
// placeholder={placeholder}
13-
// onChange={handleChange}
14-
// required = {true}
15-
// />
16-
// </div>
17-
// )
18-
// }
19-
20-
// export default Field
21-
22-
23-
241
import React from 'react';
252
import { TextField, Grid, InputAdornment, IconButton } from '@material-ui/core';
263

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import { Grid, Typography, Avatar, Paper, Button } from "@material-ui/core";
2+
import { useState } from "react";
3+
import Field from "./Field";
4+
import { GoogleLogin } from "react-google-login";
5+
// import ProgressButton from "react-progress-button";
6+
import LockOutlinedIcon from "@material-ui/icons/LockOutlined";
7+
import { Link } from "react-router-dom";
8+
import useStyles from "./styles";
9+
import styles from "./Login.module.css";
10+
import { createProfile } from "../../actions/profile";
11+
import { useDispatch } from "react-redux";
12+
import CircularProgress from "@material-ui/core/CircularProgress";
13+
14+
const initialState = {
15+
firstName: "",
16+
lastName: "",
17+
email: "",
18+
password: "",
19+
confirmPassword: "",
20+
profilePicture: "",
21+
bio: "",
22+
};
23+
24+
const AuthForm = ({ isSignup = false, onSubmit }) => {
25+
const classes = useStyles();
26+
const [formData, setFormData] = useState(initialState);
27+
28+
const handleChange = (e) => {
29+
setFormData({ ...formData, [e.target.name]: e.target.value });
30+
};
31+
32+
const dispatch = useDispatch();
33+
const [isLoading, setIsLoading] = useState(false);
34+
35+
const googleSuccess = async (res) => {
36+
console.log(res);
37+
const result = res?.profileObj;
38+
const token = res?.tokenId;
39+
dispatch(
40+
createProfile({
41+
name: result?.name,
42+
email: result?.email,
43+
userId: result?.googleId,
44+
phoneNumber: "",
45+
businessName: "",
46+
contactAddress: "",
47+
logo: result?.imageUrl,
48+
website: "",
49+
})
50+
);
51+
52+
try {
53+
dispatch({ type: "AUTH", data: { result, token } });
54+
55+
window.location.href = "/dashboard";
56+
} catch (error) {
57+
console.log(error);
58+
}
59+
};
60+
61+
const googleError = (error) => {
62+
console.log(error);
63+
console.log("Google Sign In was unseccassful. Try again later");
64+
};
65+
66+
const handleSubmit = async (e) => {
67+
e.preventDefault();
68+
69+
if (typeof onSubmit === "function") {
70+
try {
71+
setIsLoading(true);
72+
await Promise.resolve(onSubmit(formData));
73+
setIsLoading(false);
74+
} catch (err) {
75+
// handle error
76+
setIsLoading(false);
77+
}
78+
}
79+
};
80+
81+
return (
82+
<Paper className={classes.paper} elevation={2}>
83+
<Avatar className={classes.avatar}>
84+
<LockOutlinedIcon />
85+
</Avatar>
86+
<Typography component="h1" variant="h5">
87+
{isSignup ? "Sign up" : "Sign in"}
88+
</Typography>
89+
<form className={classes.form} onSubmit={handleSubmit}>
90+
<Grid container spacing={2}>
91+
{isSignup && (
92+
<>
93+
<Field
94+
name="firstName"
95+
label="First Name"
96+
handleChange={handleChange}
97+
autoFocus
98+
half
99+
/>
100+
<Field
101+
name="lastName"
102+
label="Last Name"
103+
handleChange={handleChange}
104+
half
105+
/>
106+
</>
107+
)}
108+
<Field
109+
name="email"
110+
label="Email Address"
111+
handleChange={handleChange}
112+
type="email"
113+
/>
114+
<Field
115+
name="password"
116+
label="Password"
117+
handleChange={handleChange}
118+
type={"password"}
119+
/>
120+
{isSignup && (
121+
<Field
122+
name="confirmPassword"
123+
label="Repeat Password"
124+
handleChange={handleChange}
125+
type="password"
126+
/>
127+
)}
128+
</Grid>
129+
<div className={styles.buttons}>
130+
<div>
131+
{isLoading ? (
132+
<CircularProgress />
133+
) : (
134+
<button
135+
disabled={isLoading}
136+
type="submit"
137+
className={styles.submitBtn}
138+
>
139+
{isSignup ? "Sign In" : "Sign Up"}
140+
</button>
141+
)}
142+
</div>
143+
<div>
144+
<GoogleLogin
145+
clientId={process.env.REACT_APP_GOOGLE_CLIENT_ID}
146+
render={(renderProps) => (
147+
<button
148+
className={styles.googleBtn}
149+
onClick={renderProps.onClick}
150+
disabled={renderProps.disabled}
151+
>
152+
Google
153+
</button>
154+
)}
155+
onSuccess={googleSuccess}
156+
onFailure={googleError}
157+
cookiePolicy="single_host_origin"
158+
/>
159+
</div>
160+
</div>
161+
<Grid container justifyContent="center">
162+
<Grid item>
163+
{isSignup ? (
164+
<Link to="/login">Already have an account? Sign in</Link>
165+
) : (
166+
<Link to="/signup">Don't have an account? Sign Up</Link>
167+
)}
168+
</Grid>
169+
</Grid>
170+
<Link to="forgot">
171+
<p
172+
style={{ textAlign: "center", color: "#1d7dd6", marginTop: "20px" }}
173+
>
174+
Forgotten Password?
175+
</p>
176+
</Link>
177+
</form>
178+
</Paper>
179+
);
180+
};
181+
182+
export default AuthForm;

client/src/components/Auth/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from "react";
2+
import { Container } from "@material-ui/core";
3+
import { Switch, Route, useHistory } from "react-router-dom";
4+
import SignupForm from "./signup-form";
5+
import LoginForm from "./login-form";
6+
7+
const Auth = () => {
8+
const history = useHistory();
9+
// eslint-disable-next-line
10+
const user = JSON.parse(localStorage.getItem("profile"));
11+
12+
if (user) {
13+
history.push("/dashboard");
14+
}
15+
16+
return (
17+
<Container component="main" maxWidth="xs">
18+
<Switch>
19+
<Route path="/login" component={LoginForm} />
20+
<Route path="/signup" component={SignupForm} />
21+
</Switch>
22+
</Container>
23+
);
24+
};
25+
26+
export default Auth;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import AuthForm from "./auth-form";
2+
import { useDispatch } from "react-redux";
3+
import { signin } from "../../actions/auth";
4+
5+
const LoginForm = () => {
6+
const dispatch = useDispatch();
7+
8+
const handleSubmit = (formData) => dispatch(signin(formData));
9+
10+
11+
return <AuthForm onSubmit={handleSubmit} />;
12+
};
13+
14+
export default LoginForm;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import AuthForm from "./auth-form";
2+
import { signup } from "../../actions/auth";
3+
import { useDispatch } from "react-redux";
4+
5+
const SignupForm = () => {
6+
const dispatch = useDispatch();
7+
8+
const handleSubmit = (formData) => dispatch(signup(formData))
9+
10+
return <AuthForm isSignup onSubmit={handleSubmit} />;
11+
};
12+
13+
export default SignupForm;

0 commit comments

Comments
 (0)