option to force refresh token to fix getting firestore rules

This commit is contained in:
shamsmosowi
2021-09-25 11:16:56 +10:00
parent bbd76d99e3
commit eb692ae6e6
3 changed files with 11 additions and 3 deletions

View File

@@ -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

View File

@@ -40,7 +40,7 @@ export default function Step4Rules({
const [currentRules, setCurrentRules] = useState("");
useEffect(() => {
if (rowyRunUrl && !hasRules && !currentRules)
getAuthToken()
getAuthToken(true)
.then((authToken) =>
rowyRun({
rowyRunUrl,

View File

@@ -25,7 +25,7 @@ interface IAppContext {
currentUser: firebase.User | null | undefined;
userClaims: Record<string, any> | undefined;
userRoles: string[];
getAuthToken: () => Promise<string>;
getAuthToken: (forceRefresh?: boolean) => Promise<string>;
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()) {