-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubLogin.js
More file actions
78 lines (72 loc) · 2.35 KB
/
GitHubLogin.js
File metadata and controls
78 lines (72 loc) · 2.35 KB
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
import axios from "axios";
import { useState } from "react";
export default function GitHubLogin() {
const REST_API_KEY = "497a08df8fa6b9f4dc7f";
const REDIRECT_URI = "http://localhost:3000/callback";
const AUTH_URL = `https://github.com/login/oauth/authorize?client_id=${REST_API_KEY}&redirect_uri=${REDIRECT_URI}`;
const url = new URL(window.location.href);
const authorizationCode = url.searchParams.get("code");
let local = localStorage.getItem("token");
let [userName, setUserName] = useState();
let [avatar, setAvatar] = useState();
const limitToken = "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";
const getAccessToken = async () => {
const baseUrl = "https://github.com/login/oauth/access_token";
const config = {
client_id: "497a08df8fa6b9f4dc7f",
client_secret: "4e596b7fd2f4825e0523d5fea7de246b4fa59cd1",
code: authorizationCode,
};
const params = new URLSearchParams(config).toString();
const finalUrl = `${baseUrl}?${params}`;
const tokenRequest = await (
await fetch(finalUrl, {
method: "POST",
headers: {
Accept: "application/json",
},
})
).json();
localStorage.setItem("token", tokenRequest.access_token);
// getGitHubUserInfo();
return tokenRequest;
};
const getGitHubUserInfo = async () => {
const response = await axios.get("https://api.github.com/user", {
headers: {
authorization: `token ${local}`, // 토큰 명시 필수!!!
},
});
return response;
};
return (
<div className="gitHubLogin">
<span>기여자 등록: </span>
<a
href={AUTH_URL}
onClick={async () => {
getAccessToken().then((response) => {
console.log(response);
});
// getGitHubUserInfo().then((response) => {
// setUserName(response.data.name);
// setAvatar(response.data.avatar_url);
// console.log(userName);
// console.log(avatar);
// });
}}
>
<button id="gitHubLoginBtn">GitHub 로그인</button>
<div>유저이름 : {userName}</div>
<div>
프로필사진 :
{!avatar ? (
""
) : (
<img style={{ width: "50px" }} className="phoneImage" alt="iPhone_01" src={avatar} />
)}
</div>
</a>
</div>
);
}