Skip to content

Instantly share code, notes, and snippets.

@gavilanch
Created June 25, 2019 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gavilanch/3654aee7ec6df37439cc52cfe708e20e to your computer and use it in GitHub Desktop.
Save gavilanch/3654aee7ec6df37439cc52cfe708e20e to your computer and use it in GitHub Desktop.
import { Component, OnInit, NgZone } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { AngularFireAuth } from '@angular/fire/auth';
import { Router } from '@angular/router';
@Component({
 selector: 'app-login',
 templateUrl: './login.component.html',
 styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
 errorMessage = '';
 constructor(private afAuth: AngularFireAuth,
   private router: Router,
   private fb: FormBuilder,
   private ngZone: NgZone) { }
 loginForm = this.fb.group({
   email: ['', Validators.required],
   password: ['', Validators.required]
 })
 ngOnInit() {
   this.afAuth.user.subscribe(user => {
     if (user) {
       this.ngZone.run(() => {
         this.router.navigate(['/todos']);
       })
   })
 createUser() {
  this.afAuth.auth.createUserWithEmailAndPassword(this.loginForm.value.email, this.loginForm.value.password).then(() => {
     this.router.navigate(['/todos']);
   }).catch(response => {
     this.errorMessage = response.message;
   });
 signIn() { 
this.afAuth.auth.signInWithEmailAndPassword(this.loginForm.value.email, this.loginForm.value.password).then(() => {
     this.router.navigate(['/todos']);
   }).catch(response => {
     this.errorMessage = response.message;
   });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment