Skip to content

5.) User authentification example code broken #640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
daslicht opened this issue Oct 25, 2016 · 4 comments
Closed

5.) User authentification example code broken #640

daslicht opened this issue Oct 25, 2016 · 4 comments

Comments

@daslicht
Copy link

daslicht commented Oct 25, 2016

Hi,
the 5-user-authentication.md consists of the following example code:

Login users

export class AppComponent {
  constructor(public af: AngularFire) {}

 login() {
    this.af.auth.login();
  }

  logout() {
     this.af.auth.logout();
  }
}

We get the following error:

[ts] Property 'af' does not exist on type 'AppComponent'.

Looks like af is not being added automatically to the this scope automatically.

Should it be :

export class AppComponent {
  af: AngularFire;
  title = 'app works!';

  item: FirebaseObjectObservable<any>;

  constructor(af: AngularFire) {
    this.af =  af ;
    this.item = af.database.object('/item');
  }

  login() {
      this.af.auth.login();
    }

  logout() {
     this.af.auth.logout();
  }
}

But there must be a better way to initialize af since the Angualr 2 docs say that it is not good practice to do such things in the constructor and rather use:

  ngOnInit() {
  }

But how to inject public af: AngularFire to ngOnInit()
please?

@gianpaj
Copy link
Contributor

gianpaj commented Oct 25, 2016

I believe instantiating the AngularFire class in the constructor makes sense instead of ngOnInit().

See http://stackoverflow.com/a/35763811/728287

So You should use constructor() to setup Dependency Injection and not much else. ngOnInit() is better place to "start" - it's where/when components' bindings are resolved

@ykoehler
Copy link

What if you add the @component decorator, does it work in the constructor?

@daslicht
Copy link
Author

daslicht commented Oct 25, 2016

What if you add the @component decorator, does it work in the constructor?
? it already woks in the constructor like this:

  constructor(af: AngularFire) {
    this.af =  af ;
    this.item = af.database.object('/item');
  }

I asked fro using it in ngOnInit() since it was proposed here:
#637

@daslicht
Copy link
Author

Ok I found the issue , the af variable in the constructor needas to be private or public

constructor( private af: AngularFire) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants