-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathindex.js
48 lines (37 loc) · 1.32 KB
/
index.js
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
/* eslint-disable no-shadow */
import React, { useEffect, useCallback, useContext } from 'react';
import { useHistory, useLocation } from 'react-router-dom';
import { AuthContext } from '../../contexts/AuthContext';
import api from '../../services/api';
import { login } from '../../services/authentication';
import Logo from '../../components/Logo';
import * as S from './styled';
export default function CallbackPage() {
const history = useHistory();
const { search } = useLocation();
const searchParam = new URLSearchParams(search);
const code = searchParam.get('code');
const { setUser } = useContext(AuthContext);
const authenticateUserWithGithub = useCallback(
async (code) => {
const response = await api.get(
`http://localhost:3333/auth/${code}`
);
if (response.status === 200 || response.status === 201) {
setUser(response.data.user);
login(response.data.token);
history.push('/dashboard');
}
},
[history, setUser]
);
useEffect(() => {
authenticateUserWithGithub(code);
}, [code, authenticateUserWithGithub]);
return (
<S.Container>
<Logo />
<h2>Aguardando as informações...</h2>
</S.Container>
);
}