Socialify

Folder ..

Viewing login.page.ts
82 lines (70 loc) • 2.0 KB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { Component, OnInit } from '@angular/core';
import { AlertController } from '@ionic/angular';
import { LoadingController } from '@ionic/angular';
import { Router } from '@angular/router';
import * as firebase from 'firebase/app';
import 'firebase/auth';
import * as $ from 'jquery';
import '../firebase';

// firebase.auth().signOut();

@Component({
  selector: 'app-login',
  templateUrl: './login.page.html',
  styleUrls: ['./login.page.scss'],
})
export class LoginPage implements OnInit {

  constructor(public alertController: AlertController,
    public loadingController: LoadingController, private router: Router) { }

    ionViewWillEnter() {
      this.loginCheck();
    }

  ngOnInit() {
    // this.loginCheck();
  }

  async presentLoading() {
    const loading = await this.loadingController.create({
      message: 'Checking Information',
      duration: 800
    });
    return await loading.present();
  }

  async emptyAlert() {
    const empty = await this.alertController.create({
      // header: 'Error!',
      message: 'All fields are required. Please fill the details and try again',
      buttons: ['OK']
    });

    await empty.present();
  }

  async errorAlert(message) {
    const errorAl = await this.alertController.create({
      message: message,
      buttons: ['OK']
    });
    await errorAl.present();
  }

  loginCheck() {
    firebase.auth().onAuthStateChanged(user => {
      if (!user) {
        this.presentLoading();
      } else {
        this.presentLoading();
        // console.log(user);
        this.router.navigateByUrl('/home');
      }
  });
}

  doLogin() {
    const email = $('#loginEmail').val();
    const password = $('#loginPassword').val();
    if (email === '' || password === '') {
      this.emptyAlert();
    } else {
      firebase.auth().signInWithEmailAndPassword(email, password).catch((error) => {
        const errorMessage = error.message;
        this.errorAlert(errorMessage);
      });
      this.loginCheck();
    }
  }

}