Skip to content

Instantly share code, notes, and snippets.

@sunnymopada
Created May 10, 2021 14:33
Show Gist options
  • Save sunnymopada/88ae297d3a3387034b518ccaba848324 to your computer and use it in GitHub Desktop.
Save sunnymopada/88ae297d3a3387034b518ccaba848324 to your computer and use it in GitHub Desktop.
lass AuthStore {
refreshAuthTokensPromise:Type
authService:Type
constructor(authService) {
this.authService = authService
this.initAuthStore()
}
@action.bound
initAuthStore(): void {
this.refreshAuthTokensPromise = undefined
}
@action.bound
setRefreshAccessTokens(response: Tokens): void {
//Other code
this.refreshAuthTokensPromise = undefined
}
@action.bound
refreshAuthTokensAPI(
requestObject: RefreshAuthTokensRequest,
onSuccess: Function = (): void => {},
onFailure: Function = (): void => {}
): any {
if (this.refreshAuthTokensPromise) {
return this.refreshAuthTokensPromise
}
this.refreshAuthTokensPromise = this.authService.refreshAuthTokensAPI(
requestObject
)
return bindPromiseWithOnSuccess(this.refreshAuthTokensPromise)
.to(this.setRefreshAccessTokensStatus, response => {
this.setRefreshAccessTokens(response as Tokens)
onSuccess()
})
.catch(err => {
this.setRefreshAccessTokensError(err)
onFailure()
})
}
}
export default AuthStore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment