Skip to content

Adds Refreshing token if previous token expires, #3

Merged
vikas2-sharma merged 7 commits intomainfrom
feat/event-folder-db
Sep 30, 2025
Merged

Adds Refreshing token if previous token expires, #3
vikas2-sharma merged 7 commits intomainfrom
feat/event-folder-db

Conversation

@vikas2-sharma
Copy link
Copy Markdown
Owner

Implemented feature to obtain new token for Google Drive API before driveService execute, considering previous token expires in 5 mins

This commit introduces the `kotlinx-coroutines-play-services` dependency to the project.

This library provides integration with Google Play Services Tasks API, enabling the use of coroutines with Play Services.

The dependency is added to `gradle/libs.versions.toml` and `core/build.gradle.kts`.
This commit introduces functionality to refresh Google Drive access tokens and manage their expiration within the `AuthManager`.

Key changes:
- A new `getNewToken()` suspend function is added to `AuthManager`. This function requests a new authorization token from Google Identity Services and stores the new access token and its expiration time (5 minutes from now) in `DataStorePref`.
- The `getDrivePermission` function now also saves the token's expiration time (5 minutes from issue) to `DataStorePref` upon successful authorization.
- The `AuthManager` constructor now accepts a `Context` parameter, which is used by the new `getNewToken()` function.
- Logging has been added to trace the token refresh process.
This commit changes the `getExpiresIn` function in `DataStorePref` from returning a `Flow<Long>` to a suspend function returning a `Long`.

The implementation now uses `.first()` on the mapped `DataStore` data flow to retrieve the `EXPIRES_IN_KEY` value directly, simplifying the call site by removing the need to collect the Flow.
This commit modifies the `NetworkModule` to provide the `ApplicationContext` to the `AuthManager`.

The `providesAuthManager` function in `NetworkModule.kt` now accepts `@ApplicationContext context: Context` as a parameter and passes it to the `AuthManager` constructor. This change allows `AuthManager` to access application-level context if needed.
This commit refactors `DriveManager` to enhance token handling and error management when interacting with the Google Drive API.

**Key changes:**

- **Token Refresh Logic:**
    - `getDriveService` now checks the access token's expiry time before making API calls.
    - If the token is expired, it requests a new token via `authManager.getNewToken()`.
    - This ensures that API calls are always made with a valid access token.
- **Error Handling:**
    - `getDriveService`, `createFolder`, `rootFolderId`, and `getEventFolderFromDrive` now include `try-catch` blocks to handle potential exceptions during API interactions.
    - Methods that return a value now return `null` or an empty string/list in case of an error, providing more robust error propagation.
    - `getDriveService` can now return `null` if the Drive service cannot be initialized.
- **Dependency Injection:**
    - `DriveManager` now accepts an `AuthManager` instance in its constructor to facilitate token refresh.
- **Suspend Functions:**
    - `createFolder`, `rootFolderId`, and `getEventFolderFromDrive` are now `suspend` functions to align with the asynchronous nature of network operations and token refresh.
- **Logging:**
    - Added more detailed logging for token status, API calls, and errors.
- **Removed Unused Code:**
    - Commented out `cancelDriveManagerCoroutine` as it's not currently used.
    - Removed unused `ROOT_FOLDER_KEY` import.
- **Null Safety:**
    - `createFolder` now handles potential `null` responses from the Drive API more safely.

These changes improve the reliability and robustness of Drive interactions by proactively managing token lifecycles and gracefully handling API errors.
This commit updates the `DriveManager` to accept an `AuthManager` instance as a dependency.

The `providesDriveManager` function in the Dagger module (`Provider.kt`) has been modified to inject `AuthManager` into `DriveManager` during its construction. This change likely facilitates authentication-related operations within `DriveManager`.
This commit enhances the `EventFolderRepository.getOrCreateProjectFolder` function.

The function now checks if the `rootFolderIfFromPref` retrieved from `dataStorePref` is null or blank. Previously, it only checked for null.

Additionally, debug logs have been added to trace the different execution paths within the function, such as retrieving the folder ID from local storage, Google Drive, or creating a new root folder.
@vikas2-sharma vikas2-sharma merged commit cd497bf into main Sep 30, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant