首页 文章

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

提问于
浏览
2

我是Firebase的新手,但我想在我目前的angular2项目中使用它 . 我使用Angularfire2进行电子邮件密码验证 . 据我所知,你必须调用 firebase.auth.Auth 对象的 signInWithEmailAndPassword(email, password) 方法 . 不过,我不是't know how to get an instance of this ' 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);
}
...

我的应用程序甚至没有启动 - >错误 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 回答

  • 2

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

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

    其中_fireAuth的类型为FirebaseAuth .

相关问题