Skip to content

Commit

Permalink
fix lint and use ember-lifeline
Browse files Browse the repository at this point in the history
  • Loading branch information
AmauryD committed Feb 7, 2024
1 parent 704fbba commit e8f9712
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 56 deletions.
13 changes: 5 additions & 8 deletions ember-simple-auth-token/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
"build": "rollup --config",
"deploy": "ember build -prod && ember github-pages:commit --message \"Deploy gh-pages from commit $(git rev-parse HEAD)\" && git push origin gh-pages:gh-pages",
"lint": "concurrently \"npm:lint:*(!fix)\" --names \"lint:\"",
"lint:css": "stylelint \"**/*.css\"",
"lint:css:fix": "concurrently \"npm:lint:css -- --fix\"",
"lint:fix": "concurrently \"npm:lint:*:fix\" --names \"fix:\"",
"lint:hbs": "ember-template-lint .",
"lint:hbs:fix": "ember-template-lint . --fix",
"lint:js": "eslint . --cache",
"lint:js:fix": "eslint . --fix",
"prepack": "rollup --config",
Expand All @@ -31,10 +27,10 @@
"license": "MIT",
"dependencies": {
"@babel/core": "^7.23.9",
"@embroider/addon-shim": "^1.8.6"
"@embroider/addon-shim": "^1.8.6",
"ember-lifeline": "^7.0.0"
},
"devDependencies": {
"ember-simple-auth": "~6.0.0",
"@babel/core": "^7.23.2",
"@babel/plugin-proposal-decorators": "^7.23.9",
"@babel/plugin-transform-class-properties": "^7.22.5",
Expand All @@ -44,6 +40,7 @@
"@rollup/plugin-babel": "^6.0.4",
"babel-plugin-ember-template-compilation": "^2.2.1",
"concurrently": "^8.2.2",
"ember-simple-auth": "~6.0.0",
"ember-source": "~5.6.0",
"rollup": "^4.9.6",
"rollup-plugin-copy": "^3.5.0",
Expand All @@ -59,8 +56,8 @@
"authorization"
],
"peerDependencies": {
"ember-source": ">= 4.8.0",
"ember-simple-auth": ">= 6.0.0"
"ember-simple-auth": ">= 6.0.0",
"ember-source": ">= 4.8.0"
},
"engines": {
"node": ">= 18"
Expand Down
28 changes: 12 additions & 16 deletions ember-simple-auth-token/src/authenticators/jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { get } from '@ember/object';
import { getOwner } from '@ember/application';
import { Promise, resolve } from 'rsvp';
import { isEmpty } from '@ember/utils';
import { cancel, later } from '@ember/runloop';
import { runTask, cancelTask } from 'ember-lifeline';
import TokenAuthenticator from './token';

const decode = (str) => {
Expand Down Expand Up @@ -168,13 +168,11 @@ export default class JwtAuthenticator extends TokenAuthenticator {

if (!isEmpty(refreshToken) && !isEmpty(expiresAt)) {
if (wait > 0) {
cancel(this._refreshTokenTimeout);
cancelTask(this, this._refreshTokenTimeout);
delete this._refreshTokenTimeout;
this._refreshTokenTimeout = later(
this._refreshTokenTimeout = runTask(
this,
this.refreshAccessToken,
refreshToken,
0,
() => this.refreshAccessToken(refreshToken, 0),
wait,
);
} else if (expiresAt > now) {
Expand Down Expand Up @@ -270,9 +268,9 @@ export default class JwtAuthenticator extends TokenAuthenticator {
@return {Promise} Resolving promise
*/
invalidate() {
cancel(this._refreshTokenTimeout);
cancelTask(this, this._refreshTokenTimeout);
delete this._refreshTokenTimeout;
cancel(this._tokenExpirationTimeout);
cancelTask(this, this._tokenExpirationTimeout);
delete this._tokenExpirationTimeout;
return resolve();
}
Expand Down Expand Up @@ -343,13 +341,11 @@ export default class JwtAuthenticator extends TokenAuthenticator {
this.trigger('sessionDataInvalidated');
});
} else if (attempts++ < this.refreshAccessTokenRetryAttempts) {
cancel(this._refreshTokenTimeout);
cancelTask(this, this._refreshTokenTimeout);
delete this._refreshTokenTimeout;
this._refreshTokenTimeout = later(
this._refreshTokenTimeout = runTask(
this,
this.refreshAccessToken,
refreshToken,
attempts,
() => this.refreshAccessToken(refreshToken, attempts),
this.refreshAccessTokenRetryTimeout,
);
} else if (this.tokenRefreshFailInvalidateSession) {
Expand All @@ -370,11 +366,11 @@ export default class JwtAuthenticator extends TokenAuthenticator {
const wait = Math.max((expiresAt - now) * 1000, 0);

if (!isEmpty(expiresAt)) {
cancel(this._tokenExpirationTimeout);
cancelTask(this, this._tokenExpirationTimeout);
delete this._tokenExpirationTimeout;
this._tokenExpirationTimeout = later(
this._tokenExpirationTimeout = runTask(
this,
this.handleAccessTokenExpiration,
() => this.handleAccessTokenExpiration(),
wait,
);
}
Expand Down
12 changes: 8 additions & 4 deletions ember-simple-auth-token/src/authenticators/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ export default class TokenAuthenticator extends Base {
@return {Promise} Promise that resolves when an auth token is successfully acquired from the server and rejects otherwise
*/
async authenticate(credentials, headers) {
const response = await this.makeRequest(this.serverTokenEndpoint, credentials, {
...this.headers,
...headers,
});
const response = await this.makeRequest(
this.serverTokenEndpoint,
credentials,
{
...this.headers,
...headers,
},
);
return response.json;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export default {
initialize(container) {
container.register('authenticator:token', TokenAuthenticator);
container.register('authenticator:jwt', JWTAuthenticator);
}
},
};
49 changes: 36 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test-app/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ module.exports = {
'./testem.js',
'./blueprints/*/index.js',
'./config/**/*.js',
'playwright.config.js',
'e2e/**/*.js',
'./tests/dummy/config/**/*.js',
],
parserOptions: {
Expand Down
1 change: 0 additions & 1 deletion test-app/app/routes/application.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { action } from '@ember/object';
import Route from '@ember/routing/route';
import { inject } from '@ember/service';

Expand Down
29 changes: 20 additions & 9 deletions test-app/config/fastboot.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
module.exports = function() {
module.exports = function () {
return {
buildSandboxGlobals(defaultGlobals) {
return Object.assign({}, defaultGlobals, {
TextEncoder: typeof TextEncoder !== 'undefined' ? TextEncoder : require('util').TextEncoder,
TextEncoder:
typeof TextEncoder !== 'undefined'
? TextEncoder
: require('util').TextEncoder,
AbortController,
navigator: {

},
Response: typeof Response !== 'undefined' ? Response : require('node-fetch').Response,
navigator: {},
Response:
typeof Response !== 'undefined'
? Response
: // eslint-disable-next-line n/no-missing-require
require('node-fetch').Response,
ReadableStream:
typeof ReadableStream !== 'undefined' ? ReadableStream : require('node:stream/web').ReadableStream,
typeof ReadableStream !== 'undefined'
? ReadableStream
: require('node:stream/web').ReadableStream,
WritableStream:
typeof WritableStream !== 'undefined' ? WritableStream : require('node:stream/web').WritableStream,
typeof WritableStream !== 'undefined'
? WritableStream
: require('node:stream/web').WritableStream,
TransformStream:
typeof TransformStream !== 'undefined' ? TransformStream : require('node:stream/web').TransformStream,
typeof TransformStream !== 'undefined'
? TransformStream
: require('node:stream/web').TransformStream,
Headers: typeof Headers !== 'undefined' ? Headers : undefined,
});
},
Expand Down
2 changes: 1 addition & 1 deletion test-app/config/targets.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ const browsers = [

module.exports = {
browsers,
node: 'current'
node: 'current',
};
2 changes: 1 addition & 1 deletion test-app/e2e/example.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ test('Renders page in fastboot mode', async ({ page }) => {

// Expect a title "to contain" a substring.
await expect(page).toHaveTitle(/Dummy/);
await expect(page.locator("h2")).toHaveText("Welcome to Ember");
await expect(page.locator('h2')).toHaveText('Welcome to Ember');
});
1 change: 1 addition & 0 deletions test-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"ember-page-title": "^8.2.0",
"ember-qunit": "^8.0.2",
"ember-resolver": "^11.0.1",
"ember-simple-auth": "~6.0.0",
"ember-sinon-qunit": "^7.4.0",
"ember-source": "~5.6.0",
"ember-source-channel-url": "^3.0.0",
Expand Down
3 changes: 1 addition & 2 deletions test-app/playwright.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = defineConfig({
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
}
},

/* Test against mobile viewports. */
// {
Expand Down Expand Up @@ -66,4 +66,3 @@ module.exports = defineConfig({
reuseExistingServer: !process.env.CI,
},
});

0 comments on commit e8f9712

Please sign in to comment.