import { Injectable } from '@angular/core'; import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router'; import { Observable } from 'rxjs'; import { PermissionService } from '../services/permission.service'; import { SessionStore } from '../store/session.service'; import { RouteService } from 'src/app/services/route.service' import { FirstEnterService } from 'src/app/services/first-enter.service' import { AlertController, Platform } from '@ionic/angular'; @Injectable({ providedIn: 'root' }) export class AuthGuard implements CanActivate { constructor( private router:Router, public p: PermissionService, private RouteService: RouteService, private FirstEnterService: FirstEnterService, private alertController: AlertController, ){} canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | Promise | boolean | UrlTree { console.log('hire auth!', state.url) // if user not active or no session if(!SessionStore.user.Inactivity || !SessionStore.exist) { console.log('no session', !SessionStore.user.Inactivity, !SessionStore.exist) console.log(SessionStore.user) if(SessionStore.user.LoginPreference == 'Pin') { this.router.navigate(['/pin']); } else { this.router.navigate(['/']); // console.log('goto login page') } return false } else { const pathname = state.url if(pathname.startsWith('/home/agenda')) { if(this.p.userPermission(this.p.permissionList.Agenda.access)) { return true; } else { this.router.navigate(['/login']); console.log('£1') return false; } } else if ( pathname.startsWith('/home/gabinete-digital')) { if(this.p.userPermission(this.p.permissionList.Gabinete.access)) { return true; } else { this.router.navigate(['/login']); console.log('£2') return false; } } else if(pathname.startsWith('/home/chat')) { if(this.p.userPermission(this.p.permissionList.Chat.access)) { return true; } else { console.log("no access to chat") console.log('£3') this.router.navigate(['/login']); return false; } } else if(pathname.startsWith('/home/publications')) { if(this.p.userPermission(this.p.permissionList.Actions.access)) { return true } else { console.log('£4') this.router.navigate(['/login']); return false } } else if (pathname.startsWith('/home/events')) { if(SessionStore.user.OwnerCalendars.length >= 1 || this.p.userPermission([this.p.permissionList.Gabinete.access])) { return true } else { console.log('£5') this.router.navigate(['/login']); return false } } else if (pathname == '/') { console.log('£6') this.router.navigate(['/login']); console.log('no path') return false } else { console.log('£7') console.log('pathname not match') this.router.navigate(['/login']); return false } } return true } }