Authentication Providers: Apple and Google Refresh Access Token

How to refresh the access token for third party authentication providers like Apple and Google ?

My ios app successfully logs in a user using an Apple Sign In or Google, but after a day the access token expires and user gets logged out which means the user has to manually sign in again.

I used the Swift example below to refresh the access token, but when I sign in using the refreshed access token it fails to sign me back in using Apple or Google authentication.

func getValidAccessToken(user: User) async throws → String {
// An already logged in user’s access token might be stale. To
// guarantee that the token is valid, refresh it if necessary.
try await user.refreshCustomData()
return user.accessToken!
}

Can anyone please offer any advice or suggestions?