Skip to content

Commit e3d63cc

Browse files
committed
Implemented popup state management logic; Multiple other optmizations (error/state handling; type casting; refactor) and formatting;
1 parent 413ce62 commit e3d63cc

File tree

1 file changed

+86
-69
lines changed

1 file changed

+86
-69
lines changed

src/index.ts

Lines changed: 86 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,75 +7,77 @@ import {
77
pollOauthSession,
88
retrieveAccessToken,
99
refreshToken,
10-
revokeToken,
10+
revokeToken
1111
} from './Oauth';
1212

1313
const DOMAIN_WINDOW_DEFAULT_URL = 'https://dev.frontify.test/finder';
1414
const POPUP_DEFAULT_TITLE = 'Authorize Frontify';
1515
const POPUP_STATE = {
16-
open: false,
17-
retry: false
16+
open: false
1817
}
1918

2019
let popup: Popup;
2120

2221
export async function authorize(
2322
configuration: AuthenticationConfig,
24-
popupConfiguration?: PopupConfiguration,
25-
): Promise<Token | void | null>{
26-
let token = null;
23+
popupConfiguration?: PopupConfiguration
24+
): Promise<Token | void>{
2725

2826
if (POPUP_STATE.open) {
29-
console.log('popup is already open!');
30-
return;
27+
logMessage('warning', {
28+
code: 'ERR_POPUP_OPEN',
29+
message: 'Popup already open!'
30+
})
31+
throw new Error('Popup already open!');
3132
}
3233

33-
POPUP_STATE.open = true;
34+
popup = createPopUp(
35+
popupConfiguration ?? {
36+
width: 800,
37+
height: 600,
38+
top: 50,
39+
left: 50,
40+
title: POPUP_DEFAULT_TITLE,
41+
},
42+
);
3443

35-
if (!POPUP_STATE.retry) {
36-
popup = createPopUp(
37-
popupConfiguration ?? {
38-
width: 800,
39-
height: 600,
40-
top: 50,
41-
left: 50,
42-
title: POPUP_DEFAULT_TITLE,
43-
},
44-
);
45-
}
44+
POPUP_STATE.open = true;
4645

4746
if (!configuration.domain) {
48-
await openDomainPopUp(popup).then(() => {
49-
configuration.domain = popup.getDomain();
50-
POPUP_STATE.retry = false;
51-
token = authenticate(configuration, popup).catch(() => {
52-
delete(configuration.domain);
53-
POPUP_STATE.retry = true;
54-
authorize(configuration, popupConfiguration);
47+
return openDomainPopUp(configuration, popup).then((res) => {
48+
POPUP_STATE.open = false;
49+
if (res) {
50+
return res;
51+
}
52+
}).catch(() => {
53+
delete(configuration.domain);
54+
logMessage('error', {
55+
code: 'ERR_AUTH_SKIPPED',
56+
message: 'Domain not inserted!'
5557
});
58+
throw new Error('Domain not inserted!');
5659
});
5760
} else {
58-
POPUP_STATE.retry = false;
59-
token = authenticate(configuration, popup).catch(() => {
60-
delete(configuration.domain);
61-
POPUP_STATE.retry = true;
62-
authorize(configuration, popupConfiguration);
61+
return authenticate(configuration, popup).then((res) => {
62+
POPUP_STATE.open = false;
63+
if (res) {
64+
return res;
65+
}
66+
}).catch((error) => {
67+
POPUP_STATE.open = false;
68+
throw new Error(error);
6369
});
6470
}
65-
66-
return token;
67-
6871
}
6972

7073
async function authenticate(configuration: AuthenticationConfig, popUp: Popup): Promise<Token> {
7174
try {
7275
const { authorizationUrl, codeVerifier, sessionId } = await computeAuthorizationUrl(configuration);
73-
await awaitUserAuthorization(authorizationUrl, popUp);
76+
await openAuthPopUp(authorizationUrl, popUp);
7477
const authorizationCode = await pollOauthSession(configuration, sessionId);
7578
return retrieveAccessToken(configuration, authorizationCode, codeVerifier);
7679
} catch (error) {
77-
const errorMessage = `Error generating session. Make sure the inserted domain ${configuration.domain} is a valid and secure Frontify instance`;
78-
delete(configuration.domain);
80+
const errorMessage = `Error generating session. Make sure that the inserted domain is a valid and secure Frontify instance.`;
7981
popUp.popUp?.postMessage({domainError: errorMessage}, '*');
8082
logMessage('error', {
8183
code: 'ERR_AUTH_FAILED',
@@ -86,20 +88,20 @@ async function authenticate(configuration: AuthenticationConfig, popUp: Popup):
8688
}
8789

8890
export async function refresh(token: Token): Promise<Token> {
89-
return await refreshToken(token.bearerToken.domain, token.bearerToken.refreshToken, token.clientId, token.scopes);
91+
return refreshToken(token.bearerToken.domain, token.bearerToken.refreshToken, token.clientId, token.scopes);
9092
}
9193

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

97-
async function awaitUserAuthorization(authorizationUrl: string, popUp: Popup) {
98-
popUp.navigateToUrl(authorizationUrl);
99+
function openDomainPopUp(configuration: AuthenticationConfig, popUp: Popup): Promise<Token> {
100+
popUp.navigateToUrl(DOMAIN_WINDOW_DEFAULT_URL);
99101

100102
logMessage('warning', {
101-
code: 'WARN_AUTH_OPEN',
102-
message: 'Auth window opened!'
103+
code: 'WARN_DOMAIN_OPEN',
104+
message: 'Popup window opened!'
103105
});
104106

105107
return new Promise((resolve, reject) => {
@@ -108,43 +110,45 @@ async function awaitUserAuthorization(authorizationUrl: string, popUp: Popup) {
108110
popUp.close();
109111
reject();
110112
logMessage('error', {
111-
code: 'WARN_AUTH_SUCCESS',
112-
message: 'Auth timeout!'
113+
code: 'ERR_DOMAIN_TIMEOUT',
114+
message: 'Popup window timeout!'
113115
})
114116
}, 5 * 60 * 1000);
115117

116-
popUp.onSuccess(() => {
117-
POPUP_STATE.open = false;
118+
popUp.onDomain(() => {
118119
clearTimeout(timeout);
119-
popUp.close();
120-
resolve(null);
120+
configuration.domain = popup.getDomain();
121+
authenticate(configuration, popup).then((result) => {
122+
resolve(result);
123+
}).catch((error) => {
124+
throw new Error(error ?? 'Could not verify instance!');
125+
});
121126
logMessage('warning', {
122-
code: 'WARN_AUTH_SUCCESS',
123-
message: 'Auth success!'
127+
code: 'WARN_DOMAIN_SELECT',
128+
message: 'Domain input submitted!'
124129
})
125130
});
126131

127-
popUp.onCancelled(() => {
132+
popUp.onAborted(() => {
128133
POPUP_STATE.open = false;
129134
clearTimeout(timeout);
130135
popUp.close();
131136
reject();
132137
logMessage('warning', {
133-
code: 'WARN_AUTH_CANCELLED',
134-
message: 'Auth cancelled!'
138+
code: 'WARN_DOMAIN_CLOSED',
139+
message: 'Popup window closed!'
135140
})
136141
});
137142
});
138143
}
139144

140-
function openDomainPopUp(popUp: Popup): Promise<void> {
141-
if (!POPUP_STATE.retry) {
142-
popUp.navigateToUrl(DOMAIN_WINDOW_DEFAULT_URL);
143-
logMessage('warning', {
144-
code: 'WARN_DOMAIN_OPEN',
145-
message: 'Domain window opened!'
146-
});
147-
}
145+
function openAuthPopUp(url: string, popUp: Popup): Promise<void> {
146+
popUp.navigateToUrl(url);
147+
148+
logMessage('warning', {
149+
code: 'WARN_DOMAIN_OPEN',
150+
message: 'Popup window opened!'
151+
});
148152

149153
return new Promise((resolve, reject) => {
150154
const timeout = setTimeout(() => {
@@ -153,27 +157,40 @@ function openDomainPopUp(popUp: Popup): Promise<void> {
153157
reject();
154158
logMessage('error', {
155159
code: 'ERR_DOMAIN_TIMEOUT',
156-
message: 'Domain window timeout!'
160+
message: 'Popup window timeout!'
157161
})
158162
}, 5 * 60 * 1000);
159163

160-
popUp.onDomain(() => {
164+
popUp.onAborted(() => {
165+
POPUP_STATE.open = false;
161166
clearTimeout(timeout);
167+
popUp.close();
168+
reject();
169+
logMessage('warning', {
170+
code: 'WARN_DOMAIN_CLOSED',
171+
message: 'Popup window closed!'
172+
})
173+
});
174+
175+
popUp.onSuccess(() => {
176+
POPUP_STATE.open = false;
177+
clearTimeout(timeout);
178+
popUp.close();
162179
resolve();
163180
logMessage('warning', {
164-
code: 'WARN_DOMAIN_SELECT',
165-
message: 'Domain select success!'
181+
code: 'WARN_AUTH_SUCCESS',
182+
message: 'Auth success!'
166183
})
167184
});
168185

169-
popUp.onAborted(() => {
186+
popUp.onCancelled(() => {
170187
POPUP_STATE.open = false;
171188
clearTimeout(timeout);
172189
popUp.close();
173190
reject();
174191
logMessage('warning', {
175-
code: 'WARN_DOMAIN_CLOSED',
176-
message: 'Domain window closed!'
192+
code: 'WARN_AUTH_CANCELLED',
193+
message: 'Auth cancelled!'
177194
})
178195
});
179196
});

0 commit comments

Comments
 (0)