如何使用角度4设置Firebase用户的displayName?

时间:2017-11-22 00:52:03

标签: angular firebase firebase-authentication angularfire

我一直在努力为使用电子邮件和密码注册的用户获取 displayName 的价值,它总是给我null

这是我的 AuthService



@Injectable()
export class AuthService {

  authState: any = null;

 user$: Observable<firebase.User>;
  constructor(private afAuth: AngularFireAuth,
              private db: AngularFireDatabase,
              private route: ActivatedRoute,
              private userService: UserService) {
    this.user$ = this.afAuth.authState;
    this.afAuth.authState.subscribe((auth) => {
      this.authState = auth;
    });

  }
  
  signUp(email, password) {
    this.afAuth.auth.createUserWithEmailAndPassword(email,      password)
      .then((user) => {
        this.authState = user;
    })
      .catch(error => console.log(error));
  }
  
   private updateUserData(): void {

    const path = `users/${this.currentUserId}`; // Endpoint on firebase
    const data = {
      email: this.authState.email,
      name: this.authState.displayName
    };

    this.db.object(path).update(data)
      .catch(error => console.log(error));

  }
&#13;
&#13;
&#13;

这是我的注册。

&#13;
&#13;
export class SignupComponent implements OnInit {

  constructor(private auth: AuthService,
              private user: UserService) { }

  ngOnInit() {
  }
  onSignup(form: NgForm) {
     const user = firebase.auth().currentUser;
      user.updateProfile({
      displayName: form.value.username,
      photoURL: 'https://example.com/jane-q-user/profile.jpg'
    })
      .then(function(response) {
        const displayName = user.displayName;
    }, function(error) {
      console.log(error);
    });

    const name = form.value.username;
    const email = form.value.email;
    const password = form.value.password;
    this.auth.signUp(email, password);
&#13;
&#13;
&#13;

我不知道如何使用从signUp表单中获取的名称,并将其设置为displayName以获取 firebase.user

0 个答案:

没有答案