1

I'd like to be able to store custom data in the JSON object returned by the auth.login - AngularFire2 method

After authentication using the email/password method in Ionic2 - AngularFire2 library.

signin(credentials) {
        return this.af.auth.login(credentials);
    }

The this.af.auth.login method returns a json object containing the below data.

 {
      "auth": {
        "uid": "d901fffb-********8099",
        "displayName": null,
        "photoURL": null,
        "email": "****@gmail.com",
        "emailVerified": false,
        "isAnonymous": false,
        "providerData": [
          {
            "uid": "*****@gmail.com",
            "displayName": null,
            "photoURL": null,
            "email": "****@gmail.com",
            "providerId": "password"
          }
        ],
        "apiKey": "AB*********",
        "appName": "[DEFAULT]",
        "authDomain": "******",
        "stsTokenManager": {
          "apiKey": "***********",
          "refreshToken": "ADDl5SEJAWQ90BDG8q1uN6_mn2QYpkMLALUog1ax8lkkmHSQp3jM6CE9BM34ur59Es99rlPnuNM5dsIGAqPZQ3HXRYXpmvr4Jy5UHLgyQEgSg9NydfjXeRuTgajnsPu5bGlJqt679hl_Fv-y_7LIXewW0fr9tzjyH3ur3-_QoXNT99wHazLIwqD4CdsKI95Ka64CI4O12PikH6Huti4vqzxdXy6jq-8jTA",
          "accessToken": "eyJhbGciOiJSUzI1NiIsImtpZCI6Ijk5ZGI1MDc4ZDRkOWUyMTAyZDg1NzA0MWMzMjExNjZiZGE3OGUxZjAifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vcm9hZGJhbmd2YXBwIiwiYXVkIjoicm9hZGJhbmd2YXBwIiwiYXV0aF90aW1lIjoxNDg0MTk1MjY3LCJ1c2VyX2lkIjoiZDkwMWZmZmItNzJkMy00YzVhLThjZGEtOGY0MWYyZjQ4MDk5Iiwic3ViIjoiZDkwMWZmZmItNzJkMy00YzVhLThjZGEtOGY0MWYyZjQ4MDk5IiwiaWF0IjoxNDg0MTk1MjY3LCJleHAiOjE0ODQxOTg4NjcsImVtYWlsIjoibWFnbnVzLm1lbHdpbkBnbWFpbC5jb20iLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsImZpcmViYXNlIjp7ImlkZW50aXRpZXMiOnsiZW1haWwiOlsibWFnbnVzLm1lbHdpbkBnbWFpbC5jb20iXX0sInNpZ25faW5fcHJvdmlkZXIiOiJwYXNzd29yZCJ9fQ.tyqjKCJ_QhBGTRw9ji2iq8bbwmyT4i5vt4MsYpoQHwgicew9PWV1OKoGCHQho0l5ilGHkUeXrDAg3xD7_RHhM_CLNnqNJl-m76-F3uYM1AEjzLMPd2j6q6aO6m3E3fWSa5t9rJYtvuCNaKexpAB9g_l_HTOjNsX4E9XNCB5-v0_vjt9uYPsHM5G-yjuOxru-FJYhNwCCpN3rN97XzP9vkBhWSCQTCHBM3yS4xKCgzMeUb909OUxZK4ZrQFLP0BYPB3U1izmYlSdWd_tuLHm5GMqmerdXf08W_737UpOHOZuLls2pO9RVok-FjYAji-wxQTVEI0hq-hr7v1-mCqiIOw",
          "expirationTime": 1484198865797
        },
        "redirectEventId": null
      },
      "uid": "d901f***********8099",
      "provider": 4
    }

What I am trying to do is add in fields like Company Name, Mobile No, Landline No...and a few such fields to the above json object returned.

So auth.login json promise call stores the profile data, rather than me having to store additional data in the firebase database separately.

Any help will be much appreciated.

my system information as given below:

Cordova CLI: 6.4.0 
Ionic Framework Version: 2.0.0-rc.3
Ionic CLI Version: 2.1.18
Ionic App Lib Version: 2.1.9
Ionic App Scripts Version: 0.0.48
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Linux 4.4
Node Version: v6.9.0
Xcode version: Not installed

Using Angularfire2 and firebase 3

AL.
  • 36,815
  • 10
  • 142
  • 281
Magnus Melwin
  • 1,509
  • 1
  • 21
  • 32

2 Answers2

1

According to https://docs.ionic.io/services/auth/#storing-data

it should be as simple as

auth.user.set('birthdate', '5/17/1985');

though note that you will have to import the cloud auth service

EDIT: 3/13 Ah, realized I forgot to note you must also call

auth.user.save()

after setting all fields and you can call .load() to fetch the freshest data.

babycakes
  • 547
  • 7
  • 21
0

You can also try it like this, without the auth.user I call only user

this.user.set('birthday', '3/04/1999');
this.user.set('username', 'JaneDoe');
this.user.save();