import { Injectable } from '@angular/core'; import { Model } from "./model"; import * as moment from 'moment'; @Injectable() export class AuthService extends Model{ intendedUrl: string; person: M; // id: number; signIn(id: string, secret: string, remember: boolean, callback: (status: boolean) => void, type: string = 'user'){ // this.go((agent) => { // agent.person(type).token({ // email: id, // password: secret // }, (response) => { // if(response){ // this.store.set('authToken', response['token']); // this.store.set('authName', response['user']['first_name'] + ' ' + response['user']['last_name']); // this.store.set('authTime', moment()); // this.store.set('status', true); // if(remember){ // this.store.set('remember', remember); // } // agent.resource('users').rows((userResponse)=>{ // this.store.set('authUser', JSON.stringify(userResponse['users'][0])); // this.store.set('authId', userResponse['users'][0]['id']); // this.store.set('authRoleId', userResponse['users'][0]['role_id']); // this.store.set('authType', userResponse['users'][0]['user_type']); // //get role details // // agent.resource('roles', this.store.get('authRoleId')).row((response) => { // // this.store.set('authRole', JSON.stringify(response['role'])); // callback(true); // // }); // }, this.cleverQuery.simple([{ and: [{key: 'email', operator: 'LIKE', value: `%${response['user']['email'].split('@')[0].toLowerCase()}%`}]}], ['and'])); // } // else{ // this.store.set('status', false); // callback(false); // } // }); // }); } signOut(){ this.store.remove([ 'authToken', 'authRoleId', 'authUser', 'authRole', 'remember', 'authName', 'authTime', 'status', 'authId', 'authType', 'status' ]); } expiry(): boolean{ let hours: number = (this.store.get('remember')) ? 24*7 : 24*1; return (moment().diff(this.store.get('authTime'), 'hours') > hours); } check(): boolean{ return (this.store.get('status') && !this.expiry()); } role(){ return (this.check()) ? this.store.get('authRole') : null; } // id(){ // return (this.check()) ? this.store.get('authId') : null; // } type(){ return (this.check()) ? this.store.get('authType') : null; } user(){ return (this.check()) ? this.store.get('authUser') : null; } }