()
| 44 | } |
| 45 | |
| 46 | async function validateToken(): Promise<void> { |
| 47 | const tokenField = $('input[name="personalToken"]'); |
| 48 | reportStatus(); |
| 49 | |
| 50 | if (!tokenField.validity.valid || tokenField.value.length === 0) { |
| 51 | // The Chrome options iframe auto-sizer causes the "scrollIntoView" function to scroll incorrectly unless you wait a bit |
| 52 | // https://github.com/refined-github/refined-github/issues/6807 |
| 53 | setTimeout(expandTokenSection, 100); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | reportStatus({text: 'Validating…'}); |
| 58 | |
| 59 | try { |
| 60 | const base = getApiUrl(); |
| 61 | const [tokenInfo, user] = await Promise.all([ |
| 62 | getTokenInfo(base, tokenField.value), |
| 63 | tokenUser.get(base, tokenField.value), |
| 64 | ]); |
| 65 | |
| 66 | // Build status message with user and expiration |
| 67 | let statusMessage = `👤 @${user}`; |
| 68 | if (tokenInfo.expiration) { |
| 69 | const msUntilExpiration = new Date(tokenInfo.expiration).getTime() - Date.now(); |
| 70 | const daysUntilExpiration = Math.ceil(msUntilExpiration / (1000 * 60 * 60 * 24)); |
| 71 | statusMessage += `, expires ${rtf.format(daysUntilExpiration, 'day')}`; |
| 72 | } else { |
| 73 | statusMessage += ', no expiration'; |
| 74 | } |
| 75 | |
| 76 | reportStatus({ |
| 77 | text: statusMessage, |
| 78 | scopes: tokenInfo.scopes, |
| 79 | }); |
| 80 | } catch (error) { |
| 81 | assertError(error); |
| 82 | reportStatus({error: true, text: error.message}); |
| 83 | expandTokenSection(); |
| 84 | throw error; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | export default async function initTokenValidation(syncedForm: SyncedForm | undefined): Promise<void> { |
| 89 | await validateToken(); |
no test coverage detected