small works
Some checks failed
CI / Lint (push) Failing after 7m46s
CI / Test (push) Failing after 8m24s

This commit is contained in:
2025-04-04 18:21:37 +02:00
parent 211393728d
commit 0b29bc2d44
7 changed files with 297 additions and 31 deletions

View File

@@ -82,7 +82,7 @@ export default BaseAuthenticator.extend({
*/
restore(data) {
if (this._refreshTokenTimeout) {
run.cancel(this._refreshTokenTimeout);
cancel(this._refreshTokenTimeout);
delete this._refreshTokenTimeout;
}
return this._refreshAccessToken(data);
@@ -106,13 +106,14 @@ export default BaseAuthenticator.extend({
*/
authenticate(identification, password) {
return new RSVP.Promise((resolve, reject) => {
const {
identificationAttributeName,
passwordAttributeName
} = this.getProperties('identificationAttributeName', 'passwordAttributeName');
const { identificationAttributeName, passwordAttributeName } =
this.getProperties(
'identificationAttributeName',
'passwordAttributeName',
);
const data = {
[identificationAttributeName]: identification,
[passwordAttributeName]: password
[passwordAttributeName]: password,
};
const serverTokenEndpoint = this.get('serverTokenEndpoint');
@@ -141,7 +142,7 @@ export default BaseAuthenticator.extend({
@public
*/
invalidate() {
run.cancel(this._refreshTokenTimeout);
cancel(this._refreshTokenTimeout);
delete this._refreshTokenTimeout;
return RSVP.Promise.resolve();
},
@@ -161,22 +162,24 @@ export default BaseAuthenticator.extend({
const options = {
body: JSON.stringify(data),
headers,
method: 'POST'
method: 'POST',
};
return new RSVP.Promise((resolve, reject) => {
fetch(url, options).then((response) => {
response.text().then((text) => {
let json = text ? JSON.parse(text) : {};
if (!response.ok) {
response.responseJSON = json;
reject(response);
} else {
window.localStorage.setItem('jwtLastRefreshAt', Date.now());
resolve(json);
}
});
}).catch(reject);
fetch(url, options)
.then((response) => {
response.text().then((text) => {
let json = text ? JSON.parse(text) : {};
if (!response.ok) {
response.responseJSON = json;
reject(response);
} else {
window.localStorage.setItem('jwtLastRefreshAt', Date.now());
resolve(json);
}
});
})
.catch(reject);
});
},
@@ -219,7 +222,7 @@ export default BaseAuthenticator.extend({
const offset = 1000; // Refresh 1 sec before JWT expires
const now = Date.now();
const waitMs = (jwtPayloadExpiresAt * 1000) - now - offset; //expiresAt is in sec
const waitMs = jwtPayloadExpiresAt * 1000 - now - offset; //expiresAt is in sec
if (this._refreshTokenTimeout) {
cancel(this._refreshTokenTimeout);
@@ -228,12 +231,18 @@ export default BaseAuthenticator.extend({
// Reschedule if the JWT is still valid
if (waitMs > 0) {
this._refreshTokenTimeout = later(this, this._refreshAccessToken, data, waitMs);
this._refreshTokenTimeout = later(
this,
this._refreshAccessToken,
data,
waitMs,
);
}
},
_refreshAccessToken(data) {
var timeElapsedSinceLastRefresh = Date.now() - window.localStorage.getItem('jwtLastRefreshAt')
var timeElapsedSinceLastRefresh =
Date.now() - window.localStorage.getItem('jwtLastRefreshAt');
if (timeElapsedSinceLastRefresh <= this.get('refreshTokenAfter')) {
// Request attempted too soon! Reschedule
return this._validateTokenAndScheduleRefresh(data);
@@ -242,7 +251,9 @@ export default BaseAuthenticator.extend({
const serverRefreshTokenEndpoint = this.get('serverRefreshTokenEndpoint');
return new RSVP.Promise((resolve, reject) => {
this.makeRequest(serverRefreshTokenEndpoint, data, { Authorization:data["access_token"] })
this.makeRequest(serverRefreshTokenEndpoint, data, {
Authorization: data['access_token'],
})
.then((response) => {
return this._validateTokenAndScheduleRefresh(response);
})
@@ -257,7 +268,7 @@ export default BaseAuthenticator.extend({
reason = JSON.stringify(reason.responseJSON);
}
warn(`JWT token could not be refreshed: ${reason}.`, false, {
id: 'ember-simple-auth-jwt.failedJWTTokenRefresh'
id: 'ember-simple-auth-jwt.failedJWTTokenRefresh',
});
reject();
@@ -267,10 +278,12 @@ export default BaseAuthenticator.extend({
_validateTokenAndScheduleRefresh(response) {
if (!this._validate(response)) {
return RSVP.Promise.reject('token is missing or invalid in server response');
return RSVP.Promise.reject(
'token is missing or invalid in server response',
);
}
this._scheduleTokenRefresh(response);
return RSVP.Promise.resolve(response);
}
},
});