-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathgitlab-auth.js
More file actions
168 lines (94 loc) · 3.52 KB
/
gitlab-auth.js
File metadata and controls
168 lines (94 loc) · 3.52 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// gitlab login
window.addEventListener('load', async () => {
gitToken = getStorage('gitToken') ?? '';
if (gitToken == 'undefined') {
gitToken = '';
}
// decode URL
linkData = decodeLink(window.location.href);
// clear URL
window.history.pushState(window.location.origin, 'Codeit', window.location.origin + '/full');
// if treeLoc is in local storage
if (linkData.dir) {
treeLoc = linkData.dir;
saveTreeLocLS(treeLoc);
} else {
treeLoc = getStorage('tree') ? getStorage('tree').split(',') : ['', '', ''];
// if repo dosen't have a branch (legacy treeLoc)
if (treeLoc[1] && !treeLoc[1].includes(':')) {
// add default branch to repo
treeLoc[1] += ':main';
saveTreeLocLS(treeLoc);
}
}
if (getStorage('loggedUser')) {
loggedUser = getStorage('loggedUser');
try {
loggedUser = JSON.parse(loggedUser);
// save logged user in local storage
setStorage('loggedUser', loggedUser.login);
} catch(e) {}
} else {
loggedUser = false;
}
const authURL = 'https://gitlab.com/oauth/authorize?client_id=f3b94ba233943fa82855c1b495f28c02ccaa11cf276b419d6d1798488a4bb7b0&redirect_uri=https://codeit.codes/git/gitlab/oauth&response_type=code&state=1f3b3477&scope=api';
loginButton.addEventListener('click', () => {
if (isMobile) {
window.location.href = authURL;
} else {
window.open(authURL, 'Login with Gitlab', 'height=575,width=575');
}
})
window.addEventListener('message', (event) => {
// hide intro screen
sidebar.classList.remove('intro');
// if on safari, refresh header color
if (isSafari) {
document.querySelector('meta[name="theme-color"]').content = '#313744';
onNextFrame(() => {
document.querySelector('meta[name="theme-color"]').content = '#1a1c24';
});
}
// start loading
startLoading();
const gitCode = event.data;
// get git token from Gitlab
getGitlabToken(gitCode);
})
loadLS();
// if git code exists in link
if (linkData.gitCode) {
// hide intro screen
sidebar.classList.remove('intro');
// if on safari, refresh header color
if (isSafari) {
document.querySelector('meta[name="theme-color"]').content = '#313744';
onNextFrame(() => {
document.querySelector('meta[name="theme-color"]').content = '#1a1c24';
});
}
// start loading
startLoading();
const gitCode = linkData.gitCode;
// get git token from Gitlab
getGitlabToken(gitCode);
}
});
async function getGitlabToken(gitCode) {
// post to git with clientId, clientSecret and code
const resp = await axios.post('https://gitlab.com/oauth/token?' +
'client_id=f3b94ba233943fa82855c1b495f28c02ccaa11cf276b419d6d1798488a4bb7b0' +
'&client_secret=1a4098e4770f84f01c94df563201d87b39dae740fe910622c76cd05d8ea30d03' +
'&grant_type=authorization_code&redirect_uri=https://codeit.codes/git/gitlab/oauth' +
'&code=' + gitCode);
// save git token to localStorage
gitToken = resp.access_token;
saveGitTokenLS(gitToken);
// get logged user
loggedUser = await axios.get('https://gitlab.com/api/v4/user', gitToken);
loggedUser = loggedUser.username;
// save logged user in local storage
setStorage('loggedUser', loggedUser);
// render sidebar
renderSidebarHTML();
}