/
TwitterLogin.tsx
51 lines (47 loc) · 1.43 KB
/
TwitterLogin.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
import React, { useEffect } from 'react';
import { useSelector } from 'react-redux';
import {
setLoggedIn,
setAccessToken,
setTokenExpiryDate,
selectIsLoggedIn,
selectTokenExpiryDate,
} from '../auth/authSlice';
import { setUserProfileAsync } from './twitterSlice';
import { getTwitterOauthUrl } from '../../utils/twitterOauthConfig';
import { getOAuthParams, removeHashParamsFromUrl } from '../../utils/hashUtils';
import { useAppDispatch } from '../../app/hooks';
import { Button, Text, VStack } from '@chakra-ui/react';
const { access_token, expires_in } = getOAuthParams();
removeHashParamsFromUrl();
export function TwitterLogin() {
const isLoggedIn = useSelector(selectIsLoggedIn);
const tokenExpiryDate = useSelector(selectTokenExpiryDate);
const dispatch = useAppDispatch();
useEffect(() => {
if (access_token) {
dispatch(setLoggedIn(true));
dispatch(setAccessToken(access_token));
dispatch(setTokenExpiryDate(Number(expires_in)));
dispatch(setUserProfileAsync(access_token));
}
}, []);
return (
<VStack spacing="4">
{!isLoggedIn &&
<Button
colorScheme='blue'
aria-label="Log in using OAuth 2.0"
onClick={() => window.open(getTwitterOauthUrl(), '_self')}
>
Log in with Twitter
</Button>
}
{isLoggedIn &&
<Text>
Token expiry date: {tokenExpiryDate}
</Text>
}
</VStack>
);
}