1
|
|
|
import { AngularFireAuth } from "@angular/fire/auth"; |
2
|
|
|
import { Platform } from "@ionic/angular"; |
3
|
|
|
import { auth } from "firebase"; |
4
|
|
|
import { UniFirebaseLoginConfig } from "../config/uni-firebase-login-config"; |
5
|
|
|
import { IAuthProvider } from "./i-auth-provider"; |
6
|
|
|
|
7
|
|
|
export abstract class AbstractAuth implements IAuthProvider { |
8
|
|
|
public abstract readonly providerKey: |
9
|
|
|
| "anonymous" |
10
|
|
|
| "email" |
11
|
|
|
| "facebook" |
12
|
|
|
| "github" |
13
|
|
|
| "google" |
14
|
|
|
| "phone" |
15
|
|
|
| "twitter"; |
16
|
|
|
public readonly defaultOptions: any = {}; |
17
|
|
|
|
18
|
|
|
protected constructor( |
19
|
|
|
protected angularFireAuth: AngularFireAuth, |
20
|
|
|
protected platform: Platform, |
21
|
|
|
protected config: UniFirebaseLoginConfig, |
22
|
|
|
) { |
23
|
|
|
this.applyDefaultOptions(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public abstract handleNativeLogin( |
27
|
|
|
options: any, |
28
|
|
|
): Promise<auth.UserCredential | null>; |
29
|
|
|
|
30
|
|
|
public handleLogin(options: any = {}): Promise<auth.UserCredential | null> { |
31
|
|
|
if (this.platform.is("cordova")) { |
32
|
|
|
return this.handleNativeLogin(options); |
33
|
|
|
} else { |
34
|
|
|
return this.handleBrowserLogin(); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public async handleNativeLogout(): Promise<void> { |
39
|
|
|
if (this.angularFireAuth.auth.currentUser === null) { |
40
|
|
|
console.warn("Unknown currentUser!"); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public async handleBrowserLogout(): Promise<void> { |
45
|
|
|
return this.angularFireAuth.auth.signOut(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public async handleSignOut(): Promise<void> { |
49
|
|
|
if (this.platform.is("cordova")) { |
50
|
|
|
return this.handleNativeLogout(); |
51
|
|
|
} else { |
52
|
|
|
return this.handleBrowserLogout(); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public async handleBrowserLogin(): Promise<auth.UserCredential | null> { |
57
|
|
|
const provider = this.getBrowserLoginProvider(); |
58
|
|
|
const authX = this.angularFireAuth.auth; |
59
|
|
|
|
60
|
|
|
switch (this.defaultOptions.signInType) { |
61
|
|
|
case "popup": |
62
|
|
|
return await authX.signInWithPopup(provider); |
63
|
|
|
case "redirect": |
64
|
|
|
await authX.signInWithRedirect(provider); |
65
|
|
|
// TODO: implement redirect resolution: https://stackoverflow.com/questions/40219478/firebaseauth-googleauthprovider-signinwithredirect |
66
|
|
|
throw new Error("Not implemented!"); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
throw new Error("Invalid signInType!"); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Apply default options to configuration for the provider |
74
|
|
|
*/ |
75
|
|
|
protected applyDefaultOptions(): any { |
76
|
|
|
if (this.config.providers[this.providerKey] === undefined) { |
77
|
|
|
this.config.providers[this.providerKey] = {}; |
78
|
|
|
} |
79
|
|
|
return Object.assign( |
80
|
|
|
this.config.providers[this.providerKey], |
81
|
|
|
this.defaultOptions, |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected abstract getBrowserLoginProvider(): any | null; |
86
|
|
|
} |
87
|
|
|
|