From eb692ae6e635153979f1cfb9fcb629a0f72bdd7c Mon Sep 17 00:00:00 2001 From: shamsmosowi Date: Sat, 25 Sep 2021 11:16:56 +1000 Subject: [PATCH] option to force refresh token to fix getting firestore rules --- src/components/Setup/Step2ServiceAccount.tsx | 2 ++ src/components/Setup/Step4Rules.tsx | 2 +- src/contexts/AppContext.tsx | 10 ++++++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/components/Setup/Step2ServiceAccount.tsx b/src/components/Setup/Step2ServiceAccount.tsx index 1d863b28..e2532767 100644 --- a/src/components/Setup/Step2ServiceAccount.tsx +++ b/src/components/Setup/Step2ServiceAccount.tsx @@ -39,6 +39,7 @@ export default function Step2ServiceAccount({ setVerificationStatus("LOADING"); try { const result = await checkServiceAccount(rowyRunUrl); + console.log(result); if (result) { setVerificationStatus("IDLE"); setHasAllRoles(true); @@ -162,6 +163,7 @@ export const checkServiceAccount = async ( route: runRoutes.serviceAccountAccess, signal, }); + console.log(res); return Object.values(res).reduce( (acc, value) => acc && value, true diff --git a/src/components/Setup/Step4Rules.tsx b/src/components/Setup/Step4Rules.tsx index f69db860..8114fc38 100644 --- a/src/components/Setup/Step4Rules.tsx +++ b/src/components/Setup/Step4Rules.tsx @@ -40,7 +40,7 @@ export default function Step4Rules({ const [currentRules, setCurrentRules] = useState(""); useEffect(() => { if (rowyRunUrl && !hasRules && !currentRules) - getAuthToken() + getAuthToken(true) .then((authToken) => rowyRun({ rowyRunUrl, diff --git a/src/contexts/AppContext.tsx b/src/contexts/AppContext.tsx index f684c851..fe1d546b 100644 --- a/src/contexts/AppContext.tsx +++ b/src/contexts/AppContext.tsx @@ -25,7 +25,7 @@ interface IAppContext { currentUser: firebase.User | null | undefined; userClaims: Record | undefined; userRoles: string[]; - getAuthToken: () => Promise; + getAuthToken: (forceRefresh?: boolean) => Promise; userDoc: any; theme: keyof typeof themes; themeOverridden: boolean; @@ -61,8 +61,14 @@ export const AppProvider: React.FC = ({ children }) => { // Get user data from Firebase Auth event - const getAuthToken = async () => { + const getAuthToken = async (forceRefresh: boolean = false) => { // check if token is expired + if (currentUser && forceRefresh) { + const res = await currentUser.getIdTokenResult(true); + setAuthToken(res.token as string); + return res.token; + } + if (currentUser && authToken) { const token: any = jwt_decode(authToken); if (token && token.exp * 1000 < Date.now()) {