【问题标题】:Authentication Email/Password with Firebase sdk3 and Angularfire2使用 Firebase sdk3 和 Angularfire2 验证电子邮件/密码
【发布时间】:2016-07-13 18:24:27
【问题描述】:

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

我确实找到了一些 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. 忽略编译器错误并运行程序会导致错误TypeError: this._af.auth is not a function

感谢您的帮助!

#AskFirebase

【问题讨论】:

    标签: angular firebase firebase-authentication angularfire2


    【解决方案1】:

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

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

    其中 _fireAuth 是 FirebaseAuth 类型。

    【讨论】:

      猜你喜欢
      • 2017-06-16
      • 1970-01-01
      • 2020-10-19
      • 2021-11-10
      • 2019-06-11
      • 1970-01-01
      • 2018-09-03
      • 2016-02-11
      • 2021-11-21
      相关资源
      最近更新 更多