import { Component , NgZone } from '@angular/core' ; import { AngularFireAuth } from '@angular/fire/auth' ; import { FormGroup , FormControl , Validators } from '@angular/forms' ; import { Router } from '@angular/router' ; import { FirebaseAuthService } from '../firebase-auth.service' ; import { Subscription } from 'rxjs' ; @ Component ( { selector : 'app-sign-in' , templateUrl : 'sign-in.page.html' , styleUrls : [ 'sign-in.page.scss' ] , } ) export class SignInPage { signInForm : FormGroup ; submitError : string ; authRedirectResult : Subscription ; validation_messages = { 'email' : [ { type : 'required' , message : 'Email is required.' } , { type : 'pattern' , message : 'Enter a valid email.' } ] , 'password' : [ { type : 'required' , message : 'Password is required.' } , { type : 'minlength' , message : 'Password must be at least 6 characters long.' } ] } ; constructor ( public angularFire , public router , private ngZone , private authService ) { this . signInForm = new FormGroup ( { 'email' : new FormControl ( '' , Validators . compose ( [ Validators . required , Validators . pattern ( '^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$' ) ] ) ) , 'password' : new FormControl ( '' , Validators . compose ( [ Validators . minLength ( 6 ) , Validators . required ] ) ) } ) ; this . authRedirectResult = this . authService . getRedirectResult ( ) . subscribe ( result => { if ( result . user ) { this . redirectLoggedUserToProfilePage ( ) ; } else if ( result . error ) { this . submitError = result . error ; } } ) ; } redirectLoggedUserToProfilePage ( ) { this . ngZone . run ( ( ) => { this . router . navigate ( [ 'profile' ] ) ; } ) ; } signInWithEmail ( ) { this . authService . signInWithEmail ( this . signInForm . value [ 'email' ] , this . signInForm . value [ 'password' ] ) . then ( user => { this . redirectLoggedUserToProfilePage ( ) ; } ) . catch ( error => { this . submitError = error . message ; } ) ; } facebookSignIn ( ) { this . authService . signInWithFacebook ( ) . then ( ( result ) => { if ( result . additionalUserInfo ) { this . authService . setProviderAdditionalInfo ( result . additionalUserInfo . profile ) ; } this . redirectLoggedUserToProfilePage ( ) ; } ) . catch ( ( error ) => { console . log ( error ) ; } ) ; } googleSignIn ( ) { this . authService . signInWithGoogle ( ) . then ( ( result ) => { if ( result . additionalUserInfo ) { this . authService . setProviderAdditionalInfo ( result . additionalUserInfo . profile ) ; } this . redirectLoggedUserToProfilePage ( ) ; } ) . catch ( ( error ) => { console . log ( error ) ; } ) ; } twitterSignIn ( ) { this . authService . signInWithTwitter ( ) . then ( ( result ) => { if ( result . additionalUserInfo ) { this . authService . setProviderAdditionalInfo ( result . additionalUserInfo . profile ) ; } this . redirectLoggedUserToProfilePage ( ) ; } ) . catch ( ( error ) => { console . log ( error ) ; } ) ; } }