Last active
May 21, 2020 11:22
-
-
Save usmansbk/a33c92bdbfa7417a5b7a42167f0e9980 to your computer and use it in GitHub Desktop.
react-native Google refresh token handler using react-native-google-signin npm package
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { GoogleSignin } from 'react-native-google-signin'; | |
export default class GoogleOAuth { | |
refreshGoogleToken = () => { | |
return this._refreshGoogleTokenImpl(); | |
} | |
_refreshGoogleTokenImpl = () => { | |
return new Promise(async (res, rej) => { | |
const isSignedIn = await GoogleSignin.isSignedIn(); | |
if (isSignedIn) { | |
console.debug('refreshing the google access token'); | |
GoogleSignin.signInSilently() | |
.then(async (authResponse) => { | |
const { idToken, accessTokenExpirationDate } = authResponse; | |
res({ | |
token: idToken, | |
expires_at: accessTokenExpirationDate | |
}); | |
}).catch(error => { | |
console.debug('Failed to sign in with Google', error); | |
rej('Failed to sign in with Google'); | |
}); | |
} else { | |
console.debug('User is not signed in with Google'); | |
rej('Failed to refresh google token'); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
HOW TO USE
In App.js
import Amplify, { Auth } from 'aws-amplify';
import GoogleOAuthClass from '<path to file>/GoogleOAuth.js';
import aws_config from './src/aws-exports';
const GoogleOAuth = new GoogleOAuthClass();
Amplify.configure(aws_config);
Auth.configure({
refreshHandlers: {
'google': GoogleOAuth.refreshGoogleToken
}
});