Skip to content

Commit 1b22eae

Browse files
committed
fix: try signInWithRedirect if signInWithPopup fails (#43)
1 parent 40a88a6 commit 1b22eae

4 files changed

Lines changed: 19 additions & 7 deletions

File tree

src/app/auth.service.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {inject, Injectable} from '@angular/core';
2-
import {Auth, GoogleAuthProvider, idToken, user, signInWithPopup, signOut, User} from '@angular/fire/auth';
2+
import {Auth, getRedirectResult, GoogleAuthProvider, idToken, signInWithPopup, signInWithRedirect, signOut, user, User} from '@angular/fire/auth';
33
import {BehaviorSubject, Subscription} from 'rxjs';
44

55
@Injectable({
@@ -25,6 +25,7 @@ export class AuthService {
2525
this.userSubscription = this.user$.subscribe((aUser: User | null) => {
2626
this.userSubject.next(aUser);
2727
});
28+
getRedirectResult(this.auth); // ignore result
2829
}
2930

3031
signInWithPopup() {
@@ -33,6 +34,12 @@ export class AuthService {
3334
return signInWithPopup(this.auth, provider);
3435
}
3536

37+
signInWithRedirect() {
38+
const provider = new GoogleAuthProvider();
39+
provider.setCustomParameters({hd: 'docchula.com'});
40+
return signInWithRedirect(this.auth, provider);
41+
}
42+
3643
signOut() {
3744
return signOut(this.auth);
3845
}

src/app/welcome/welcome.page.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,15 @@ export class WelcomePage implements OnInit, OnDestroy {
8686
await loading.present();
8787

8888
this.authService.signInWithPopup().catch(async (error) => {
89-
await this.alertError(
90-
"Login Failed",
91-
`Unable to sign in. Please try again. (${error.code}: ${error.message})`,
92-
);
89+
if (error.code === "auth/popup-closed-by-user") {
90+
// User closed the popup without completing the sign-in. May cause by popup blocker.
91+
await this.authService.signInWithRedirect();
92+
} else {
93+
await this.alertError(
94+
"Login Failed",
95+
`Unable to sign in. Please try again. (${error.code}: ${error.message})`,
96+
);
97+
}
9398
}).finally(() => loading.dismiss());
9499
}
95100

src/environments/environment.prod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export const environment = {
22
production: true,
33
firebase: {
44
apiKey: 'AIzaSyChVnSa_0Lk0U4SvPi_yTmpzCC5W2CZqaI',
5-
authDomain: 'keendev-flick-a.firebaseapp.com',
5+
authDomain: 'flick.docchula.com',
66
databaseURL: 'https://keendev-flick-a.firebaseio.com',
77
projectId: 'keendev-flick-a',
88
storageBucket: 'keendev-flick-a.appspot.com',

src/environments/environment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const environment = {
66
production: false,
77
firebase: {
88
apiKey: 'AIzaSyChVnSa_0Lk0U4SvPi_yTmpzCC5W2CZqaI',
9-
authDomain: 'keendev-flick-a.firebaseapp.com',
9+
authDomain: 'flick.docchula.com',
1010
databaseURL: 'https://keendev-flick-a.firebaseio.com',
1111
projectId: 'keendev-flick-a',
1212
storageBucket: 'keendev-flick-a.appspot.com',

0 commit comments

Comments
 (0)