auth.service.ts 1.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import {Injectable} from '@angular/core';
import {BehaviorSubject} from "rxjs/Rx";
import 'rxjs/add/operator/map';
import { AppSettings } from '../app.settings';
import { NgRedux, select } from 'ng2-redux';
import { IAppState } from '../store/store';
import { ILoginInfo, ILoginInfoToken } from '../store/logininfo/logininfo.types';
import { LOGININFO_INITIAL_STATE } from '../store/logininfo/logininfo.initial-state';

@Injectable()
export class AuthService {

    constructor(
        private _ngRedux: NgRedux<IAppState>) {

    };

    isLoggedIn(role) {
        return new Promise((resolve, reject) => {
            this._ngRedux.select(state => {
                return state.loginInfo;
            }).subscribe(loginInfo => {
                if (loginInfo.size > 0) {
                    loginInfo.reduce(({}, loginInfoToken) => {
                        if (loginInfoToken.auth_token && loginInfoToken.auth_token.length > 0 && loginInfoToken.auth_role === role) {
                            resolve(true);
                        }
                        else {
                            resolve(false);
                        }
                        return loginInfoToken;
                    }, {});
                } else
                    resolve(false);
            },
                error => {
                    console.log("Error Sending Verification Code");
                    reject("Error Getting Auth Data");
                },
                () => console.log("Getting Auth Data"));
        });
    }

}