-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetGithubTokenAsync.js
More file actions
114 lines (101 loc) · 3.17 KB
/
getGithubTokenAsync.js
File metadata and controls
114 lines (101 loc) · 3.17 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import * as AuthSession from 'expo-auth-session';
import {AsyncStorage } from '@react-native-async-storage/async-storage';
// Make an app https://github.com/settings/applications/new
// Firebase Docs: https://firebase.google.com/docs/auth/web/github-auth
// The github auth callback should be something like: https://auth.expo.io/@bacon/github
const REDIRECT_URL = AuthSession.getRedirectUrl(); //AuthSession.makeRedirectUri();
const GithubStorageKey = '@Expo:GithubToken';
// Add your API stuff here...
const github = {
id: '9a007c4810bc6d66bb27',
secret: 'f0857862e71cca9228195f82d84f3e99bec6ca37',
};
const githubFields = [
'user',
'public_repo',
'repo',
'repo_deployment',
'repo:status',
'read:repo_hook',
'read:org',
'read:public_key',
'read:gpg_key',
];
function authUrlWithId(id, fields) {
return (
`https://github.com/login/oauth/authorize` +
`?client_id=${id}` +
`&redirect_uri=${encodeURIComponent(REDIRECT_URL)}` +
`&scope=${encodeURIComponent(fields.join(' '))}`
);
}
async function createTokenWithCode(code) {
const url =
`https://github.com/login/oauth/access_token` +
`?client_id=${github.id}` +
`&client_secret=${github.secret}` +
`&code=${code}`;
const res = await fetch(url, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
});
return res.json();
}
async function teste() {
let token = await AsyncStorage.getItem(GithubStorageKey);
const url =
`https://api.github.com/user`;
const res = await fetch(url, {
method: 'get',
headers: {
Accept: 'application/json',
'Authorization': 'token '+ token,
},
});
return res.json();
}
async function getGithubTokenAsync() {
try {
const { type, params } = await AuthSession.startAsync({
authUrl: authUrlWithId(github.id, githubFields),
});
console.log('getGithubTokenAsync: A: ', { type, params });
if (type !== 'success') {
// type === 'cancel' = if you cancel out of the modal
// type === 'error' = if you click "no" on the redirect page
return null;
}
// this is different to `type === 'error'`
if (params.error) {
const { error, error_description, error_uri } = params;
/*
If you didn't set the URI to match `REDIRECT_URL` in `https://github.com/settings/applications/...`
error: "redirect_uri_mismatch",
error_description: "The redirect_uri MUST match the registered callback URL for this application.",
*/
if (error === 'redirect_uri_mismatch') {
console.warn(
`Please set the "Authorization callback URL" in your Github application settings to ${REDIRECT_URL}`
);
}
throw new Error(`Github Auth: ${error} ${error_description}`);
}
const { token_type, scope, access_token } = await createTokenWithCode(
params.code
);
// { token_type, scope, access_token }
console.log('getGithubTokenAsync: B: ', {
token_type,
scope,
access_token,
});
console.log(teste(access_token))
return access_token;
} catch ({ message }) {
throw new Error(`Github Auth: ${message}`);
}
}
export default getGithubTokenAsync;