diff --git a/src/app/components/login/login.component.ts b/src/app/components/login/login.component.ts index 041b0d03..0ff9ac01 100644 --- a/src/app/components/login/login.component.ts +++ b/src/app/components/login/login.component.ts @@ -22,6 +22,7 @@ export class LoginComponent implements OnInit { public version: string; public isLightThemeEnabled: boolean = false; public loginError: boolean = false; + public returnUrl: string = ""; loginForm = new FormGroup({ username: new FormControl('', [Validators.required]), @@ -41,6 +42,7 @@ export class LoginComponent implements OnInit { async ngOnInit() { const server_id = this.route.snapshot.paramMap.get('server_id'); + this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/'; this.serverService.get(parseInt(server_id, 10)).then((server: Server) => { this.server = server; @@ -72,7 +74,12 @@ export class LoginComponent implements OnInit { server.authToken = response.access_token; await this.serverService.update(server); - this.router.navigate(['/server', this.server.id, 'projects']); + if (this.returnUrl.length <= 1) { + this.router.navigate(['/server', this.server.id, 'projects']) + } else { + this.router.navigateByUrl(this.returnUrl); + } + }, error => { this.loginError = true; }); diff --git a/src/app/guards/login-guard.ts b/src/app/guards/login-guard.ts index f4182705..5b34f0c6 100644 --- a/src/app/guards/login-guard.ts +++ b/src/app/guards/login-guard.ts @@ -1,19 +1,20 @@ import { Injectable } from '@angular/core'; -import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot } from '@angular/router'; +import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router'; import { Server } from 'app/models/server'; import { ServerService } from '../services/server.service'; @Injectable() export class LoginGuard implements CanActivate { constructor( - private serverService: ServerService + private serverService: ServerService, + private router: Router ) {} canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) { const server_id = next.paramMap.get('server_id'); return this.serverService.get(parseInt(server_id, 10)).then((server: Server) => { if (server.authToken) return true; - return false + this.router.navigate(['/server', server.id, 'login'], { queryParams: { returnUrl: state.url }}); }); } } diff --git a/src/app/layouts/default-layout/default-layout.component.ts b/src/app/layouts/default-layout/default-layout.component.ts index 2c174bf9..eb42edd5 100644 --- a/src/app/layouts/default-layout/default-layout.component.ts +++ b/src/app/layouts/default-layout/default-layout.component.ts @@ -21,6 +21,7 @@ export class DefaultLayoutComponent implements OnInit, OnDestroy { public isInstalledSoftwareAvailable = false; public uiVersion = version; public isLoginPage = false; + public routeSubscription; serverStatusSubscription: Subscription; shouldStopServersOnClosing = true; @@ -41,9 +42,10 @@ export class DefaultLayoutComponent implements OnInit, OnDestroy { ) {} ngOnInit() { - if (this.router.url.includes("login")) { - this.isLoginPage = true; - }; + this.checkIfUserIsLoginPage(); + this.routeSubscription = this.router.events.subscribe((val) => { + if (val instanceof NavigationEnd) this.checkIfUserIsLoginPage(); + }); this.recentlyOpenedServerId = this.recentlyOpenedProjectService.getServerId(); this.recentlyOpenedProjectId = this.recentlyOpenedProjectService.getProjectId(); @@ -67,6 +69,14 @@ export class DefaultLayoutComponent implements OnInit, OnDestroy { this.shouldStopServersOnClosing = this.electronService.isElectronApp; } + checkIfUserIsLoginPage() { + if (this.router.url.includes("login")) { + this.isLoginPage = true; + } else { + this.isLoginPage = false; + } + } + logout() { let serverId = this.router.url.split("/server/")[1].split("/")[0]; this.serverService.get(+serverId).then((server: Server) => { @@ -104,5 +114,6 @@ export class DefaultLayoutComponent implements OnInit, OnDestroy { ngOnDestroy() { this.serverStatusSubscription.unsubscribe(); + this.routeSubscription.unsubscribe(); } }