使用Firebase sdk3和Angularfire2验证电子邮件/密码

时间:2016-07-13 18:24:27

标签: angular firebase firebase-authentication angularfire2

我是Firebase的新手,但我想在我目前的angular2项目中使用它。我使用Angularfire2进行电子邮件密码验证。据我了解API,您必须调用signInWithEmailAndPassword(email, password)对象的firebase.auth.Auth方法。不过,我不知道如何获得这个'Auth'的实例。 我确实找到了example进行身份验证。但该示例不使用电子邮件/密码身份验证。我找不到任何关于如何以正确的方式调用上述方法的示例。

我找到了一些JS-Examples。在那里,'Auth'对象以某种方式注入。这似乎不像我尝试的那样工作。这是我到目前为止(不工作):

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);
}
...

我的应用甚至没有开始 - &gt;错误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.忽略编译器错误并运行程序会导致错误TypeError: this._af.auth is not a function

感谢您的帮助!

#AskFirebase

1 个答案:

答案 0 :(得分:2)

好吧,我仍然没有找到如何使用signInWithEmailAndPassword方法。我设法用电子邮件/密码登录。你可以使用

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

其中_fireAuth的类型为FirebaseAuth。