1
|
|
|
import { Injectable } from "@angular/core"; |
2
|
|
|
import { Router } from "@angular/router"; |
3
|
|
|
import { Platform } from "@ionic/angular"; |
4
|
|
|
import { auth } from "firebase/app"; |
5
|
|
|
import { Observable } from "rxjs"; |
6
|
|
|
import { UserModel } from "../model/user-model"; |
7
|
|
|
import { AuthProvider } from "../providers/auth-provider"; |
8
|
|
|
import { IAuthProvider } from "../providers/i-auth-provider"; |
9
|
|
|
import { AuthStorageProvider } from "../storage/auth-storage-provider.service"; |
10
|
|
|
import { IAuthOptions } from "./i-auth-options"; |
11
|
|
|
import { IAuthProviderOptions } from "./i-auth-provider-options"; |
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 options: IAuthOptions = { |
20
|
|
|
afterLoginPage: "/", |
21
|
|
|
signInPage: "/sign-in", |
22
|
|
|
storage: false, |
23
|
|
|
storageUserTable: "users", |
24
|
|
|
}; |
25
|
|
|
public providerOptions: IAuthProviderOptions = { |
26
|
|
|
google: { |
27
|
|
|
offline: true, |
28
|
|
|
scopes: "profile email", |
29
|
|
|
signInType: "popup", |
30
|
|
|
webClientId: "xxxxxx.apps.googleusercontent.com", |
31
|
|
|
}, |
32
|
|
|
}; |
33
|
|
|
|
34
|
|
|
public constructor( |
35
|
|
|
protected router: Router, |
36
|
|
|
protected platform: Platform, |
37
|
|
|
protected authProvider: AuthProvider, |
38
|
|
|
protected authStorageProvider: AuthStorageProvider<User>, |
39
|
|
|
) { |
40
|
|
|
this.authStorageProvider.options.storage = this.options.storage; |
41
|
|
|
this.authStorageProvider.options.userTable = this.options.storageUserTable; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public async signInByProvider( |
45
|
|
|
provider: IAuthProvider, |
46
|
|
|
storeInDb = false, |
47
|
|
|
): Promise<auth.UserCredential | null> { |
48
|
|
|
const credential = await provider.handleLogin(); |
49
|
|
|
if (storeInDb && credential && credential.user !== null) { |
50
|
|
|
await this.authStorageProvider |
51
|
|
|
.getProvider() |
52
|
|
|
.updateStoredDataByFirebaseUser(credential.user); |
53
|
|
|
} |
54
|
|
|
this.onAfterLogin(); |
55
|
|
|
return credential; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public async signInAnonymously(storeInDb = false): Promise<any> { |
59
|
|
|
return this.signInByProvider(this.authProvider.authAnonymous); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public async signInViaEmail(storeInDb = false): Promise<any> { |
63
|
|
|
return this.signInByProvider(this.authProvider.authEmail); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public async signInViaFacebook(storeInDb = false): Promise<any> { |
67
|
|
|
return this.signInByProvider(this.authProvider.authFacebook); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public async signInViaGithub(storeInDb = false): Promise<any> { |
71
|
|
|
return this.signInByProvider(this.authProvider.authGithub); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public async signInViaGoogle(storeInDb = false): Promise<any> { |
75
|
|
|
return this.signInByProvider(this.authProvider.authGoogle); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public async signInViaPhone(storeInDb = false): Promise<any> { |
79
|
|
|
return this.signInByProvider(this.authProvider.authPhone); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public async signInViaTwitter(storeInDb = false): Promise<any> { |
83
|
|
|
return this.signInByProvider(this.authProvider.authTwitter); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Handle sign out request |
88
|
|
|
*/ |
89
|
|
|
public async signOut(): Promise<void> { |
90
|
|
|
const currentUser = auth().currentUser; |
91
|
|
|
if (currentUser) { |
92
|
|
|
const provider = await this.authProvider.getProviderByUser( |
93
|
|
|
currentUser, |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
if (provider) { |
97
|
|
|
await provider.handleSignOut(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
this.onAfterSignOut(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get user profile data |
105
|
|
|
*/ |
106
|
|
|
public getUser(): Observable<unknown | User | null> { |
107
|
|
|
return this.authStorageProvider.getUser(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public setUserFactoryFunction(factory: () => any): any { |
111
|
|
|
this.authStorageProvider.userFactory.factoryFunction = factory; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public async updateUserData(user: User) { |
115
|
|
|
await this.authStorageProvider |
116
|
|
|
.getProvider() |
117
|
|
|
.updateStoredDataByUser(user); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected onAfterSignOut() { |
121
|
|
|
if (this.options.signInPage) { |
122
|
|
|
this.router.navigate([this.options.signInPage]); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected onAfterLogin() { |
127
|
|
|
if (this.options.afterLoginPage) { |
128
|
|
|
this.router.navigate([this.options.afterLoginPage]); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|