【发布时间】:2018-07-26 16:59:19
【问题描述】:
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { User } from './../classes/user';
import { AlertService } from './alert.service';
import { Alert } from './../classes/alert';
import { AlertType } from './../enums/alert-type.enum';
import { Observable } from 'rxjs';
import 'rxjs/add/Observable/of';
import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFirestore, AngularFirestoreDocument } from 'angularfire2/firestore';
import 'rxjs/add/operator/switchMap';
import { from } from 'rxjs';
import 'rxjs/add/observable/fromPromise';
import { AngularFireAuthModule } from 'angularfire2/auth';
@Injectable({
providedIn: 'root'
})
export class AuthService {
public currentUser: Observable<User | null>;
constructor(
private router: Router,
private alertService: AlertService,
private afAuth: AngularFireAuth,
private db: AngularFirestore
) {
// TODO fetch the user from the Firebase backend, then set the user(actioned!)
this.currentUser = this.afAuth.authState
.switchMap((user) => {
if (user) {
return this.db.doc<User>(`users/${user.uid}`).valueChanges();
} else {
return Observable.of(null);
}
});
}
public signup(firstName: string, lastName: string, email: string, password: string): Observable<boolean> {
// TODO call Firebase signup function(actioned!)
return Observable.fromPromise(
this.afAuth.auth.createUserWithEmailAndPassword(email, password)
.then((user) => {
const userRef: AngularFirestoreDocument<User> = this.db.doc(`users/${user.uid}`);
const updatedUser = {
id: user.uid,
email: user.email,
firstName,
lastName,
photoUrl: 'https://avatarfiles.alphacoders.com/131/131718.jpg'
};
userRef.set(updatedUser);
return true;
})
.catch((err) => false)
);
return Observable.of(true);
}
public login(email: string, password: string): Observable<boolean> {
// TODO call Firebase login function
return Observable.of(true);
}
public logout(): void {
// TODO call Firebase logout function
this.router.navigate(['/login']);
this.alertService.alerts.next(new Alert('You have been signed out.'));
}
}
嗨 - 我希望有人可以帮助我解决以下问题 - 我收到一个错误,如图所示,这是错误来自的一段代码。我是 Angular 和 Firebase 的新手,仍在学习它们的工作原理..
基本上-我正在尝试创建一个聊天应用程序-代码在说-我创建了一个带有电子邮件和密码的用户,它将解析来自导入的电子邮件和密码。
src/app/services/auth.service.ts(47,85) 中的错误:错误 TS2339:“UserCredential”类型上不存在属性“uid”。 src/app/services/auth.service.ts(49,22):错误 TS2339:“UserCredential”类型上不存在属性“uid”。 src/app/services/auth.service.ts(50,25):错误 TS2339:“UserCredential”类型上不存在属性“email”。 src/app/services/auth.service.ts(60,5):错误 TS7027:检测到无法访问的代码。
一切正常,除了这些错误让我发疯!
【问题讨论】:
-
您必须为
UserCredential发布您的类型定义。这是 Typescript 编译错误,而不是运行时错误。此外,您的return Observable.of(true)无法访问,因为您已经返回了Observable.fromPromise()
标签: angular firebase firebase-authentication angularfire2