Pushing user data from successful registration to firebase DB against user unique id -
to install use command - npm i firebase @angular/fire --save
---------------------------------------------------------------------------
// Initialize firebase -
firebase.config.ts
var config = {
apiKey: "XXXXXXXXXXXXXXXXXXXXX",
authDomain: "XXXXXXXXXXXXXXXXXX",
databaseURL: "XXXXXXXXXXXXXXXXX",
projectId: "XXX-22ba4",
storageBucket: "XXXXXXXXX-XXXXX.appspot.com",
messagingSenderId: "1022661456269"
};
initialize app using - AngularFireModule.initializeApp(firebaseConfig)
into your app.module.ts imported from @angular/fire
app.module.ts -
import { AngularFireModule } from '@angular/fire';
import { AngularFireAuthModule } from '@angular/fire/auth';
import { AngularFireDatabaseModule } from '@angular/fire/database';
@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [
BrowserModule,
IonicModule.forRoot(),
AppRoutingModule,
AngularFireModule.initializeApp(firebaseConfig),
AngularFireAuthModule,
AngularFireDatabaseModule
]
--------------------------------------------------------------
user.config.ts -
export interface Profile {
firstName:string,
lastName:string,
picture:string
}
export interface User{
email:string,
password:string
}
----------------------------------------------------------------
home.page.ts
user ={} as User;
profile ={} as Profile;
async registerUser(){
try{
this.fireAuth.auth.createUserAndRetrieveDataWithEmailAndPassword(this.user.email, this.user.password).then(response =>{
if(response.user.uid)
this.fireDatabase.object(`${response.user.uid}`).set(this.profile).then(response =>{
console.log('User data is pushed.', response);
this.navCtrl.navigateRoot('home');
});
else
throw 'Failed to store user data';
})
}catch(e){
console.log('some error', e);
}
}
hope this may help.