-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor token storage & renewal with TokenState class #63
- Loading branch information
Showing
10 changed files
with
239 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import { | ||
getCurrentAccessToken, | ||
getRefreshToken, | ||
resetAllTokens, | ||
storeAccessToken, | ||
storeCurrentAccessToken, | ||
storeRefreshToken, | ||
} from "../utils/storage"; | ||
import { TokenPayload, getTokenPayload, isTokenExpired } from "../utils/token"; | ||
|
||
type Subscriber = (token: TokenPayload | null) => void; | ||
type Unsubscribe = () => void; | ||
|
||
export class TokenState { | ||
private _refreshToken = getRefreshToken(); | ||
private _refreshTokenPayload: TokenPayload | null = null; | ||
private _accessToken = getCurrentAccessToken(); | ||
private _accessTokenPayload: TokenPayload | null = null; | ||
private refreshTokenSubscribers: Subscriber[] = []; | ||
private accessTokenSubscribers: Subscriber[] = []; | ||
|
||
constructor() { | ||
this.afterRefreshTokenUpdate(this.refreshToken, false); | ||
this.afterAccessTokenUpdate(this.accessToken, false); | ||
} | ||
|
||
get refreshToken() { | ||
return this._refreshToken; | ||
} | ||
set refreshToken(refreshToken: string | null) { | ||
this._refreshToken = refreshToken; | ||
this.afterRefreshTokenUpdate(refreshToken); | ||
} | ||
|
||
get refreshTokenPayload() { | ||
return this._refreshTokenPayload; | ||
} | ||
|
||
get accessToken() { | ||
return this._accessToken; | ||
} | ||
set accessToken(accessToken: string | null) { | ||
this._accessToken = accessToken; | ||
this.afterAccessTokenUpdate(accessToken); | ||
} | ||
|
||
get accessTokenPayload() { | ||
return this._accessTokenPayload; | ||
} | ||
|
||
get authenticated(): boolean { | ||
return Boolean(this.accessToken); | ||
} | ||
|
||
get scope(): string | null { | ||
return this.accessTokenPayload?.scope ?? null; | ||
} | ||
|
||
get locale(): string | null { | ||
return this.accessTokenPayload?.locale ?? null; | ||
} | ||
|
||
get instanceId(): string | null { | ||
return this.accessTokenPayload?.instanceId ?? null; | ||
} | ||
|
||
isRefreshTokenExpired(): boolean { | ||
return isTokenExpired(this._refreshTokenPayload); | ||
} | ||
|
||
resetAllTokens(): void { | ||
this._refreshToken = null; | ||
this._refreshTokenPayload = null; | ||
|
||
this._accessToken = null; | ||
this._accessTokenPayload = null; | ||
|
||
resetAllTokens(); | ||
} | ||
|
||
onRefreshTokenUpdate(callback: Subscriber): Unsubscribe { | ||
this.refreshTokenSubscribers.push(callback); | ||
|
||
// Initially call with current value | ||
callback(this.refreshTokenPayload); | ||
|
||
return () => { | ||
const index = this.refreshTokenSubscribers.findIndex( | ||
(s) => s === callback, | ||
); | ||
this.refreshTokenSubscribers.splice(index, 1); | ||
}; | ||
} | ||
|
||
onAccessTokenUpdate(callback: Subscriber): Unsubscribe { | ||
this.accessTokenSubscribers.push(callback); | ||
|
||
// Initially call with current value | ||
callback(this.accessTokenPayload); | ||
|
||
return () => { | ||
const index = this.accessTokenSubscribers.findIndex( | ||
(s) => s === callback, | ||
); | ||
this.accessTokenSubscribers.splice(index, 1); | ||
}; | ||
} | ||
|
||
private afterRefreshTokenUpdate( | ||
refreshToken: string | null, | ||
store = true, | ||
): void { | ||
this._refreshTokenPayload = refreshToken | ||
? getTokenPayload(refreshToken) | ||
: null; | ||
|
||
if (refreshToken && store) { | ||
storeRefreshToken(refreshToken); | ||
} | ||
|
||
this.notifyRefreshTokenSubscribers(); | ||
} | ||
|
||
private afterAccessTokenUpdate( | ||
accessToken: string | null, | ||
store = true, | ||
): void { | ||
const payload = accessToken ? getTokenPayload(accessToken) : null; | ||
this._accessTokenPayload = payload; | ||
|
||
if (accessToken && payload && store) { | ||
storeAccessToken(payload.scope, accessToken); | ||
storeCurrentAccessToken(accessToken); | ||
} | ||
|
||
this.notifyAccessTokenSubscribers(); | ||
} | ||
|
||
private notifyRefreshTokenSubscribers(): void { | ||
this.refreshTokenSubscribers.forEach((callback) => | ||
callback(this.refreshTokenPayload), | ||
); | ||
} | ||
|
||
private notifyAccessTokenSubscribers(): void { | ||
this.accessTokenSubscribers.forEach((callback) => | ||
callback(this.accessTokenPayload), | ||
); | ||
} | ||
} | ||
|
||
export const tokenState = new TokenState(); |
Oops, something went wrong.