1
|
|
|
import { Injectable } from "@angular/core"; |
2
|
|
|
import { Router } from "@angular/router"; |
3
|
|
|
import { Platform } from "@ionic/angular"; |
4
|
|
|
import * as firebase from "firebase"; |
5
|
|
|
import { auth } from "firebase/app"; |
6
|
|
|
import { Observable } from "rxjs"; |
7
|
|
|
import { UniFirebaseLoginConfig } from "../config/uni-firebase-login-config"; |
8
|
|
|
import { UserModel } from "../model/user-model"; |
9
|
|
|
import { AuthProvider } from "../providers/auth-provider"; |
10
|
|
|
import { IAuthProvider } from "../providers/i-auth-provider"; |
11
|
|
|
import { AuthStorageProvider } from "../storage/auth-storage-provider.service"; |
12
|
|
|
import { IAuthService } from "./i-auth-service"; |
13
|
|
|
|
14
|
|
|
@Injectable({ |
15
|
|
|
providedIn: "root", |
16
|
|
|
}) |
17
|
|
|
export class BaseAuthService<User extends UserModel = UserModel> |
18
|
|
|
implements IAuthService { |
19
|
|
|
public constructor( |
20
|
|
|
protected router: Router, |
21
|
|
|
protected platform: Platform, |
22
|
|
|
protected authProvider: AuthProvider, |
23
|
|
|
protected authStorageProvider: AuthStorageProvider<User>, |
24
|
|
|
protected config: UniFirebaseLoginConfig, |
25
|
|
|
) {} |
26
|
|
|
|
27
|
|
|
public async signInByProvider( |
28
|
|
|
provider: IAuthProvider, |
29
|
|
|
redirect: boolean = true, |
30
|
|
|
callbackBeforeRedirect: ( |
31
|
|
|
credential: auth.UserCredential | null, |
32
|
|
|
) => Promise<void> | void = () => undefined, |
33
|
|
|
): Promise<void> { |
34
|
|
|
const credential = await provider.handleLogin(); |
35
|
|
|
if ( |
36
|
|
|
this.config.storage !== false && |
37
|
|
|
credential && |
38
|
|
|
credential.user !== null |
39
|
|
|
) { |
40
|
|
|
await this.authStorageProvider |
41
|
|
|
.getProvider() |
42
|
|
|
.updateStoredDataByFirebaseUser(credential.user); |
43
|
|
|
} |
44
|
|
|
await callbackBeforeRedirect(credential); |
45
|
|
|
if (redirect) { |
46
|
|
|
await this.redirectAfterLogin(); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public async signInAnonymously( |
51
|
|
|
redirect: boolean = true, |
52
|
|
|
callbackBeforeRedirect: ( |
53
|
|
|
credential: auth.UserCredential | null, |
54
|
|
|
) => Promise<void> | void = () => undefined, |
55
|
|
|
): Promise<void> { |
56
|
|
|
await this.signInByProvider( |
57
|
|
|
this.authProvider.authAnonymous, |
58
|
|
|
redirect, |
59
|
|
|
callbackBeforeRedirect, |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public async signInViaEmail( |
64
|
|
|
redirect: boolean = true, |
65
|
|
|
callbackBeforeRedirect: ( |
66
|
|
|
credential: auth.UserCredential | null, |
67
|
|
|
) => Promise<void> | void = () => undefined, |
68
|
|
|
): Promise<void> { |
69
|
|
|
await this.signInByProvider( |
70
|
|
|
this.authProvider.authEmail, |
71
|
|
|
redirect, |
72
|
|
|
callbackBeforeRedirect, |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public async signInViaFacebook( |
77
|
|
|
redirect: boolean = true, |
78
|
|
|
callbackBeforeRedirect: ( |
79
|
|
|
credential: auth.UserCredential | null, |
80
|
|
|
) => Promise<void> | void = () => undefined, |
81
|
|
|
): Promise<void> { |
82
|
|
|
await this.signInByProvider( |
83
|
|
|
this.authProvider.authFacebook, |
84
|
|
|
redirect, |
85
|
|
|
callbackBeforeRedirect, |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public async signInViaGithub( |
90
|
|
|
redirect: boolean = true, |
91
|
|
|
callbackBeforeRedirect: ( |
92
|
|
|
credential: auth.UserCredential | null, |
93
|
|
|
) => Promise<void> | void = () => undefined, |
94
|
|
|
): Promise<void> { |
95
|
|
|
await this.signInByProvider( |
96
|
|
|
this.authProvider.authGithub, |
97
|
|
|
redirect, |
98
|
|
|
callbackBeforeRedirect, |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public async signInViaGoogle( |
103
|
|
|
redirect: boolean = true, |
104
|
|
|
callbackBeforeRedirect: ( |
105
|
|
|
credential: auth.UserCredential | null, |
106
|
|
|
) => Promise<void> | void = () => undefined, |
107
|
|
|
): Promise<void> { |
108
|
|
|
await this.signInByProvider( |
109
|
|
|
this.authProvider.authGoogle, |
110
|
|
|
redirect, |
111
|
|
|
callbackBeforeRedirect, |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public async signInViaPhone( |
116
|
|
|
redirect: boolean = true, |
117
|
|
|
callbackBeforeRedirect: ( |
118
|
|
|
credential: auth.UserCredential | null, |
119
|
|
|
) => Promise<void> | void = () => undefined, |
120
|
|
|
): Promise<void> { |
121
|
|
|
await this.signInByProvider( |
122
|
|
|
this.authProvider.authPhone, |
123
|
|
|
redirect, |
124
|
|
|
callbackBeforeRedirect, |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public async signInViaTwitter( |
129
|
|
|
redirect: boolean = true, |
130
|
|
|
callbackBeforeRedirect: ( |
131
|
|
|
credential: auth.UserCredential | null, |
132
|
|
|
) => Promise<void> | void = () => undefined, |
133
|
|
|
): Promise<void> { |
134
|
|
|
await this.signInByProvider( |
135
|
|
|
this.authProvider.authTwitter, |
136
|
|
|
redirect, |
137
|
|
|
callbackBeforeRedirect, |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public get currentFirebaseUser(): firebase.User | null { |
142
|
|
|
return firebase.auth().currentUser; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Handle sign out request |
147
|
|
|
*/ |
148
|
|
|
public async signOut(): Promise<void> { |
149
|
|
|
const currentUser = auth().currentUser; |
150
|
|
|
|
151
|
|
|
if (currentUser) { |
152
|
|
|
const providers = this.authProvider.getProvidersByUser(currentUser); |
153
|
|
|
|
154
|
|
|
for (const provider of providers) { |
155
|
|
|
await provider.handleSignOut(); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
await this.redirectAfterSignOut(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get user profile data |
163
|
|
|
*/ |
164
|
|
|
public getUser(fromCache: boolean = true): Observable<User | null> { |
165
|
|
|
if (fromCache) { |
166
|
|
|
return this.authStorageProvider.getUser(); |
167
|
|
|
} else { |
168
|
|
|
return this.authStorageProvider.getUserNonCached(); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public async updateUserData(user: User) { |
173
|
|
|
await this.authStorageProvider |
174
|
|
|
.getProvider() |
175
|
|
|
.updateStoredDataByUser(user); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
protected async redirectAfterSignOut() { |
179
|
|
|
if (this.config.signInPage) { |
180
|
|
|
await this.router.navigate([this.config.signInPage]); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
protected async redirectAfterLogin() { |
185
|
|
|
if (this.config.afterSignInPage) { |
186
|
|
|
console.log("Redirect ", this.config.afterSignInPage); |
187
|
|
|
await this.router.navigate([this.config.afterSignInPage]); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|