2021-04-13 21:53:58 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import {
|
|
|
|
ActivatedRouteSnapshot,
|
|
|
|
CanActivate,
|
|
|
|
Router,
|
|
|
|
RouterStateSnapshot
|
|
|
|
} from '@angular/router';
|
|
|
|
|
2021-05-19 20:36:44 +02:00
|
|
|
import { SettingsStorageService } from '../services/settings-storage.service';
|
2021-04-13 21:53:58 +02:00
|
|
|
import { TokenStorageService } from '../services/token-storage.service';
|
|
|
|
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
|
|
export class AuthGuard implements CanActivate {
|
|
|
|
constructor(
|
|
|
|
private router: Router,
|
2021-05-19 20:36:44 +02:00
|
|
|
private settingsStorageService: SettingsStorageService,
|
2021-04-13 21:53:58 +02:00
|
|
|
private tokenStorageService: TokenStorageService
|
|
|
|
) {}
|
|
|
|
|
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
2021-05-19 20:36:44 +02:00
|
|
|
if (route.queryParams?.utm_source) {
|
|
|
|
this.settingsStorageService.setSetting(
|
|
|
|
'utm_source',
|
|
|
|
route.queryParams?.utm_source
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-13 21:53:58 +02:00
|
|
|
const isLoggedIn = !!this.tokenStorageService.getToken();
|
|
|
|
|
|
|
|
if (isLoggedIn) {
|
|
|
|
if (state.url === '/start') {
|
|
|
|
this.router.navigate(['/home']);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not logged in
|
|
|
|
if (state.url !== '/start') {
|
|
|
|
this.router.navigate(['/start']);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|