Skip to content

Instantly share code, notes, and snippets.

@luizhfraraujo
Created July 11, 2019 20:02
Show Gist options
  • Save luizhfraraujo/4c333a952c0b300797adaf3d8f4e7390 to your computer and use it in GitHub Desktop.
Save luizhfraraujo/4c333a952c0b300797adaf3d8f4e7390 to your computer and use it in GitHub Desktop.
Create user on firebase from another user.
import { Injectable } from "@angular/core";
import { User } from "../models/user.model";
import { AngularFireList, AngularFireDatabase } from "@angular/fire/database";
import * as firebase from "firebase";
import { UserService } from "./user.service";
import { AngularFireAuth } from "@angular/fire/auth";
import { environment } from "../../environments/environment";
@Injectable({
providedIn: "root"
})
@Injectable()
export class UserManagementService {
private dbPath = "/users";
currentUser: User;
usersRef: AngularFireList<any>;
constructor(
private afDb: AngularFireDatabase,
private userService: UserService,
private afAuth: AngularFireAuth
) {
this.userService.getCurrentUser().then(user => {
this.currentUser = user;
});
this.usersRef = afDb.list(this.dbPath);
}
create(user: User, credentials): Promise<any> {
return new Promise<any>((resolve, reject) => {
const authApp = firebase.initializeApp(
environment.firebase,
"CreateUserApp"
);
authApp
.auth()
.createUserWithEmailAndPassword(credentials.email, credentials.password)
.then(
res => {
user.key = res.user.uid;
this.usersRef.update(res.user.uid, user).then(
resRef => {
authApp.auth().signOut();
resolve(resRef);
authApp.delete();
},
errRef => {
reject(errRef);
authApp.delete();
}
);
},
err => reject(err)
);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment