2

I am new to Firebase, but I want to use it in my current angular2 project. I am stuck with Email-Password-Authentication using Angularfire2. As far as I do understand the API you have to call the signInWithEmailAndPassword(email, password) method of the firebase.auth.Auth object. I don't know how to get an instance of this 'Auth', though. I did find an example for authentication in general. But the example does not use email/password authentication. I cannot find any example on how to call the aforementioned method in the right way.

I did find some JS-Examples. There, the 'Auth'-Object is injected somehow. This does not seem to work the way I tried it. This is what i got so far (not working):

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

My App does not even start -> Error Cannot resolve all parameters for 'AuthService'(FirebaseAuth, AngularFire, ?). If I try to use this._af.auth().signInWithEmailAndPassword(email, password) the Typescript compiler complains: error TS2349: Cannot invoke an expression whose type lacks a call signature. Ignoring the compiler error and running the program results in an error TypeError: this._af.auth is not a function.

Thank you for your help!

#AskFirebase

1 Answer 1

2

Well, i still didnt find out how to use the signInWithEmailAndPassword method. I managed to login with email/password, though. You can just use

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

where _fireAuth is of type FirebaseAuth.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.