-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.tsx
80 lines (71 loc) · 2.1 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// BackButton Component
/**
* Component that handles user login / logout via google sign-in
* @packageDocumentation
*/
import React, { useState, useContext } from 'react';
import { GoogleLogout, GoogleLogin } from 'react-google-login';
import styled from 'styled-components';
import config from '../../config/ConfigLoader';
import { AuthContext } from '../../contexts/AuthContext';
const CustomGoogleLogin = styled(GoogleLogin)`
width: 100px;
height: 56px;
margin-top: auto;
margin-bottom: auto;
margin-right: 5px;
`;
const CustomGoogleLogout = styled(GoogleLogout)`
width: 100px;
height: 56px;
margin-top: auto;
margin-bottom: auto;
margin-right: 5px;
`;
/** GoogleButton State */
interface State {
/** True if user is logged in*/
isLoggedIn: boolean;
}
/*
Button that handles google login and logout
*/
export const GoogleButton: React.FC = () => {
const authContext = useContext(AuthContext);
const [isLoggedIn, setIsLoggedIn] = useState<State['isLoggedIn']>(false);
const logIn = (response) => {
authContext.setTokenId(response.tokenObj.id_token);
setIsLoggedIn(true);
};
const logOut = () => {
authContext.setTokenId('');
setIsLoggedIn(false);
};
const handleError = () => {};
return (
<>
{!isLoggedIn && (
<CustomGoogleLogin
clientId={config.clientId}
buttonText="Login"
onSuccess={logIn}
onFailure={handleError}
responseType="id_token"
cookiePolicy={'single_host_origin'}
isSignedIn={true}
/>
)}
{isLoggedIn && (
<CustomGoogleLogout
clientId={config.clientId}
buttonText="Logout"
onLogoutSuccess={logOut}
onFailure={handleError}
cookiePolicy={'single_host_origin'}
isSignedIn={true}
/>
)}
</>
);
};
export default GoogleButton;