Skip to content

Commit 1f75ea3

Browse files
Merge pull request #103 from dipscoder/authFlow
Auth flow
2 parents 3a2ac33 + a71d283 commit 1f75ea3

File tree

5 files changed

+149
-23
lines changed

5 files changed

+149
-23
lines changed
5.85 KB
Loading

src/components/Login.js

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import {
88
Divider,
99
Checkbox,
1010
Button,
11-
Grid
11+
Grid,
12+
Avatar
1213
} from '@material-ui/core';
13-
import auth from '../services/authService';
14+
import auth, { signInWithGoogle } from '../services/authService';
1415

1516
const useStyles = makeStyles(theme => ({
1617
root: {
@@ -61,7 +62,7 @@ const useStyles = makeStyles(theme => ({
6162
divider: {
6263
width: '308px',
6364
height: '2px',
64-
marginTop: '45px',
65+
marginTop: '20px',
6566
borderRadius: '25px',
6667
backgroundColor: '#291757'
6768
},
@@ -77,7 +78,7 @@ const useStyles = makeStyles(theme => ({
7778
},
7879
button: {
7980
marginLeft: '246px',
80-
marginTop: '40px',
81+
marginTop: '5px',
8182
width: '100px',
8283
height: '28px',
8384
borderRadius: '20px',
@@ -91,6 +92,31 @@ const useStyles = makeStyles(theme => ({
9192
marginLeft: '215px'
9293
}
9394
},
95+
googleBtn: {
96+
textTransform: 'none',
97+
fontSize: '13px',
98+
display: 'flex',
99+
borderRadius: '20px',
100+
alignItems: 'center',
101+
boxShadow: theme.shadows[3],
102+
backgroundColor: '#291757',
103+
color: theme.palette.primary.contrastText,
104+
transition: 'background-color 0.5s',
105+
padding: '7px 30px',
106+
'&:hover': {
107+
backgroundColor: theme.palette.primary.dark,
108+
transition: 'background-color 0.5s',
109+
cursor: 'pointer'
110+
}
111+
},
112+
avatar: {
113+
width: '20px',
114+
height: '20px'
115+
},
116+
text: {
117+
flexGrow: 1,
118+
textAlign: 'center'
119+
},
94120
cross: {
95121
width: '12px',
96122
height: '12px',
@@ -129,6 +155,12 @@ export default function Login({ handleClose }) {
129155
});
130156
};
131157

158+
const handleSignInWithGoogle = () => {
159+
signInWithGoogle().catch(e => {
160+
console.log(e);
161+
});
162+
};
163+
132164
return (
133165
<Card className={classes.root}>
134166
<Grid container>
@@ -177,6 +209,30 @@ export default function Login({ handleClose }) {
177209
variant="outlined"
178210
onChange={handleChange}
179211
/>
212+
<div
213+
style={{
214+
display: 'flex',
215+
flexDirection: 'column',
216+
alignItems: 'center',
217+
paddingRight: '90px'
218+
}}
219+
>
220+
<Typography style={{ color: '#291757', fontWeight: 'bold' }}>
221+
OR
222+
</Typography>
223+
<Button
224+
className={classes.googleBtn}
225+
onClick={() => handleSignInWithGoogle()}
226+
>
227+
<Avatar
228+
src="../static/images/google-logo.png"
229+
className={classes.avatar}
230+
/>
231+
<Typography component="p" variant="h6" className={classes.text}>
232+
Login with Google
233+
</Typography>
234+
</Button>
235+
</div>
180236
<Divider className={classes.divider} />
181237
<div className={classes.bottomPart}>
182238
<Checkbox className={classes.checkbox} />

src/layouts/DrawerLayout/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
import MenuIcon from '@material-ui/icons/Menu';
2626
import ListItemIcon from '@material-ui/core/ListItemIcon';
2727
import { faBloggerB } from '@fortawesome/free-brands-svg-icons';
28+
import { useSelector } from 'react-redux';
2829
const drawerWidth = 300;
2930

3031
const useStyles = makeStyles(theme => ({
@@ -120,6 +121,7 @@ const useStyles = makeStyles(theme => ({
120121
}));
121122
export default function DrawerLayout({ children }) {
122123
const classes = useStyles();
124+
const user = useSelector(state => state.account.user);
123125
const history = useHistory();
124126
const [toggle, setToggle] = useState(false);
125127
const myStyles = {
@@ -168,11 +170,11 @@ export default function DrawerLayout({ children }) {
168170
<Avatar
169171
className={classes.idavatar}
170172
alt="ProfileIcon"
171-
src="./static/profile/icons/icons.png"
173+
src={user?.photoURL}
172174
/>
173175
<div style={{ display: 'block', marginLeft: '10px' }}>
174176
<Typography style={{ fontSize: '18px', fontWeight: '600' }}>
175-
John Doe
177+
{user?.displayName}
176178
</Typography>
177179
<div style={{ display: 'flex' }}>
178180
<Typography variant="subtitle1" className={classes.idtext}>

src/services/authService.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import jwtDecode from 'jwt-decode';
22
import axios from 'src/utils/axios';
33

44
import firebase from 'firebase';
5+
require('firebase/auth');
56

67
class AuthService {
78
// Configure Firebase.
@@ -115,3 +116,9 @@ const authService = new AuthService();
115116

116117
export default authService;
117118
export { firebase };
119+
120+
export const signInWithGoogle = () => {
121+
const provider = new firebase.auth.GoogleAuthProvider();
122+
provider.setCustomParameters({ prompt: 'select_account' });
123+
return firebase.auth().signInWithPopup(provider);
124+
};

src/views/pages/HomeView/Hero.js

Lines changed: 78 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useState, useEffect } from 'react';
22
import PropTypes from 'prop-types';
33
import clsx from 'clsx';
44

@@ -9,8 +9,14 @@ import {
99
Grid,
1010
Hidden,
1111
Typography,
12-
makeStyles
12+
makeStyles,
13+
Dialog
1314
} from '@material-ui/core';
15+
import { useDispatch, useSelector } from 'react-redux';
16+
import { useHistory } from 'react-router';
17+
import { firebase } from 'src/services/authService';
18+
import Login from '../../../components/Login';
19+
import { login, dismissLogin } from 'src/actions/accountActions';
1420

1521
const useStyles = makeStyles(theme => ({
1622
root: {
@@ -64,8 +70,61 @@ const useStyles = makeStyles(theme => ({
6470

6571
function Hero({ className, ...rest }) {
6672
const classes = useStyles();
73+
const user = useSelector(state => state.account.user);
74+
const loginFlag = useSelector(state => state.account.login);
75+
const [text, setText] = useState('');
76+
const history = useHistory();
77+
const dispatch = useDispatch();
78+
79+
useEffect(() => {
80+
if (user === null || user === undefined) {
81+
setText('Login and Create Profile');
82+
return;
83+
}
84+
// setText("Go to Dashboard")
85+
const userId = user.uid;
86+
const db = firebase.firestore();
87+
88+
db.collection('users')
89+
.doc(userId)
90+
.get()
91+
.then(doc => {
92+
if (doc.exists) {
93+
setText('Go To Dashboard');
94+
} else {
95+
setText('Register');
96+
}
97+
});
98+
}, [user]);
99+
100+
const handleClick = () => {
101+
if (user !== null && user !== undefined) {
102+
if (text === 'Go To Dashboard') {
103+
history.push('/dashboard');
104+
}
105+
if (text === 'Register') {
106+
history.push('/register');
107+
}
108+
} else {
109+
dispatch(login());
110+
}
111+
};
112+
113+
const handleClose = () => {
114+
dispatch(dismissLogin());
115+
};
116+
67117
return (
68118
<div className={clsx(classes.root, className)} {...rest}>
119+
<Dialog
120+
maxWidth
121+
open={!user && !!loginFlag}
122+
onClose={handleClose}
123+
aria-labelledby="form-dialog-title"
124+
className={classes.dialog}
125+
>
126+
<Login handleClose={handleClose} />
127+
</Dialog>
69128
<Container
70129
maxWidth="lg"
71130
style={{ padding: '40px 0px 0px', position: 'relative' }}
@@ -143,21 +202,23 @@ function Hero({ className, ...rest }) {
143202
<Grid item xs={12} md={12}>
144203
<Typography variant="h1" color="secondary" />
145204
<Box>
146-
<Button
147-
style={{
148-
backgroundColor: '#ffffff',
149-
color: '#B20000',
150-
textTransform: 'capitalize',
151-
fontWeight: 700,
152-
borderRadius: '20px'
153-
}}
154-
component="a"
155-
href="/register"
156-
size="large"
157-
variant="contained"
158-
>
159-
Login and Create Profile
160-
</Button>
205+
{text.length > 0 && (
206+
<Button
207+
style={{
208+
backgroundColor: '#ffffff',
209+
color: '#B20000',
210+
textTransform: 'capitalize',
211+
fontWeight: 700,
212+
borderRadius: '20px'
213+
}}
214+
component="a"
215+
onClick={handleClick}
216+
size="large"
217+
variant="contained"
218+
>
219+
{text}
220+
</Button>
221+
)}
161222
</Box>
162223
</Grid>
163224
</Grid>

0 commit comments

Comments
 (0)