11import { Component , OnInit } from '@angular/core' ;
22import { AuthService } from './auth.service' ;
33import { Router } from '@angular/router' ;
4- import { LoadingController } from '@ionic/angular' ;
4+ import { AlertController , LoadingController } from '@ionic/angular' ;
55import { NgForm } from '@angular/forms' ;
66
77@Component ( {
@@ -16,12 +16,13 @@ export class AuthPage implements OnInit {
1616 constructor (
1717 private authService : AuthService ,
1818 private router : Router ,
19- private loadingCtrl : LoadingController
19+ private loadingCtrl : LoadingController ,
20+ private alrtCtrl : AlertController
2021 ) { }
2122
2223 ngOnInit ( ) { }
2324
24- onLogin ( ) {
25+ authenticate ( email : string , password : string ) {
2526 this . isLoading = true ;
2627 this . authService . login ( ) ;
2728 this . loadingCtrl
@@ -31,33 +32,36 @@ export class AuthPage implements OnInit {
3132 } )
3233 . then ( ( loadingEl ) => {
3334 loadingEl . present ( ) ;
34- setTimeout ( ( ) => {
35+ this . authService . signup ( email , password ) . subscribe ( ( resData ) => {
3536 this . isLoading = false ;
3637 loadingEl . dismiss ( ) ;
3738 this . router . navigateByUrl ( '/places/tabs/discover' ) ;
38- } , 1500 ) ;
39+ } , errRes => {
40+ loadingEl . dismiss ( ) ;
41+ this . showAlert ( errRes . error . error . message ) ;
42+ } ) ;
3943 } ) ;
4044 }
4145
42- onSubmit ( form : NgForm ) {
43- if ( form . invalid ) {
46+ onSubmit ( form : NgForm ) {
47+ if ( form . invalid ) {
4448 return ;
4549 }
4650 const email = form . value . email ;
4751 const password = form . value . password ;
4852
49- console . log ( email , password ) ;
50-
51-
52- if ( ! this . isLogin ) {
53- // Send a request to login endpoint
54- }
55- else {
56- // Send a request to signup endpoint
57- }
53+ this . authenticate ( email , password ) ;
5854 }
5955
60- onSwitchAuthMode ( ) {
61- this . isLogin = ! this . isLogin ;
56+ private showAlert ( message : string ) {
57+ this . alrtCtrl
58+ . create ( {
59+ header : 'Authentication Failed!' ,
60+ message : message ,
61+ buttons : [ 'Okay' ] ,
62+ } )
63+ . then ( ( alertEl ) => {
64+ alertEl . present ( ) ;
65+ } ) ;
6266 }
6367}
0 commit comments