Passed
Push — master ( 0543ef...cf6673 )
by Miloš
02:59
created

AbstractAuth.handleNativeLogout   A

Complexity

Conditions 2

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
1
import { AngularFireAuth } from "@angular/fire/auth";
2
import { Platform } from "@ionic/angular";
3
import { auth } from "firebase";
4
import { IAuthProvider } from "./i-auth-provider";
5
6
export abstract class AbstractAuth implements IAuthProvider {
7
    public readonly providerOptions: any;
8
9
    protected constructor(
10
        protected angularFireAuth: AngularFireAuth,
11
        protected platform: Platform,
12
    ) {}
13
14
    public abstract handleNativeLogin(
15
        options: any,
16
    ): Promise<auth.UserCredential | null>;
17
18
    public handleLogin(options: any = {}): Promise<auth.UserCredential | null> {
19
        if (this.platform.is("cordova")) {
20
            return this.handleNativeLogin(options);
21
        } else {
22
            return this.handleBrowserLogin();
23
        }
24
    }
25
26
    public async handleNativeLogout(): Promise<void> {
27
        if (this.angularFireAuth.auth.currentUser === null) {
28
            console.warn("Unknown currentUser!");
29
        }
30
    }
31
32
    public async handleBrowserLogout(): Promise<void> {
33
        return this.angularFireAuth.auth.signOut();
34
    }
35
36
    public async handleLogout(): Promise<void> {
37
        if (this.platform.is("cordova")) {
38
            return this.handleNativeLogout();
39
        } else {
40
            return this.handleBrowserLogout();
41
        }
42
    }
43
44
    public async handleBrowserLogin(): Promise<auth.UserCredential | null> {
45
        const provider = this.getBrowserLoginProvider();
46
        const authX = this.angularFireAuth.auth;
47
48
        switch (this.providerOptions.signInType) {
49
            case "popup":
50
                return await authX.signInWithPopup(provider);
51
            case "redirect":
52
                await authX.signInWithRedirect(provider);
53
                // TODO: implement redirect resolution: https://stackoverflow.com/questions/40219478/firebaseauth-googleauthprovider-signinwithredirect
54
                throw new Error("Not implemented!");
55
        }
56
57
        throw new Error("Invalid signInType!");
58
    }
59
60
    protected abstract getBrowserLoginProvider(): any | null;
61
}
62