### File

`projects/app-base-library/src/lib/angular/services/app-base.service.ts`

### Index

###### Properties

- Public api
- Public db
- Private environment
- Public firebaseApps
- Public firebaseUser
- Public headers

###### Methods

- Public initApp
- Public initBaseApp
- Private initDev
- Public initFirebase
- Public initPermissions
- Public initRouter
- Public initTranslation
- Public loginFirebase
- Public logoutFirebase
- Public validateRole

### Constructor

`constructor(db: AngularFireDatabase, baseService: BaseService, config: ConfigService, ngxPermissions: NgxPermissionsService, rolesService: NgxRolesService, translateService: TranslateService, afAuth: AngularFireAuth)`
Defined in projects/app-base-library/src/lib/angular/services/app-base.service.ts:19

Parameters :

Name | Type | Optional
db | `AngularFireDatabase` | no
baseService | `BaseService` | no
config | `ConfigService` | no
ngxPermissions | `NgxPermissionsService` | no
rolesService | `NgxRolesService` | no
translateService | `TranslateService` | no
afAuth | `AngularFireAuth` | no

### Methods

Public initApp
`initApp(environment: any, configUrl: string)`
Defined in projects/app-base-library/src/lib/angular/services/app-base.service.ts:33

Parameters :

Name | Type | Optional
environment | `any` | no
configUrl | `string` | no

Returns : `any`

Public initBaseApp
`initBaseApp(options: any)`
Defined in projects/app-base-library/src/lib/angular/services/app-base.service.ts:59

Parameters :

Name | Type | Optional
options | `any` | no

Returns : `any`

Private initDev
`initDev()`
Defined in projects/app-base-library/src/lib/angular/services/app-base.service.ts:153
Returns : `any`

Public initFirebase
`initFirebase()`
Defined in projects/app-base-library/src/lib/angular/services/app-base.service.ts:81
Returns : `void`

Public initPermissions
`initPermissions()`
Defined in projects/app-base-library/src/lib/angular/services/app-base.service.ts:109
Returns : `void`

Public initRouter
`initRouter(router: , component: )`
Defined in projects/app-base-library/src/lib/angular/services/app-base.service.ts:77

Parameters :

Name | Optional
router | no
component | no

Returns : `void`

Public initTranslation
`initTranslation()`
Defined in projects/app-base-library/src/lib/angular/services/app-base.service.ts:96
Returns : `void`

Public loginFirebase
`loginFirebase(email: , password: )`
Defined in projects/app-base-library/src/lib/angular/services/app-base.service.ts:129

Parameters :

Name | Optional
email | no
password | no

Returns : `any`

Public logoutFirebase
`logoutFirebase()`
Defined in projects/app-base-library/src/lib/angular/services/app-base.service.ts:145
Returns : `any`

Public validateRole
`validateRole(roleName: )`
Defined in projects/app-base-library/src/lib/angular/services/app-base.service.ts:125

Parameters :

Name | Optional
roleName | no

Returns : `void`

### Properties

Public api
`api: `any``
Type : `any`
Defined in projects/app-base-library/src/lib/angular/services/app-base.service.ts:31

Public db
`db: `AngularFireDatabase``
Type : `AngularFireDatabase`
Defined in projects/app-base-library/src/lib/angular/services/app-base.service.ts:22

Private environment
`environment: `any``
Type : `any`
Defined in projects/app-base-library/src/lib/angular/services/app-base.service.ts:16

Public firebaseApps
`firebaseApps: `any``
Type : `any`
Default value : `{}`
Defined in projects/app-base-library/src/lib/angular/services/app-base.service.ts:18

Public firebaseUser
`firebaseUser: `Observable<firebase.User>``
Type : `Observable<firebase.User>`
Defined in projects/app-base-library/src/lib/angular/services/app-base.service.ts:19

Public headers
`headers: `Array<any>``
Type : `Array<any>`
Defined in projects/app-base-library/src/lib/angular/services/app-base.service.ts:17

    import {Injectable} from '@angular/core';
    import {Observable} from 'rxjs/Observable';
    import {AngularFireDatabase} from 'angularfire2/database';
    import {AngularFireAuth} from 'angularfire2/auth';
    import {NgxPermissionsService,NgxRolesService} from 'ngx-permissions';
    import {TranslateService} from '@ngx-translate/core';
    import {BaseService } from './base.service';
    import {ConfigService } from './config.service';
    import {configDefaults} from '../../configDefaults';
    import {i18n} from '../../shared/i18n';
    import * as firebase from 'firebase/app';

    @Injectable()
    export class AppBaseService {

        private environment: any;
        public headers: Array<any>;
        public firebaseApps: any = {};
        public firebaseUser: Observable<firebase.User>;

        constructor(
            public db: AngularFireDatabase,
            private baseService: BaseService,
            private config: ConfigService,
            private ngxPermissions: NgxPermissionsService,
            private rolesService: NgxRolesService,
            private translateService: TranslateService,
            private afAuth: AngularFireAuth) {
        }

        public api: any;

        public initApp(environment: any, configUrl: string) {
            this.environment = environment;
            this.config.setDefaults(configDefaults);
            this.config.set('environment', environment);
            if (environment.config) {
                this.config.extend(environment.config);
            }
            return new Promise((resolve, reject) => {
                let appOptions: any = {
                    // baseConfigUrl: configDefaults.baseConfigUrl,
                    baseService: this.baseService,
                    translateService: this.translateService,
                    configDefaults: configDefaults,
                };
                if (configUrl) {
                    appOptions.configUrl = configUrl;
                }
                this.initBaseApp(appOptions).then(() => {
                    this.initPermissions();
                    this.initFirebase();
                    this.initTranslation();
                    this.initDev().then(() => resolve());
                });
            });
        }

        public initBaseApp(options: any) {
            return new Promise((resolve) => {
                let opts: any = {};
                if (options.config) {
                    opts.config = options.config;
                }
                if (options.configUrl) {
                    opts.settingsFile = options.configUrl;
                }
                if (options.translateService) {
                    opts.translateService = options.translateService;
                }
                this.config.initialize(opts).then((settings: any) => {
                    resolve(settings);
                });
            });
        }

        public initRouter(router, component) {
            this.config.initRouter(router, component);
        }

        public initFirebase() {
            if(this.config.get('firebaseApps')) {
                Object.keys(this.config.get('firebaseApps')).forEach((k) => {
                    firebase.initializeApp(this.config.get('firebaseApps')[k], k);
                    // default firebaseApp
                    if(k === this.config.get('id')) {
                        this.config.set('firebase', this.config.get('firebaseApps')[k]);
                    }
                });
                this.firebaseApps = firebase.apps;
            }
            // this.db.object('config').set(this.config['config'])
            this.firebaseUser = this.afAuth.authState;
        }

        public initTranslation() {
            let language = this.config.get('language');
            // this language will be used as a fallback when a translation isn't found in the current language
            this.translateService.setDefaultLang(configDefaults.language);
            Object.keys(this.config.get('i18n')).forEach((k, i) => {
                if(i18n[k]) {
                    this.translateService.setTranslation(k, i18n[k]);
                }
            });
            // the language to use, if the lang isn't available, it will use the current loader to get them
            this.translateService.use(language);
        }

        public initPermissions() {
            let permissions: any[] = this.config.get('permissions');
            let roles = this.config.get('roles');
            configDefaults.permissions.forEach((permission) => {
                permissions.push(permission);
            });
            this.ngxPermissions.loadPermissions(permissions);
            Object.keys(configDefaults.roles).forEach((k, i) => {
                if (!roles[k]){
                    roles[k] = configDefaults.roles[k];
                }
            });
            // this.config.set('roles', roles );
            this.rolesService.addRole('ANONYMOUS',this.validateRole);
        }

        public validateRole(roleName) {
            // console.log('validateRole: ',roleName);
        }

        public loginFirebase(email, password) {
            return new Promise((resolve, reject) => {
                if (!email) {
                    this.afAuth.auth.signInAnonymously().then((res) => {
                        resolve(res);
                    });
                    return;
                }
                this.afAuth.auth.signInWithEmailAndPassword(email,password).then((res) => {
                    resolve(res);
                }).catch((err) => {
                    reject(err);
                });
            });
        }

        public logoutFirebase() {
            return new Promise((resolve, reject) => {
                this.afAuth.auth.signOut().then(() => {
                    resolve();
                });
            });
        }

        private initDev() {
            return new Promise((resolve, reject) => {
                if (!this.environment.production && !this.config.get('dev')) {
                    resolve();
                    return;
                };
                this.rolesService.addRole('DEVELOPER',this.validateRole);
                this.config.lock('dev');
                let settings = this.config.all();

                if (settings.autoLogin) {
                    // dev: load credentials
                    this.baseService.fetchCredentials(settings.credentialsUrl).then((credentials) => {
                        if (settings.firebase && this.baseService.credentials[settings.firebase.authDomain]) {
                            // dev: firebase auto login
                            let c = credentials[settings.firebase.authDomain];
                            this.loginFirebase(c.email,c.password).then(() => {
                                this.rolesService.addRole('OWNER',this.validateRole);
                                this.config.set('authenticated',true);
                                // fixme:
                                // this.db.object('config').set(this.config.all())
                                resolve();
                            });
                        } else {
                            resolve();
                        }
                    }, () => {
                        resolve();
                    });
                } else {
                    resolve();
                }

            });

        }

    }

# results matching ""

# No results matching ""