|
1 | | -import { Component, OnDestroy, OnInit } from '@angular/core'; |
2 | | -import { AlertController, LoadingController, IonContent, IonCard, IonCardHeader, IonCardTitle, IonCardContent, IonButton, IonText } from '@ionic/angular/standalone'; |
3 | | -import { Router } from '@angular/router'; |
4 | | -import { ManService } from '../man.service'; |
5 | | -import { HttpErrorResponse } from '@angular/common/http'; |
6 | | -import { Subscription } from 'rxjs'; |
7 | | -import { User } from '@angular/fire/auth'; |
8 | | -import { AuthService } from '../auth.service'; |
| 1 | +import { Component, OnDestroy, OnInit } from "@angular/core"; |
| 2 | +import { |
| 3 | + AlertController, |
| 4 | + LoadingController, |
| 5 | + IonContent, |
| 6 | + IonCard, |
| 7 | + IonCardHeader, |
| 8 | + IonCardTitle, |
| 9 | + IonCardContent, |
| 10 | + IonButton, |
| 11 | + IonText, |
| 12 | +} from "@ionic/angular/standalone"; |
| 13 | +import { Router } from "@angular/router"; |
| 14 | +import { ManService } from "../man.service"; |
| 15 | +import { HttpErrorResponse } from "@angular/common/http"; |
| 16 | +import { Subscription } from "rxjs"; |
| 17 | +import { User } from "@angular/fire/auth"; |
| 18 | +import { AuthService } from "../auth.service"; |
9 | 19 |
|
10 | 20 | @Component({ |
11 | | - selector: 'app-welcome', |
12 | | - templateUrl: './welcome.page.html', |
13 | | - styleUrls: ['./welcome.page.scss'], |
14 | | - imports: [IonContent, IonCard, IonCardHeader, IonCardTitle, IonCardContent, IonButton, IonText] |
| 21 | + selector: "app-welcome", |
| 22 | + templateUrl: "./welcome.page.html", |
| 23 | + styleUrls: ["./welcome.page.scss"], |
| 24 | + imports: [ |
| 25 | + IonContent, |
| 26 | + IonCard, |
| 27 | + IonCardHeader, |
| 28 | + IonCardTitle, |
| 29 | + IonCardContent, |
| 30 | + IonButton, |
| 31 | + IonText, |
| 32 | + ], |
15 | 33 | }) |
16 | 34 | export class WelcomePage implements OnInit, OnDestroy { |
17 | | - authStateSubscription: Subscription; |
18 | | - isAuthChecked: boolean; |
19 | | - user: User; |
20 | | - isSafari = /Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent); |
21 | | - isIAB = /FBAN/.test(navigator.userAgent) || /FBAV/.test(navigator.userAgent) || /Line\//.test(navigator.userAgent); |
| 35 | + authStateSubscription: Subscription; |
| 36 | + isAuthChecked: boolean; |
| 37 | + user: User; |
| 38 | + isSafari = |
| 39 | + /Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent); |
| 40 | + isIAB = |
| 41 | + /FBAN/.test(navigator.userAgent) || |
| 42 | + /FBAV/.test(navigator.userAgent) || |
| 43 | + /Line\//.test(navigator.userAgent); |
22 | 44 |
|
23 | | - constructor( |
24 | | - private router: Router, private authService: AuthService, |
25 | | - private manService: ManService, private loadingCtrl: LoadingController, |
26 | | - public alertCtrl: AlertController) { |
27 | | - } |
| 45 | + constructor( |
| 46 | + private router: Router, |
| 47 | + private authService: AuthService, |
| 48 | + private manService: ManService, |
| 49 | + private loadingCtrl: LoadingController, |
| 50 | + public alertCtrl: AlertController |
| 51 | + ) {} |
28 | 52 |
|
29 | | - ngOnInit() { |
30 | | - this.loadingCtrl.create({ |
31 | | - message: 'Authenticating...', |
32 | | - duration: 7000 |
33 | | - }).then(loading => { |
34 | | - loading.present(); |
35 | | - this.authStateSubscription = this.authService.user.subscribe((user) => { |
36 | | - this.user = user; |
37 | | - if (user) { |
38 | | - // User is signed in. |
39 | | - this.goToHome().then(() => { |
40 | | - loading.dismiss(); |
41 | | - }); |
42 | | - } else { |
43 | | - this.isAuthChecked = false; |
44 | | - } |
45 | | - loading.dismiss(); |
| 53 | + ngOnInit() { |
| 54 | + this.loadingCtrl |
| 55 | + .create({ |
| 56 | + message: "Authenticating...", |
| 57 | + duration: 7000, |
| 58 | + }) |
| 59 | + .then((loading) => { |
| 60 | + loading.present(); |
| 61 | + this.authStateSubscription = this.authService.user.subscribe((user) => { |
| 62 | + this.user = user; |
| 63 | + if (user) { |
| 64 | + // User is signed in. |
| 65 | + this.goToHome().then(() => { |
| 66 | + loading.dismiss(); |
46 | 67 | }); |
47 | | - }).catch(e => console.log('Reject', e)); |
48 | | - } |
| 68 | + } else { |
| 69 | + this.isAuthChecked = false; |
| 70 | + } |
| 71 | + loading.dismiss(); |
| 72 | + }); |
| 73 | + }) |
| 74 | + .catch((e) => console.log("Reject", e)); |
| 75 | + } |
49 | 76 |
|
50 | | - ngOnDestroy() { |
51 | | - this.authStateSubscription.unsubscribe(); |
52 | | - } |
| 77 | + ngOnDestroy() { |
| 78 | + this.authStateSubscription.unsubscribe(); |
| 79 | + } |
53 | 80 |
|
54 | | - login() { |
55 | | - this.authService.signInWithPopup(); |
56 | | - } |
| 81 | + async login() { |
| 82 | + const loading = await this.loadingCtrl.create({ |
| 83 | + message: "Signing in...", |
| 84 | + spinner: "crescent", |
| 85 | + }); |
| 86 | + await loading.present(); |
57 | 87 |
|
58 | | - logout() { |
59 | | - this.authService.signOut().then().catch(e => console.log('Reject', e)); |
60 | | - this.isAuthChecked = false; |
| 88 | + try { |
| 89 | + await this.authService.signInWithPopup(); |
| 90 | + } catch (e) { |
| 91 | + console.error("Login failed", e); |
| 92 | + await this.alertError( |
| 93 | + "Login Failed", |
| 94 | + "Unable to sign in. Please try again." |
| 95 | + ); |
| 96 | + } finally { |
| 97 | + loading.dismiss(); |
61 | 98 | } |
| 99 | + } |
62 | 100 |
|
63 | | - goToHome() { |
64 | | - if (this.isAuthChecked) { |
65 | | - return this.router.navigate(['/home']); |
66 | | - } |
67 | | - this.user.getIdToken().then(idToken => { |
68 | | - this.manService.setIdToken(idToken); |
69 | | - this.manService.checkAuthorization().toPromise().then((result) => { |
70 | | - if (result) { |
71 | | - this.isAuthChecked = true; |
72 | | - return this.router.navigate(['/home']); |
73 | | - } |
74 | | - }, (reason: HttpErrorResponse) => { |
75 | | - if (reason instanceof ErrorEvent) { |
76 | | - this.alertError('Client Error', 'Please check your network connection.'); |
77 | | - } else if (reason.status === 401) { |
78 | | - this.alertError('Unregistered!', 'You are not allowed to access this website.'); |
79 | | - } else if ([500, 502, 503, 504].includes(reason.status)) { |
80 | | - this.alertError('Server Error', 'Please contact administrator.'); |
81 | | - } else { |
82 | | - this.manService.changeEndpoint(); |
83 | | - this.alertError('Connection Error', 'Unable to reach server. You may try again.'); |
84 | | - } |
85 | | - }); |
86 | | - }); |
87 | | - return new Promise<void>(resolve => resolve()); |
| 101 | + logout() { |
| 102 | + this.authService |
| 103 | + .signOut() |
| 104 | + .then() |
| 105 | + .catch((e) => console.log("Reject", e)); |
| 106 | + this.isAuthChecked = false; |
| 107 | + } |
| 108 | + |
| 109 | + goToHome() { |
| 110 | + if (this.isAuthChecked) { |
| 111 | + return this.router.navigate(["/home"]); |
88 | 112 | } |
| 113 | + this.user.getIdToken().then((idToken) => { |
| 114 | + this.manService.setIdToken(idToken); |
| 115 | + this.manService |
| 116 | + .checkAuthorization() |
| 117 | + .toPromise() |
| 118 | + .then( |
| 119 | + (result) => { |
| 120 | + if (result) { |
| 121 | + this.isAuthChecked = true; |
| 122 | + return this.router.navigate(["/home"]); |
| 123 | + } |
| 124 | + }, |
| 125 | + (reason: HttpErrorResponse) => { |
| 126 | + if (reason instanceof ErrorEvent) { |
| 127 | + this.alertError( |
| 128 | + "Client Error", |
| 129 | + "Please check your network connection." |
| 130 | + ); |
| 131 | + } else if (reason.status === 401) { |
| 132 | + this.alertError( |
| 133 | + "Unregistered!", |
| 134 | + "You are not allowed to access this website." |
| 135 | + ); |
| 136 | + } else if ([500, 502, 503, 504].includes(reason.status)) { |
| 137 | + this.alertError("Server Error", "Please contact administrator."); |
| 138 | + } else { |
| 139 | + this.manService.changeEndpoint(); |
| 140 | + this.alertError( |
| 141 | + "Connection Error", |
| 142 | + "Unable to reach server. You may try again." |
| 143 | + ); |
| 144 | + } |
| 145 | + } |
| 146 | + ); |
| 147 | + }); |
| 148 | + return new Promise<void>((resolve) => resolve()); |
| 149 | + } |
89 | 150 |
|
90 | | - async alertError(header: string, message: string) { |
91 | | - const alert = await this.alertCtrl.create({ |
92 | | - header, |
93 | | - message, |
94 | | - buttons: ['OK'] |
95 | | - }); |
| 151 | + async alertError(header: string, message: string) { |
| 152 | + const alert = await this.alertCtrl.create({ |
| 153 | + header, |
| 154 | + message, |
| 155 | + buttons: ["OK"], |
| 156 | + }); |
96 | 157 |
|
97 | | - await alert.present(); |
98 | | - } |
| 158 | + await alert.present(); |
| 159 | + } |
99 | 160 | } |
0 commit comments