Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Read User Email Without Prompting

If I install an app on Google Play I notice that it informs me about what permissions the app requires. Once it does it in the app it also prompts me. This is all very fine.

I would like to just read the Google or Apple account e-mail - without prompting the end user for an e-mail.

I don't want to use the email as an auth token, but prefill a "Subscribe to News Letter" field. The user can then toggle ON or OFF (or choose to add another email).

like image 757
Norfeldt Avatar asked Feb 20 '18 10:02

Norfeldt


1 Answers

If I'm getting the question right, You need to get the primary email address of the currently signed in user, which can be used to pre-fill some kind of a form (eg. sign-up form)

For Android

Try the following react native wrapper library react-native-account-manager

Once you have setup using the instructions on the readme from the above link, use the following code to retrieve the list of signed in google accounts.

Please note this could result in an empty list, if there are no google accounts associated with the device

import AccountManager from 'react-native-account-manager';

AccountManager.getAccountsByType('accountName').then((accounts) => {
// console.log('available accounts', accounts);
let [firstAccount] = accounts;

AccountManager.getUserData(firstAccount, 'storedKey').then((storedData) => {
// console.log('stored data for storeKey', storedData);

AccountManager.setUserData(account, 'storedKey', JSON.stringify({foo: "bar"})).then(() => {
  // console.log('data successfully stored');
  })
});
})

For iOS

Bad luck

Apple does NOT expose the iPhone user’s details like their email address, password or credit card details to the app developer.

An alternative is to use the icloud token to create the user. You could use the following guide to do it

For a wrapper on obtaining a iCloud token using react-native use react-native-icloud-user-tokenlibrary

Kudos

like image 193
Ismail Iqbal Avatar answered Oct 20 '22 18:10

Ismail Iqbal