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

AuthProvider.getProvidersByUser   A

Complexity

Conditions 5

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 18
rs 9.2333
c 0
b 0
f 0
cc 5
1
import { Injectable } from "@angular/core";
2
import * as firebase from "firebase";
3
import { AnonymousAuth } from "../modules/anonymous/anonymous-auth";
4
import { EmailAuth } from "../modules/email/email-auth";
5
import { FacebookAuth } from "../modules/facebook/facebook-auth";
6
import { GithubAuth } from "../modules/github/github-auth";
7
import { GoogleAuth } from "../modules/google/google-auth";
8
import { PhoneAuth } from "../modules/phone/phone-auth";
9
import { TwitterAuth } from "../modules/twitter/twitter-auth";
10
import { IAuthProvider } from "./i-auth-provider";
11
12
@Injectable({
13
    providedIn: "root",
14
})
15
export class AuthProvider {
16
    public constructor(
17
        public authAnonymous: AnonymousAuth,
18
        public authEmail: EmailAuth,
19
        public authFacebook: FacebookAuth,
20
        public authGithub: GithubAuth,
21
        public authGoogle: GoogleAuth,
22
        public authPhone: PhoneAuth,
23
        public authTwitter: TwitterAuth,
24
    ) {}
25
26
    public getProviderById(
27
        providerId: string | null | undefined,
28
    ): IAuthProvider | null {
29
        switch (providerId) {
30
            case "password":
31
                return this.authEmail;
32
            case "phone":
33
                return this.authPhone;
34
            case "facebook.com":
35
                return this.authFacebook;
36
            case "github.com":
37
                return this.authGithub;
38
            case "google.com":
39
                return this.authGoogle;
40
            case "twitter.com":
41
                return this.authTwitter;
42
            case "apple.com":
43
            case "yahoo.com":
44
            case "hotmail.com":
45
                throw new Error(`Provider ${providerId} not implemented!`);
46
        }
47
48
        return null;
49
    }
50
51
    public getProvidersByUser(user: firebase.User): IAuthProvider[] {
52
        if (user.isAnonymous) {
53
            return [this.authAnonymous];
54
        }
55
56
        const providers: IAuthProvider[] = [];
57
58
        for (const providerData of user.providerData) {
59
            if (providerData) {
60
                const provider = this.getProviderById(providerData.providerId);
61
                if (provider) {
62
                    providers.push(provider);
63
                }
64
            }
65
        }
66
67
        return providers;
68
    }
69
}
70