Skip to content

Commit

Permalink
Modify Token.isValid() to use availability of tokens
Browse files Browse the repository at this point in the history
Instead of manually tracking if there is data, we can just check if the
data is there
  • Loading branch information
mvantellingen committed Jan 8, 2025
1 parent a51451d commit b49f892
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-apes-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/federated-token": minor
---

Modify `Token.isValid()` on availability of tokens instead of manually tracking it
15 changes: 5 additions & 10 deletions packages/core/src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export class FederatedToken {
refreshTokens: Record<string, string>;
values: Record<string, unknown>;

private _hasData: boolean;
private _accessTokenModified: boolean;
private _refreshTokenModified: boolean;
private _valueModified: boolean;
Expand Down Expand Up @@ -73,19 +72,16 @@ export class FederatedToken {
setAccessToken(name: string, token: AccessToken) {
this.tokens[name] = token;
this._accessTokenModified = true;
this._hasData = true;
}

setRefreshToken(name: string, token: string) {
this.refreshTokens[name] = token;
this._refreshTokenModified = true;
this._hasData = true;
}

setValue(name: string, value: unknown): void {
this.values[name] = value;
this._valueModified = true;
this._hasData = true;
}

isAccessTokenModified() {
Expand All @@ -100,8 +96,11 @@ export class FederatedToken {
return this._valueModified;
}

isValid() {
return this._hasData;
public isValid(): boolean {
return (
Object.keys(this.tokens).length > 0 ||
Object.keys(this.refreshTokens).length > 0
);
}

// Return the expire time of the first token that expires
Expand Down Expand Up @@ -145,8 +144,6 @@ export class FederatedToken {
Buffer.from(at, "base64").toString("ascii"),
);

this._hasData = true;

if (trackModified) {
this._valueModified = !isEqual(this.values, token.values);

Expand Down Expand Up @@ -180,8 +177,6 @@ export class FederatedToken {
Buffer.from(value, "base64").toString("ascii"),
);

this._hasData = true;

// TODO: Validate json

// Merge tokens in object
Expand Down

0 comments on commit b49f892

Please sign in to comment.