使用 Firebase sdk3 和 Angularfire2 进行身份验证 Email/Password

Authentication Email/Password with Firebase sdk3 and Angularfire2

我是 Firebase 的新手,但我想在我当前的 angular2 项目中使用它。我坚持使用 Angularfire2 的电子邮件密码身份验证。据我所知,API 你必须调用 firebase.auth.Auth 对象的 signInWithEmailAndPassword(email, password) 方法。不过,我不知道如何获取此 'Auth' 的实例。 一般来说,我确实找到了一个 example 用于身份验证。但是示例没有使用 email/password 身份验证。我找不到任何有关如何以正确方式调用上述方法的示例。

我确实找到了一些 JS 示例。在那里,'Auth'-Object 以某种方式被注入。这似乎不像我尝试的那样有效。这是我到目前为止得到的(不工作):

main.ts

bootstrap(AppComponent, [..., FIREBASE_PROVIDERS,
defaultFirebase({
    apiKey: "<>",
    authDomain: "<>",
    databaseURL: "<>",
    storageBucket: "<>",
}),firebaseAuthConfig({
    provider: AuthProviders.Password,
    method: AuthMethods.Password
}), AuthService])
.catch(err => console.error(err));

auth.service.ts:

@Injectable()
export class AuthService{

private _authState: FirebaseAuthState = null;

constructor(private _fireAuth:FirebaseAuth, private _af:AngularFire, private _auth:Auth){
    _fireAuth.subscribe( (state:FirebaseAuthState) =>{
        this._authState = state;
    });
}

get authenticated():boolean{
    return this._authState !== null;
}

get id(): string {
    return this.authenticated ? this._authState.uid : '';
}

signIn(email:string, password:string):Promise<User>{
    return this._auth.signInWithEmailAndPassword(email, password);
}
...

我的应用程序甚至无法启动 -> 错误 Cannot resolve all parameters for 'AuthService'(FirebaseAuth, AngularFire, ?)。 如果我尝试使用 this._af.auth().signInWithEmailAndPassword(email, password),Typescript 编译器会抱怨:error TS2349: Cannot invoke an expression whose type lacks a call signature. Igning the compiler error and 运行 the program results in an error TypeError: this._af.auth is not a function.

感谢您的帮助!

#AskFirebase

嗯,我仍然没有找到如何使用 signInWithEmailAndPassword 方法。不过,我设法使用 email/password 登录。你可以只使用

signIn(email:string, password:string){
  _fireAuth.login({
    email: email,
    password: password
  });
}

其中 _fireAuth 属于 FirebaseAuth 类型。