General source code refactoring
This commit is contained in:
57
src/auth/content-session.js
Normal file
57
src/auth/content-session.js
Normal file
@@ -0,0 +1,57 @@
|
||||
function createAuthContentSession({
|
||||
messageTypes,
|
||||
AUTH_STATUS,
|
||||
appendBackgroundRequestLog,
|
||||
authSessionState,
|
||||
requestTokenRelay,
|
||||
sendRuntimeMessage,
|
||||
updateAuthBadge
|
||||
}) {
|
||||
function requestTokenRelayOnce() {
|
||||
if (!authSessionState.tokenRelayRequest) {
|
||||
authSessionState.authStatus = AUTH_STATUS.pending;
|
||||
updateAuthBadge(authSessionState.authStatus);
|
||||
authSessionState.tokenRelayRequest = requestTokenRelay()
|
||||
.then((token) => {
|
||||
authSessionState.accessToken = token;
|
||||
authSessionState.authStatus = AUTH_STATUS.authenticated;
|
||||
requestConsumerTokenServiceOnce().catch(() => {});
|
||||
return token;
|
||||
})
|
||||
.catch((error) => {
|
||||
authSessionState.accessToken = "";
|
||||
authSessionState.authStatus = AUTH_STATUS.unauthenticated;
|
||||
throw error;
|
||||
})
|
||||
.finally(() => {
|
||||
updateAuthBadge(authSessionState.authStatus);
|
||||
});
|
||||
} else {
|
||||
updateAuthBadge(authSessionState.authStatus);
|
||||
}
|
||||
|
||||
return authSessionState.tokenRelayRequest;
|
||||
}
|
||||
|
||||
function requestConsumerTokenServiceOnce() {
|
||||
if (!authSessionState.consumerTokenServiceRequest) {
|
||||
authSessionState.consumerTokenServiceRequest = sendRuntimeMessage({
|
||||
type: messageTypes.requestConsumerTokenService
|
||||
}).then((response) => {
|
||||
appendBackgroundRequestLog(response && response.debugRequests);
|
||||
|
||||
if (!response || !response.ok) {
|
||||
throw new Error(response && response.error
|
||||
? response.error
|
||||
: "Unable to load consumer token service.");
|
||||
}
|
||||
|
||||
return response.authorizations;
|
||||
});
|
||||
}
|
||||
|
||||
return authSessionState.consumerTokenServiceRequest;
|
||||
}
|
||||
|
||||
return { requestTokenRelayOnce, requestConsumerTokenServiceOnce };
|
||||
}
|
||||
Reference in New Issue
Block a user