( auth: AuthState, method: AuthMethod, token: Token, hostname: Hostname, )
| 42 | * @returns The updated auth state. |
| 43 | */ |
| 44 | export async function addAccount( |
| 45 | auth: AuthState, |
| 46 | method: AuthMethod, |
| 47 | token: Token, |
| 48 | hostname: Hostname, |
| 49 | ): Promise<AuthState> { |
| 50 | const accountList = auth.accounts; |
| 51 | const encryptedToken = await encryptValue(token); |
| 52 | |
| 53 | let newAccount = { |
| 54 | hostname: hostname, |
| 55 | method: method, |
| 56 | platform: getPlatformFromHostname(hostname), |
| 57 | token: encryptedToken, |
| 58 | user: null, // Will be updated during the refresh call below |
| 59 | } as Account; |
| 60 | |
| 61 | newAccount = await refreshAccount(newAccount); |
| 62 | const newAccountUUID = getAccountUUID(newAccount); |
| 63 | |
| 64 | const existingIndex = accountList.findIndex( |
| 65 | (a) => getAccountUUID(a) === newAccountUUID, |
| 66 | ); |
| 67 | |
| 68 | if (existingIndex >= 0) { |
| 69 | // Clear the cached Octokit client so the new token is used |
| 70 | clearOctokitClientCacheForAccount(accountList[existingIndex]); |
| 71 | // Replace the existing account (e.g. re-authentication with a new token) |
| 72 | rendererLogInfo( |
| 73 | 'addAccount', |
| 74 | `updating existing account for user ${newAccount.user?.login}`, |
| 75 | ); |
| 76 | accountList[existingIndex] = newAccount; |
| 77 | } else { |
| 78 | accountList.push(newAccount); |
| 79 | } |
| 80 | |
| 81 | return { |
| 82 | accounts: accountList, |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Remove an account from the auth state. |
no test coverage detected