-
-
Notifications
You must be signed in to change notification settings - Fork 437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix signin & signup #9
Open
tobyleye
wants to merge
2
commits into
panshak:master
Choose a base branch
from
tobyleye:fix-auth
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React, { useState } from "react"; | ||
import { TextField, Grid, InputAdornment, IconButton } from "@material-ui/core"; | ||
|
||
import Visibility from "@material-ui/icons/Visibility"; | ||
import VisibilityOff from "@material-ui/icons/VisibilityOff"; | ||
|
||
const Field = ({ | ||
name, | ||
handleChange, | ||
label, | ||
half, | ||
autoFocus, | ||
type, | ||
placeholder, | ||
}) => { | ||
const [showPass, setShowPass] = useState(false); | ||
|
||
const togglePasswordVisibility = () => { | ||
setShowPass((show) => !show); | ||
}; | ||
|
||
return ( | ||
<Grid item xs={12} sm={half ? 6 : 12}> | ||
<TextField | ||
name={name} | ||
onChange={handleChange} | ||
placeholder={placeholder} | ||
variant="outlined" | ||
required | ||
fullWidth | ||
label={label} | ||
autoFocus={autoFocus} | ||
type={showPass ? "text" : type} | ||
InputProps={ | ||
name === "password" | ||
? { | ||
endAdornment: ( | ||
<InputAdornment position="end"> | ||
<IconButton onClick={togglePasswordVisibility}> | ||
{!showPass ? <Visibility /> : <VisibilityOff />} | ||
</IconButton> | ||
</InputAdornment> | ||
), | ||
} | ||
: null | ||
} | ||
/> | ||
</Grid> | ||
); | ||
}; | ||
|
||
export default Field; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,182 @@ | ||||||
import { Grid, Typography, Avatar, Paper, Button } from "@material-ui/core"; | ||||||
import { useState } from "react"; | ||||||
import Field from "./Field"; | ||||||
import { GoogleLogin } from "react-google-login"; | ||||||
// import ProgressButton from "react-progress-button"; | ||||||
import LockOutlinedIcon from "@material-ui/icons/LockOutlined"; | ||||||
import { Link } from "react-router-dom"; | ||||||
import useStyles from "./styles"; | ||||||
import styles from "./Login.module.css"; | ||||||
import { createProfile } from "../../actions/profile"; | ||||||
import { useDispatch } from "react-redux"; | ||||||
import CircularProgress from "@material-ui/core/CircularProgress"; | ||||||
|
||||||
const initialState = { | ||||||
firstName: "", | ||||||
lastName: "", | ||||||
email: "", | ||||||
password: "", | ||||||
confirmPassword: "", | ||||||
profilePicture: "", | ||||||
bio: "", | ||||||
}; | ||||||
|
||||||
const AuthForm = ({ isSignup = false, onSubmit }) => { | ||||||
const classes = useStyles(); | ||||||
const [formData, setFormData] = useState(initialState); | ||||||
|
||||||
const handleChange = (e) => { | ||||||
setFormData({ ...formData, [e.target.name]: e.target.value }); | ||||||
}; | ||||||
|
||||||
const dispatch = useDispatch(); | ||||||
const [isLoading, setIsLoading] = useState(false); | ||||||
|
||||||
const googleSuccess = async (res) => { | ||||||
console.log(res); | ||||||
const result = res?.profileObj; | ||||||
const token = res?.tokenId; | ||||||
dispatch( | ||||||
createProfile({ | ||||||
name: result?.name, | ||||||
email: result?.email, | ||||||
userId: result?.googleId, | ||||||
phoneNumber: "", | ||||||
businessName: "", | ||||||
contactAddress: "", | ||||||
logo: result?.imageUrl, | ||||||
website: "", | ||||||
}) | ||||||
); | ||||||
|
||||||
try { | ||||||
dispatch({ type: "AUTH", data: { result, token } }); | ||||||
|
||||||
window.location.href = "/dashboard"; | ||||||
} catch (error) { | ||||||
console.log(error); | ||||||
} | ||||||
}; | ||||||
|
||||||
const googleError = (error) => { | ||||||
console.log(error); | ||||||
console.log("Google Sign In was unseccassful. Try again later"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
}; | ||||||
|
||||||
const handleSubmit = async (e) => { | ||||||
e.preventDefault(); | ||||||
|
||||||
if (typeof onSubmit === "function") { | ||||||
try { | ||||||
setIsLoading(true); | ||||||
await Promise.resolve(onSubmit(formData)); | ||||||
setIsLoading(false); | ||||||
} catch (err) { | ||||||
// handle error | ||||||
setIsLoading(false); | ||||||
} | ||||||
} | ||||||
}; | ||||||
|
||||||
return ( | ||||||
<Paper className={classes.paper} elevation={2}> | ||||||
<Avatar className={classes.avatar}> | ||||||
<LockOutlinedIcon /> | ||||||
</Avatar> | ||||||
<Typography component="h1" variant="h5"> | ||||||
{isSignup ? "Sign up" : "Sign in"} | ||||||
</Typography> | ||||||
<form className={classes.form} onSubmit={handleSubmit}> | ||||||
<Grid container spacing={2}> | ||||||
{isSignup && ( | ||||||
<> | ||||||
<Field | ||||||
name="firstName" | ||||||
label="First Name" | ||||||
handleChange={handleChange} | ||||||
autoFocus | ||||||
half | ||||||
/> | ||||||
<Field | ||||||
name="lastName" | ||||||
label="Last Name" | ||||||
handleChange={handleChange} | ||||||
half | ||||||
/> | ||||||
</> | ||||||
)} | ||||||
<Field | ||||||
name="email" | ||||||
label="Email Address" | ||||||
handleChange={handleChange} | ||||||
type="email" | ||||||
/> | ||||||
<Field | ||||||
name="password" | ||||||
label="Password" | ||||||
handleChange={handleChange} | ||||||
type={"password"} | ||||||
/> | ||||||
{isSignup && ( | ||||||
<Field | ||||||
name="confirmPassword" | ||||||
label="Repeat Password" | ||||||
handleChange={handleChange} | ||||||
type="password" | ||||||
/> | ||||||
)} | ||||||
</Grid> | ||||||
<div className={styles.buttons}> | ||||||
<div> | ||||||
{isLoading ? ( | ||||||
<CircularProgress /> | ||||||
) : ( | ||||||
<button | ||||||
disabled={isLoading} | ||||||
type="submit" | ||||||
className={styles.submitBtn} | ||||||
> | ||||||
{isSignup ? "Sign Up" : "Sign In"} | ||||||
</button> | ||||||
)} | ||||||
</div> | ||||||
<div> | ||||||
<GoogleLogin | ||||||
clientId={process.env.REACT_APP_GOOGLE_CLIENT_ID} | ||||||
render={(renderProps) => ( | ||||||
<button | ||||||
className={styles.googleBtn} | ||||||
onClick={renderProps.onClick} | ||||||
disabled={renderProps.disabled} | ||||||
> | ||||||
</button> | ||||||
)} | ||||||
onSuccess={googleSuccess} | ||||||
onFailure={googleError} | ||||||
cookiePolicy="single_host_origin" | ||||||
/> | ||||||
</div> | ||||||
</div> | ||||||
<Grid container justifyContent="center"> | ||||||
<Grid item> | ||||||
{isSignup ? ( | ||||||
<Link to="/login">Already have an account? Sign in</Link> | ||||||
) : ( | ||||||
<Link to="/signup">Don't have an account? Sign Up</Link> | ||||||
)} | ||||||
</Grid> | ||||||
</Grid> | ||||||
<Link to="forgot"> | ||||||
<p | ||||||
style={{ textAlign: "center", color: "#1d7dd6", marginTop: "20px" }} | ||||||
> | ||||||
Forgotten Password? | ||||||
</p> | ||||||
</Link> | ||||||
</form> | ||||||
</Paper> | ||||||
); | ||||||
}; | ||||||
|
||||||
export default AuthForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React from "react"; | ||
import { Container } from "@material-ui/core"; | ||
import { Switch, Route, useHistory } from "react-router-dom"; | ||
import AuthForm from "./auth-form"; | ||
import { useDispatch } from "react-redux"; | ||
import { signin, signup } from "../../actions/auth"; | ||
|
||
const Auth = () => { | ||
const history = useHistory(); | ||
// eslint-disable-next-line | ||
const user = JSON.parse(localStorage.getItem("profile")); | ||
|
||
if (user) { | ||
history.push("/dashboard"); | ||
} | ||
|
||
const dispatch = useDispatch(); | ||
|
||
return ( | ||
<Container component="main" maxWidth="xs"> | ||
<Switch> | ||
<Route path="/login"> | ||
<AuthForm onSubmit={() => dispatch(signin())} /> | ||
</Route> | ||
<Route path="/signup"> | ||
<AuthForm isSignup onSubmit={() => dispatch(signup())} /> | ||
</Route> | ||
</Switch> | ||
</Container> | ||
); | ||
}; | ||
|
||
export default Auth; |
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is best if we dont leave console.logs in our code.