|
1 | | - |
| 1 | + |
| 2 | +// gitlab login |
| 3 | + |
| 4 | +window.onload = async () => { |
| 5 | + |
| 6 | + gitToken = getStorage('gitToken') ?? ''; |
| 7 | + |
| 8 | + if (gitToken == 'undefined') { |
| 9 | + gitToken = ''; |
| 10 | + } |
| 11 | + |
| 12 | + |
| 13 | + // decode URL |
| 14 | + linkData = decodeLink(window.location.href); |
| 15 | + |
| 16 | + // clear URL |
| 17 | + window.history.pushState(window.location.origin, 'Codeit', window.location.origin + '/full'); |
| 18 | + |
| 19 | + |
| 20 | + // if treeLoc is in local storage |
| 21 | + if (linkData.dir) { |
| 22 | + |
| 23 | + treeLoc = linkData.dir; |
| 24 | + saveTreeLocLS(treeLoc); |
| 25 | + |
| 26 | + } else { |
| 27 | + |
| 28 | + treeLoc = getStorage('tree') ? getStorage('tree').split(',') : ['', '', '']; |
| 29 | + |
| 30 | + // if repo dosen't have a branch (legacy treeLoc) |
| 31 | + if (treeLoc[1] && !treeLoc[1].includes(':')) { |
| 32 | + |
| 33 | + // add default branch to repo |
| 34 | + treeLoc[1] += ':main'; |
| 35 | + saveTreeLocLS(treeLoc); |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + if (getStorage('loggedUser')) { |
| 43 | + |
| 44 | + loggedUser = getStorage('loggedUser'); |
| 45 | + |
| 46 | + try { |
| 47 | + |
| 48 | + loggedUser = JSON.parse(loggedUser); |
| 49 | + |
| 50 | + // save logged user in local storage |
| 51 | + setStorage('loggedUser', loggedUser.login); |
| 52 | + |
| 53 | + } catch(e) {} |
| 54 | + |
| 55 | + } else { |
| 56 | + |
| 57 | + loggedUser = false; |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + 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'; |
| 63 | + |
| 64 | + loginButton.addEventListener('click', () => { |
| 65 | + |
| 66 | + if (isMobile) { |
| 67 | + |
| 68 | + window.location.href = authURL; |
| 69 | + |
| 70 | + } else { |
| 71 | + |
| 72 | + window.open(authURL, 'Login with Gitlab', 'height=575,width=575'); |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + }) |
| 77 | + |
| 78 | + |
| 79 | + window.addEventListener('message', (event) => { |
| 80 | + |
| 81 | + // if redirected from git auth |
| 82 | + if (event.source.location.pathname === '/git/gitlab/login') { |
| 83 | + |
| 84 | + // hide intro screen |
| 85 | + sidebar.classList.remove('intro'); |
| 86 | + |
| 87 | + // if on safari, refresh header color |
| 88 | + if (isSafari) { |
| 89 | + |
| 90 | + document.querySelector('meta[name="theme-color"]').content = '#313744'; |
| 91 | + |
| 92 | + onNextFrame(() => { |
| 93 | + |
| 94 | + document.querySelector('meta[name="theme-color"]').content = '#1a1c24'; |
| 95 | + |
| 96 | + }); |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + // start loading |
| 101 | + startLoading(); |
| 102 | + |
| 103 | + const gitCode = event.data; |
| 104 | + |
| 105 | + // get git token from Gitlab |
| 106 | + getGitlabToken(gitCode); |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + }) |
| 111 | + |
| 112 | + |
| 113 | + loadLS(); |
| 114 | + |
| 115 | + |
| 116 | + // if git code exists in link |
| 117 | + if (linkData.gitCode) { |
| 118 | + |
| 119 | + // hide intro screen |
| 120 | + sidebar.classList.remove('intro'); |
| 121 | + |
| 122 | + // if on safari, refresh header color |
| 123 | + if (isSafari) { |
| 124 | + |
| 125 | + document.querySelector('meta[name="theme-color"]').content = '#313744'; |
| 126 | + |
| 127 | + onNextFrame(() => { |
| 128 | + |
| 129 | + document.querySelector('meta[name="theme-color"]').content = '#1a1c24'; |
| 130 | + |
| 131 | + }); |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + // start loading |
| 136 | + startLoading(); |
| 137 | + |
| 138 | + const gitCode = linkData.gitCode; |
| 139 | + |
| 140 | + // get git token from Gitlab |
| 141 | + getGitlabToken(gitCode); |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | +} |
| 146 | + |
| 147 | +async function getGitlabToken(gitCode) { |
| 148 | + |
| 149 | + // post to git with clientId, clientSecret and code |
| 150 | + const resp = await axios.post('https://gitlab.com/oauth/token?' + |
| 151 | + 'client_id=f3b94ba233943fa82855c1b495f28c02ccaa11cf276b419d6d1798488a4bb7b0' + |
| 152 | + '&client_secret=1a4098e4770f84f01c94df563201d87b39dae740fe910622c76cd05d8ea30d03' + |
| 153 | + '&grant_type=authorization_code&redirect_uri=https://codeit.codes/git/gitlab/oauth' + |
| 154 | + '&code=' + gitCode); |
| 155 | + |
| 156 | + // save git token to localStorage |
| 157 | + gitToken = resp.access_token; |
| 158 | + saveGitTokenLS(gitToken); |
| 159 | + |
| 160 | + |
| 161 | + // get logged user |
| 162 | + loggedUser = await axios.get('https://api.github.com/user', gitToken); |
| 163 | + loggedUser = loggedUser.username; |
| 164 | + |
| 165 | + // save logged user in local storage |
| 166 | + setStorage('loggedUser', loggedUser); |
| 167 | + |
| 168 | + |
| 169 | + // render sidebar |
| 170 | + renderSidebarHTML(); |
| 171 | + |
| 172 | +} |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | +await axios.post('https://gitlab.com/oauth/token?client_id=&client_secret=&code=&&redirect_uri=https://codeit.codes/git/gitlab/oauth') |
| 179 | + |
0 commit comments