Skip to content

Commit

Permalink
Merge pull request #320 from jetstreamapp/bug/312
Browse files Browse the repository at this point in the history
Download file - authorize google does not allow folder selection
  • Loading branch information
paustint authored Apr 21, 2023
2 parents 6922d5b + 75dd25d commit 841297a
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions libs/shared/ui-utils/src/lib/hooks/useGoogleApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function useGoogleApi({ clientId, scopes = [SCOPES['drive.file']] }: Goog
| undefined
>();
const tokenExpiration = useRef<Maybe<Date>>(_tokenExpiration);
const [currentTokenExpiration, setCurrentTokenExpiration] = useState<Maybe<Date>>(tokenExpiration.current);
const [gapiScriptLoaded, gapiScriptLoadError] = useInjectScriptGapi();
const [gisScriptLoaded, gisScriptLoadError] = useInjectScriptGis();
const [loading, setLoading] = useState(!_apiLoaded);
Expand Down Expand Up @@ -75,6 +76,7 @@ export function useGoogleApi({ clientId, scopes = [SCOPES['drive.file']] }: Goog
if (response.error !== undefined) {
_tokenExpiration = null;
tokenExpiration.current = _tokenExpiration;
setCurrentTokenExpiration(tokenExpiration.current);
setError(response.error);
if (tokenCallback.current) {
tokenCallback.current.reject(response);
Expand All @@ -83,6 +85,7 @@ export function useGoogleApi({ clientId, scopes = [SCOPES['drive.file']] }: Goog
} else {
_tokenExpiration = addSeconds(new Date(), Number(response.expires_in));
tokenExpiration.current = _tokenExpiration;
setCurrentTokenExpiration(tokenExpiration.current);
setError(null);
if (tokenCallback.current) {
tokenCallback.current.resolve(response);
Expand Down Expand Up @@ -136,8 +139,8 @@ export function useGoogleApi({ clientId, scopes = [SCOPES['drive.file']] }: Goog
}, [rollbar]);

const isTokenValid = useCallback(() => {
return !!tokenClient.current && !!tokenResponse.current && !!tokenExpiration.current && isAfter(tokenExpiration.current, new Date());
}, []);
return !!tokenClient.current && !!tokenResponse.current && !!currentTokenExpiration && isAfter(currentTokenExpiration, new Date());
}, [currentTokenExpiration]);

const getToken = useCallback(() => {
return new Promise<google.accounts.oauth2.TokenResponse>((resolve, reject) => {
Expand All @@ -163,6 +166,7 @@ export function useGoogleApi({ clientId, scopes = [SCOPES['drive.file']] }: Goog
_tokenResponse = tokenResponse.current;
_tokenExpiration = null;
tokenExpiration.current = _tokenExpiration;
setCurrentTokenExpiration(tokenExpiration.current);
}, []);

return {
Expand Down

0 comments on commit 841297a

Please sign in to comment.