Skip to content

Commit

Permalink
refactor: 💡 use head method request & clean up comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lisbet-alvarez committed Dec 27, 2024
1 parent 27f3fd2 commit 5c0125e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
10 changes: 6 additions & 4 deletions addons/auth/addon/authenticators/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,20 @@ export default class BaseAuthenticator extends SimpleAuthBaseAuthenticator {
*/
async validateToken(token, tokenID) {
const tokenValidationURL = this.buildTokenValidationEndpointURL(tokenID);

// Note: waitForPromise is needed to provide the necessary integration with @ember/test-helpers
// visit https://www.npmjs.com/package/@ember/test-waiters for more info.
const response = await waitForPromise(
fetch(tokenValidationURL, {
method: 'get',
method: 'head',
headers: { Authorization: `Bearer ${token}` },
}),
);

// Note: Always consume response object in order to avoid memory leaks.
// Note: HEAD request is made here to avoid dealing with a response body
// visit https://undici.nodejs.org/#/?id=garbage-collection for more info.
await response.json();
// We do not use the undici package but the link informs us that garbage
// collection is undefined when response body is not consumed.

console.log('TESTING VALIDATE TOKEN!!', response);

// 401 and 404 responses mean the token is invalid, whereas other types of
Expand Down
8 changes: 3 additions & 5 deletions addons/auth/addon/authenticators/oidc.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ export default class OIDCAuthenticator extends BaseAuthenticator {
// Note: waitForPromise is needed to provide the necessary integration with @ember/test-helpers
// visit https://www.npmjs.com/package/@ember/test-waiters for more info.
const response = await waitForPromise(fetch(url, { method: 'post', body }));

// Note: Always consume response object in order to avoid memory leaks.
// visit https://undici.nodejs.org/#/?id=garbage-collection for more info.
const json = await response.json();
if (response.status < 400) {
// Store meta about the pending OIDC flow
Expand Down Expand Up @@ -101,8 +98,10 @@ export default class OIDCAuthenticator extends BaseAuthenticator {
// Fetch the endpoint and get the response JSON
const response = await waitForPromise(fetch(url, { method: 'post', body }));

// Note: Always consume response object in order to avoid memory leaks.
// Note: Always consume response body in order to avoid memory leaks
// visit https://undici.nodejs.org/#/?id=garbage-collection for more info.
// We do not use the undici package but the link informs us that garbage
// collection is undefined when response body is not consumed.
const json = await response.json();

if (response.status === 202) {
Expand All @@ -111,7 +110,6 @@ export default class OIDCAuthenticator extends BaseAuthenticator {
} else if (response.status < 400) {
// Response was successful, meaning a token was obtained.
// Authenticate with the session service using the response JSON.
// const json = await response.json();
await this.session.authenticate('authenticator:oidc', json);
return true;
} else {
Expand Down

0 comments on commit 5c0125e

Please sign in to comment.