Skip to content

Commit 5eba54f

Browse files
author
Jeff Ma
committed
Add logout functionality
1 parent 4bf2bed commit 5eba54f

6 files changed

Lines changed: 56 additions & 30 deletions

File tree

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"prebuild": "yarn build:frontend && node ./src/backend/scripts/generate-gcp-key.js",
2929
"build": "cross-env NODE_ENV=production babel src/backend --out-dir lib --copy-files",
3030
"start": "cross-env NODE_ENV=production babel-node lib/index.js",
31-
"deploy": "git push heroku main",
31+
"deploy": "fly deploy",
3232
"migrate": "knex migrate:latest --knexfile src/backend/knexfile.js",
3333
"seed:dev": "cross-env NODE_ENV=development knex seed:run --knexfile src/backend/knexfile.js",
3434
"seed": "knex seed:run --knexfile src/backend/knexfile.js",
@@ -85,8 +85,7 @@
8585
"react-scripts": "3.4.3",
8686
"redux": "^4.0.5",
8787
"styled-components": "^5.2.1",
88-
"typeface-ibm-plex-sans": "^1.1.13",
89-
"redux-devtools-extension": "^2.13.8"
88+
"typeface-ibm-plex-sans": "^1.1.13"
9089
},
9190
"browserslist": {
9291
"production": [
@@ -117,6 +116,7 @@
117116
"eslint-plugin-jsx-a11y": "^6.3.1",
118117
"eslint-plugin-prettier": "^3.1.4",
119118
"esm": "^3.2.25",
120-
"mocha": "^8.2.0"
119+
"mocha": "^8.2.0",
120+
"redux-devtools-extension": "^2.13.8"
121121
}
122122
}

src/backend/scripts/generate-gcp-key.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Load the GCP Key file on keroku
1+
// Load the GCP Key file on our production server
22
// The file is not stored in git,
3-
// so heroku will need to generate it from .env
3+
// so the production server will need to generate it from .env
44

55
require('dotenv').config()
66
const fs = require('fs')

src/frontend/components/TopBar/ProfileDropdown.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react';
2-
import { useHistory } from 'react-router-dom';
32
import styled from 'styled-components';
43
import MUIGrid from '@mui/material/Grid';
54
import MUITypography from '@mui/material/Typography';
@@ -8,6 +7,7 @@ import ClickAwayListener from '@mui/material/ClickAwayListener';
87
const Typography = styled(MUITypography)`
98
font: ${({ theme }) => theme.fonts.bold14};
109
text-align: center;
10+
cursor: pointer;
1111
`;
1212

1313
const Grid = styled(MUIGrid)``;
@@ -38,21 +38,17 @@ const Container = styled.div`
3838
}
3939
`;
4040

41-
const ProfileDropdown = ({ className, onClose }) => {
42-
const history = useHistory();
43-
44-
return (
45-
<ClickAwayListener onClickAway={onClose}>
46-
<Container className={className}>
47-
<Arrow />
48-
<MainBox>
49-
<Grid xs={20}>
50-
<Typography>Sign Out</Typography>
51-
</Grid>
52-
</MainBox>
53-
</Container>
54-
</ClickAwayListener>
55-
);
56-
};
41+
const ProfileDropdown = ({ className, onClose, onLogout }) => (
42+
<ClickAwayListener onClickAway={onClose}>
43+
<Container className={className}>
44+
<Arrow />
45+
<MainBox>
46+
<Grid xs={20}>
47+
<Typography onClick={onLogout}>Sign Out</Typography>
48+
</Grid>
49+
</MainBox>
50+
</Container>
51+
</ClickAwayListener>
52+
);
5753

5854
export default ProfileDropdown;

src/frontend/components/TopBar/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import { useHistory } from 'react-router-dom';
23
import styled from 'styled-components';
34
import useProfilePicture from '../../hooks/profile-picture';
45
import { Link } from 'react-router-dom';
@@ -10,6 +11,7 @@ import WaterloopLogoSVG from './assets/topbar-logo.svg';
1011
import UnstyledDesktopMenu from './DesktopMenu';
1112
import UnstyledProfileDropdown from './ProfileDropdown';
1213
import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown';
14+
import useGoogleAuth from '../../hooks/google-auth';
1315

1416
const AppBar = styled(MUIAppBar)`
1517
background-color: ${({ theme }) => theme.colours.blues.blue1};
@@ -56,8 +58,15 @@ const ProfilePicture = styled.img`
5658
const TopBar = () => {
5759
const [menuOpen, setMenuOpen] = React.useState(false);
5860
const [dropdownOpen, setDropdownOpen] = React.useState(false);
61+
const { signOut } = useGoogleAuth();
62+
const history = useHistory();
5963
const { profilePicture } = useProfilePicture();
6064

65+
const onLogout = () => {
66+
signOut();
67+
history.push('/sign-in');
68+
};
69+
6170
return (
6271
<div>
6372
<AppBar position="relative">
@@ -86,7 +95,10 @@ const TopBar = () => {
8695
</AppBar>
8796
{menuOpen && <DesktopMenu onClose={() => setMenuOpen(false)} />}
8897
{dropdownOpen && (
89-
<ProfileDropdown onClose={() => setDropdownOpen(false)} />
98+
<ProfileDropdown
99+
onClose={() => setDropdownOpen(false)}
100+
onLogout={onLogout}
101+
/>
90102
)}
91103
</div>
92104
);

src/frontend/hooks/google-auth.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import { useCallback } from 'react';
2-
import { useGoogleLogin } from 'react-google-login';
2+
import { useGoogleLogin, useGoogleLogout } from 'react-google-login';
33
import * as userActions from '../state/user/actions';
44
import { useDispatch } from 'react-redux';
55
import api from '../api';
6+
import Cookies from 'js-cookie';
7+
8+
const scopes = [
9+
'profile',
10+
'email',
11+
'https://www.googleapis.com/auth/admin.directory.group.readonly',
12+
];
613

714
const useGoogleAuth = (onAuthComplete) => {
815
const dispatch = useDispatch();
@@ -36,15 +43,26 @@ const useGoogleAuth = (onAuthComplete) => {
3643
onFailure: (err) => {
3744
console.log('failed auth', err);
3845
},
39-
clientId:
40-
'538509890740-e3dai2feq6knjfdspqde5ogt2kme0chm.apps.googleusercontent.com',
41-
scope:
42-
'profile email https://www.googleapis.com/auth/admin.directory.group.readonly',
46+
clientId: process.env.REACT_APP_GOOGLE_CLIENT_ID,
47+
scope: scopes.join(' '),
4348
prompt: 'consent',
4449
});
4550

51+
const { signOut } = useGoogleLogout({
52+
clientId: process.env.REACT_APP_GOOGLE_CLIENT_ID,
53+
onLogoutSuccess: () => {
54+
// removeAllCookies();
55+
Cookies.remove('tokenId');
56+
console.log('successful logout');
57+
},
58+
onFailure: () => {
59+
console.error('Failed to logout!');
60+
},
61+
});
62+
4663
return {
4764
signIn,
65+
signOut,
4866
};
4967
};
5068

src/frontend/pages/sign-in/SignInPage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const SignInPage = () => {
104104
Cookies.set('tokenId', tokenId, { expires: 1 });
105105
addAuthTokenToRequests(tokenId);
106106
console.log('Auth Complete');
107-
107+
// TODO: store accessToken in browser storage.
108108
api.google
109109
.updateUserGroups(userId, groupIds, accessToken)
110110
.then((resp) => {

0 commit comments

Comments
 (0)