Passed
Push — master ( dc03f9...e0c6dc )
by Miloš
03:58
created

src/providers/abstract-auth.ts   A

Complexity

Total Complexity 13
Complexity/F 1.63

Size

Lines of Code 87
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 68
mnd 5
bc 5
fnc 8
dl 0
loc 87
rs 10
bpm 0.625
cpm 1.625
noi 0
c 0
b 0
f 0

8 Functions

Rating   Name   Duplication   Size   Complexity  
A AbstractAuth.handleNativeLogout 0 4 2
A AbstractAuth.getBrowserLoginProvider 0 2 1
A AbstractAuth.handleBrowserLogin 0 15 2
A AbstractAuth.handleSignOut 0 6 2
A AbstractAuth.applyDefaultOptions 0 11 2
A AbstractAuth.handleLogin 0 6 2
A AbstractAuth.handleNativeLogin 0 4 1
A AbstractAuth.handleBrowserLogout 0 3 1
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