Commit 06e6bd1a authored by Robert Knight's avatar Robert Knight

Extract `exchangeAuthCodeForToken` method

Extract a helper from a long async method to shorten it.
parent edb9c257
...@@ -161,6 +161,20 @@ export class AuthService extends TinyEmitter { ...@@ -161,6 +161,20 @@ export class AuthService extends TinyEmitter {
return tokenInfo; return tokenInfo;
}; };
/**
* Exchange authorization code retrieved from login popup for a new
* access token.
*/
const exchangeAuthCodeForToken = async () => {
const code = /** @type {string} */ (authCode);
authCode = null; // Auth codes can only be used once.
const client = await oauthClient();
const tokenInfo = await client.exchangeAuthCode(code);
saveToken(tokenInfo);
return tokenInfo;
};
/** /**
* Retrieve an access token for the API. * Retrieve an access token for the API.
* *
...@@ -189,16 +203,7 @@ export class AuthService extends TinyEmitter { ...@@ -189,16 +203,7 @@ export class AuthService extends TinyEmitter {
tokenInfoPromise = Promise.resolve(null); tokenInfoPromise = Promise.resolve(null);
} }
} else if (authCode) { } else if (authCode) {
// Exchange authorization code retrieved from login popup for a new tokenInfoPromise = exchangeAuthCodeForToken();
// access token.
const code = authCode;
authCode = null; // Auth codes can only be used once.
tokenInfoPromise = oauthClient()
.then(client => client.exchangeAuthCode(code))
.then(tokenInfo => {
saveToken(tokenInfo);
return tokenInfo;
});
} else { } else {
// Attempt to load the tokens from the previous session. // Attempt to load the tokens from the previous session.
tokenInfoPromise = Promise.resolve(loadToken()); tokenInfoPromise = Promise.resolve(loadToken());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment