Skip to content

Instantly share code, notes, and snippets.

@usmansbk
Last active May 21, 2020 11:22
Show Gist options
  • Save usmansbk/a33c92bdbfa7417a5b7a42167f0e9980 to your computer and use it in GitHub Desktop.
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
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');
}
});
}
}
@usmansbk
Copy link
Author

usmansbk commented May 27, 2019

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
}
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment