From ed8d012a4b42d9f30e44f26960a1879bc4145e1d Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Sun, 19 Aug 2018 20:01:04 -0600 Subject: [PATCH] Fixed rpc not connecting after login --- client/state/AppState.ts | 1 + client/state/HttpApi.ts | 33 ++++++++++++++++++--------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/client/state/AppState.ts b/client/state/AppState.ts index 269fdfd..5477dd3 100644 --- a/client/state/AppState.ts +++ b/client/state/AppState.ts @@ -29,6 +29,7 @@ export default class AppState extends TypedEventEmitter { super(); this.sprinklersRpc.on("newUserData", this.userStore.receiveUserData); this.sprinklersRpc.on("tokenError", this.checkToken); + this.httpApi.on("tokenGranted", () => this.emit("hasToken")); this.httpApi.on("tokenError", this.checkToken); this.on("checkToken", this.doCheckToken); diff --git a/client/state/HttpApi.ts b/client/state/HttpApi.ts index d5715d4..b70aa4f 100644 --- a/client/state/HttpApi.ts +++ b/client/state/HttpApi.ts @@ -1,14 +1,16 @@ +import { action } from "mobx"; + import { TokenStore } from "@client/state/TokenStore"; import ApiError from "@common/ApiError"; import { ErrorCode } from "@common/ErrorCode"; import { TokenGrantPasswordRequest, TokenGrantRefreshRequest, TokenGrantResponse } from "@common/httpApi"; import log from "@common/logger"; import { DefaultEvents, TypedEventEmitter } from "@common/TypedEventEmitter"; -import { runInAction } from "mobx"; export { ApiError }; interface HttpApiEvents extends DefaultEvents { + tokenGranted(response: TokenGrantResponse): void; error(err: ApiError): void; tokenError(err: ApiError): void; } @@ -39,6 +41,8 @@ export default class HttpApi extends TypedEventEmitter { this.emit("tokenError", err); } }); + + this.on("tokenGranted", this.onTokenGranted); } async makeRequest(url: string, options?: RequestInit, body?: any): Promise { @@ -82,13 +86,7 @@ export default class HttpApi extends TypedEventEmitter { const response: TokenGrantResponse = await this.makeRequest("/token/grant", { method: "POST", }, request); - runInAction("grantPasswordSuccess", () => { - this.tokenStore.accessToken.token = response.access_token; - this.tokenStore.refreshToken.token = response.refresh_token; - this.tokenStore.saveLocalStorage(); - }); - const { accessToken } = this.tokenStore; - log.debug({ aud: accessToken.claims!.aud }, "got password grant tokens"); + this.emit("tokenGranted", response); } async grantRefresh() { @@ -102,12 +100,17 @@ export default class HttpApi extends TypedEventEmitter { const response: TokenGrantResponse = await this.makeRequest("/token/grant", { method: "POST", }, request); - runInAction("grantRefreshSuccess", () => { - this.tokenStore.accessToken.token = response.access_token; - this.tokenStore.refreshToken.token = response.refresh_token; - this.tokenStore.saveLocalStorage(); - }); - const { accessToken } = this.tokenStore; - log.debug({ aud: accessToken.claims!.aud }, "got refresh grant tokens"); + this.emit("tokenGranted", response); + } + + @action.bound + private onTokenGranted(response: TokenGrantResponse) { + this.tokenStore.accessToken.token = response.access_token; + this.tokenStore.refreshToken.token = response.refresh_token; + this.tokenStore.saveLocalStorage(); + const { accessToken, refreshToken } = this.tokenStore; + log.debug({ + accessToken: accessToken.claims, refreshToken: refreshToken.claims, + }, "got new tokens"); } }