| 39 | } |
| 40 | |
| 41 | async function getRefreshToken() { |
| 42 | const code = utils.urlParam(window.location.href, 'code'); |
| 43 | const state = utils.urlParam(window.location.href, 'state'); |
| 44 | window.history.replaceState('', '', '/mal/oauth'); |
| 45 | if (!state || !code) throw 'Url wrong'; |
| 46 | const challenge = sessionStorage.getItem(state); |
| 47 | if (!challenge) throw 'No challenge found'; |
| 48 | return api.request |
| 49 | .xhr('POST', { |
| 50 | url: 'https://myanimelist.net/v1/oauth2/token', |
| 51 | headers: { |
| 52 | 'Content-Type': 'application/x-www-form-urlencoded', |
| 53 | }, |
| 54 | data: `&client_id=${clientId}&grant_type=authorization_code&code=${code}&code_verifier=${challenge}`, |
| 55 | }) |
| 56 | .then(res => JSON.parse(res.responseText)) |
| 57 | .then(json => { |
| 58 | if (json && json.refresh_token && json.access_token) { |
| 59 | api.settings.set('malToken', json.access_token); |
| 60 | api.settings.set('malRefresh', json.refresh_token); |
| 61 | $('.card-text.succ').prepend(j.html(api.storage.lang('anilistClass_authentication'))); |
| 62 | $('body').removeClass(); |
| 63 | $('body').addClass('success'); |
| 64 | return; |
| 65 | } |
| 66 | if (json && json.error) throw json.error; |
| 67 | throw 'Something went wrong'; |
| 68 | }); |
| 69 | } |