Skip to content

Commit

Permalink
Refactor: Change promise to callback
Browse files Browse the repository at this point in the history
  • Loading branch information
diavrank committed Sep 1, 2021
1 parent 9120df4 commit 7ef8346
Showing 1 changed file with 47 additions and 49 deletions.
96 changes: 47 additions & 49 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,57 @@ import { GoogleSignin } from '@react-native-google-signin/google-signin';
/**
* Login with Google using Meteor Accounts and React Native
* @param configuration Options for the configure method of GoogleSignin.
* @param callback function to see if there is error
* @returns {Promise<void> | Promise.Promise}
*/
Meteor.loginWithGoogle = function(configuration) {
return new Promise(async(resolve, reject) => {
try {
GoogleSignin.configure(configuration);
await GoogleSignin.hasPlayServices({
showPlayServicesUpdateDialog: true
});
let userInfo;
const isSignedIn = await GoogleSignin.isSignedIn();
if (!isSignedIn) {
userInfo = await GoogleSignin.signIn();
if (!userInfo) {
reject({ reason: 'Something went wrong obtaining user info', details: { userInfo } });
return;
}
} else {
userInfo = await GoogleSignin.signInSilently();
if (!userInfo) {
reject({ reason: 'Something went wrong obtaining user info', details: { userInfo } });
return;
}
Meteor.loginWithGoogle = async function(configuration, callback) {
try {
GoogleSignin.configure(configuration);
await GoogleSignin.hasPlayServices({
showPlayServicesUpdateDialog: true
});
let userInfo;
const isSignedIn = await GoogleSignin.isSignedIn();
if (!isSignedIn) {
userInfo = await GoogleSignin.signIn();
if (!userInfo) {
callback({ reason: 'Something went wrong obtaining user info', details: { userInfo } });
return;
}
} else {
userInfo = await GoogleSignin.signInSilently();
if (!userInfo) {
callback({ reason: 'Something went wrong obtaining user info', details: { userInfo } });
return;
}
const tokens = await GoogleSignin.getTokens();
Meteor._startLoggingIn();
Meteor.call(
'login',
{
googleSignIn: true,
accessToken: tokens.accessToken,
refreshToken: undefined,
idToken: tokens.idToken,
serverAuthCode: userInfo.serverAuthCode,
email: userInfo.user.email,
imageUrl: userInfo.user.photo,
userId: userInfo.user.id
},
(error, response) => {
if (error) {
GoogleSignin.revokeAccess();
GoogleSignin.signOut();
reject(error);
}
Meteor._endLoggingIn();
Meteor._handleLoginCallback(error, response);
resolve();
}
);
} catch (error) {
reject({ reason: 'Error in Google Signing', details: error });
}
});
const tokens = await GoogleSignin.getTokens();
Meteor._startLoggingIn();
Meteor.call(
'login',
{
googleSignIn: true,
accessToken: tokens.accessToken,
refreshToken: undefined,
idToken: tokens.idToken,
serverAuthCode: userInfo.serverAuthCode,
email: userInfo.user.email,
imageUrl: userInfo.user.photo,
userId: userInfo.user.id
},
(error, response) => {
if (error) {
GoogleSignin.revokeAccess();
GoogleSignin.signOut();
}
Meteor._endLoggingIn();
Meteor._handleLoginCallback(error, response);
typeof callback == 'function' && callback(error);
}
);
} catch (error) {
callback({ reason: 'Error in Google Signing', details: error });
}
};

Meteor.logoutFromGoogle = function() {
Expand Down

0 comments on commit 7ef8346

Please sign in to comment.