Did I understand the user auth().signInWithCredential correctly? #5654
-
I'm using the following to do Google Sign In now, basically I need only information of the user's name and id.
However from the social-auth (google), it shows that I should sign in with So in short, my understanding is..
Did I understand correctly? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
I think what you describe is correct. This is what I do: async googleSignIn(link?: boolean): Promise<boolean> {
try {
await GoogleSignin.hasPlayServices();
GoogleSignin.configure({
webClientId: AppConfig.getGoogleWebClientId(),
offlineAccess: true,
});
const data = await GoogleSignin.signIn();
console.log('UserStore::googleSignIn - user result is:', JSON.stringify(data, null, 2));
this.removeUserChangeListener();
const providers = await this.getProvidersForEmail(data.user.email);
console.log(
'UserStore::googleSignIn - got providers for email: ',
JSON.stringify(providers, null, 2)
);
// if google.com is not in the providers, it is first login via google.
let googleProvider = false;
let passwordProvider = false;
for (let i = 0; i < providers.length; i++) {
if (providers[i] === 'google.com') {
googleProvider = true;
} else if (providers[i] === 'password') {
passwordProvider = true;
}
}
// If there are providers, but not google, google actually just automatically does it right.
// That doesn't work for facebook though
if (providers.length !== 0 && !googleProvider) {
if (passwordProvider) {
console.log(
'UserStore::googleSignIn - other providers but google auto-connects if it is password provider'
);
} else {
this.handleCredentialInUse();
return Promise.resolve(false);
}
}
// If there are no other providers we should ask if they already have an account
if (
providers.length === 0 &&
(!firebase.auth().currentUser?.providerData ||
firebase.auth().currentUser?.providerData.length === 0)
) {
console.log('UserStore::googleSignIn - no other providers. See if they are sure');
const choice = await UserStore.firstLoginDialog();
if (choice === 'Other Accounts') {
console.log('UserStore::googleSignIn - user wants to handle other accounts');
return Promise.resolve(false);
}
console.log(
'UserStore::googleSignIn - user is just fine continuing and creating new account'
);
this.setIsNewUser(true);
}
// create a new firebase credential with the token
const credential = firebase.auth.GoogleAuthProvider.credential(
data.idToken
// data.accessToken
);
console.log('UserStore::googleSignIn - credential was', JSON.stringify(credential, null, 2));
let firebaseUserCredential;
if (!link) {
// login with credential
firebaseUserCredential = await firebase.auth().signInWithCredential(credential);
Analytics.setAnalyticsUser(firebaseUserCredential.user.uid); // TODO, set our own ID?
Analytics.setAnalyticsUserProperties({ email: firebaseUserCredential.user.email });
Analytics.analyticsEvent('successGoogleSignIn');
} else {
const { currentUser } = firebase.auth();
if (!currentUser) {
return Promise.resolve(false);
}
firebaseUserCredential = await currentUser.linkWithCredential(credential);
Analytics.setAnalyticsUser(currentUser.uid); // TODO, set our own ID?
Analytics.setAnalyticsUserProperties({ email: currentUser.email });
Analytics.analyticsEvent('successGoogleLink');
}
const { currentUser } = firebase.auth();
if (firebaseUserCredential.additionalUserInfo?.profile?.picture && currentUser) {
try {
await currentUser.updateProfile({
photoURL: firebaseUserCredential.additionalUserInfo?.profile?.picture,
});
} catch (e) {
console.log('UserStore::googleSignIn - error setting photoURL?', e);
}
}
console.log(
'UserStore::googleSignIn - firebaseCredential was',
JSON.stringify(firebaseUserCredential, null, 2)
);
this.userChangedHandler(
firebaseUserCredential.user,
firebaseUserCredential.additionalUserInfo
);
return Promise.resolve(true);
} catch (error) {
if (error.code === statusCodes.SIGN_IN_CANCELLED) {
// user cancelled the login flow
console.log('UserStore::googleSignIn - user cancelled');
} else if (error.code === statusCodes.IN_PROGRESS) {
// operation (e.g. sign in) is in progress already
console.log('UserStore::googleSignIn - signin already in progress');
} else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
// play services not available or outdated
console.log('UserStore::googleSignIn - play services not available');
} else if (
error.code === 'auth/email-already-in-use' ||
error.code === 'auth/credential-already-in-use'
) {
console.log('UserStore::googleSignIn - email already in use, instruct on unlink/delete');
this.handleAccountInUse();
} else if (error.code === 'auth/account-exists-with-different-credential') {
this.handleCredentialInUse();
} else {
// some other error happened
console.log('UserStore::googleSignIn - unknown error?', error);
RX.Alert.show(
I18NService.translate('ConnectedAccountsConnectionError'),
I18NService.translate(error.code)
);
}
} finally {
this.addUserChangedListener();
}
return Promise.resolve(false);
} So many different failure modes, it's crazy 😅 |
Beta Was this translation helpful? Give feedback.
I think what you describe is correct. This is what I do: