Skip to content

Commit

Permalink
Linting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
henser committed Oct 5, 2021
1 parent 45c4d7a commit e3d6814
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 59 deletions.
6 changes: 1 addition & 5 deletions src/Oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,7 @@ export async function pollOauthSession(config: AuthenticationConfig, sessionId:
}
}

export async function getAccessToken(
config: AuthenticationConfig,
code: string,
codeVerifier: string,
): Promise<Token> {
export async function getAccessToken(config: AuthenticationConfig, code: string, codeVerifier: string): Promise<Token> {
if (!config.domain) {
throw new Error('No domain provided!');
}
Expand Down
113 changes: 59 additions & 54 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,26 @@ import {
pollOauthSession,
getAccessToken,
getRefreshToken,
revokeToken
revokeToken,
} from './Oauth';

const DOMAIN_WINDOW_DEFAULT_URL = 'https://dev.frontify.test/finder';
const POPUP_DEFAULT_TITLE = 'Authorize Frontify';
const POPUP_STATE = {
open: false
}
open: false,
};

let popup: Popup;

export async function authorize(
configuration: AuthenticationConfig,
popupConfiguration?: PopupConfiguration
): Promise<Token | void>{

popupConfiguration?: PopupConfiguration,
): Promise<Token | void> {
if (POPUP_STATE.open) {
logMessage('warning', {
code: 'ERR_POPUP_OPEN',
message: 'Popup already open!'
})
message: 'Popup already open!',
});
throw new Error('Popup already open!');
}

Expand All @@ -44,29 +43,33 @@ export async function authorize(
POPUP_STATE.open = true;

if (!configuration.domain) {
return openDomainPopUp(configuration, popup).then((res) => {
POPUP_STATE.open = false;
if (res) {
return res;
}
}).catch(() => {
delete(configuration.domain);
logMessage('error', {
code: 'ERR_AUTH_SKIPPED',
message: 'Domain not inserted!'
return openDomainPopUp(configuration, popup)
.then((res) => {
POPUP_STATE.open = false;
if (res) {
return res;
}
})
.catch(() => {
delete configuration.domain;
logMessage('error', {
code: 'ERR_AUTH_SKIPPED',
message: 'Domain not inserted!',
});
throw new Error('Domain not inserted!');
});
throw new Error('Domain not inserted!');
});
} else {
return authenticate(configuration, popup).then((res) => {
POPUP_STATE.open = false;
if (res) {
return res;
}
}).catch((error) => {
POPUP_STATE.open = false;
throw new Error(error);
});
return authenticate(configuration, popup)
.then((res) => {
POPUP_STATE.open = false;
if (res) {
return res;
}
})
.catch((error) => {
POPUP_STATE.open = false;
throw new Error(error);
});
}
}

Expand All @@ -75,7 +78,7 @@ export async function refresh(token: Token): Promise<Token> {
}

export async function revoke(token: Token): Promise<Token> {
await revokeToken(token.bearerToken.domain, token.bearerToken.accessToken)
await revokeToken(token.bearerToken.domain, token.bearerToken.accessToken);
return token;
}

Expand All @@ -87,10 +90,10 @@ async function authenticate(configuration: AuthenticationConfig, popUp: Popup):
return getAccessToken(configuration, authorizationCode, codeVerifier);
} catch (error) {
const errorMessage = `Error generating session. Make sure that the inserted domain is a valid and secure Frontify instance.`;
popUp.popUp?.postMessage({domainError: errorMessage}, '*');
popUp.popUp?.postMessage({ domainError: errorMessage }, '*');
logMessage('error', {
code: 'ERR_AUTH_FAILED',
message: errorMessage
message: errorMessage,
});
throw new Error(errorMessage);
}
Expand All @@ -101,7 +104,7 @@ function openDomainPopUp(configuration: AuthenticationConfig, popUp: Popup): Pro

logMessage('warning', {
code: 'WARN_DOMAIN_OPEN',
message: 'Popup window opened!'
message: 'Popup window opened!',
});

return new Promise((resolve, reject) => {
Expand All @@ -111,22 +114,24 @@ function openDomainPopUp(configuration: AuthenticationConfig, popUp: Popup): Pro
reject();
logMessage('error', {
code: 'ERR_DOMAIN_TIMEOUT',
message: 'Popup window timeout!'
})
message: 'Popup window timeout!',
});
}, 5 * 60 * 1000);

popUp.onDomain(() => {
clearTimeout(timeout);
configuration.domain = popup.getDomain();
authenticate(configuration, popup).then((result) => {
resolve(result);
}).catch((error) => {
throw new Error(error ?? 'Could not verify instance!');
});
authenticate(configuration, popup)
.then((result) => {
resolve(result);
})
.catch((error) => {
throw new Error(error ?? 'Could not verify instance!');
});
logMessage('warning', {
code: 'WARN_DOMAIN_SELECT',
message: 'Domain input submitted!'
})
message: 'Domain input submitted!',
});
});

popUp.onAborted(() => {
Expand All @@ -136,8 +141,8 @@ function openDomainPopUp(configuration: AuthenticationConfig, popUp: Popup): Pro
reject();
logMessage('warning', {
code: 'WARN_DOMAIN_CLOSED',
message: 'Popup window closed!'
})
message: 'Popup window closed!',
});
});
});
}
Expand All @@ -147,7 +152,7 @@ function openAuthPopUp(url: string, popUp: Popup): Promise<void> {

logMessage('warning', {
code: 'WARN_DOMAIN_OPEN',
message: 'Popup window opened!'
message: 'Popup window opened!',
});

return new Promise((resolve, reject) => {
Expand All @@ -157,8 +162,8 @@ function openAuthPopUp(url: string, popUp: Popup): Promise<void> {
reject();
logMessage('error', {
code: 'ERR_DOMAIN_TIMEOUT',
message: 'Popup window timeout!'
})
message: 'Popup window timeout!',
});
}, 5 * 60 * 1000);

popUp.onAborted(() => {
Expand All @@ -168,8 +173,8 @@ function openAuthPopUp(url: string, popUp: Popup): Promise<void> {
reject();
logMessage('warning', {
code: 'WARN_DOMAIN_CLOSED',
message: 'Popup window closed!'
})
message: 'Popup window closed!',
});
});

popUp.onSuccess(() => {
Expand All @@ -179,8 +184,8 @@ function openAuthPopUp(url: string, popUp: Popup): Promise<void> {
resolve();
logMessage('warning', {
code: 'WARN_AUTH_SUCCESS',
message: 'Auth success!'
})
message: 'Auth success!',
});
});

popUp.onCancelled(() => {
Expand All @@ -190,12 +195,12 @@ function openAuthPopUp(url: string, popUp: Popup): Promise<void> {
reject();
logMessage('warning', {
code: 'WARN_AUTH_CANCELLED',
message: 'Auth cancelled!'
})
message: 'Auth cancelled!',
});
});
});
}

function createPopUp(configuration: PopupConfiguration): Popup {
return new Popup(configuration ?? {});
}
}

0 comments on commit e3d6814

Please sign in to comment.