Fix for redirection to login page

This commit is contained in:
piotrpekala7 2021-05-07 20:30:31 +02:00
parent 06ad9c2f07
commit a5b4a12849
3 changed files with 26 additions and 7 deletions

View File

@ -22,6 +22,7 @@ export class LoginComponent implements OnInit {
public version: string; public version: string;
public isLightThemeEnabled: boolean = false; public isLightThemeEnabled: boolean = false;
public loginError: boolean = false; public loginError: boolean = false;
public returnUrl: string = "";
loginForm = new FormGroup({ loginForm = new FormGroup({
username: new FormControl('', [Validators.required]), username: new FormControl('', [Validators.required]),
@ -41,6 +42,7 @@ export class LoginComponent implements OnInit {
async ngOnInit() { async ngOnInit() {
const server_id = this.route.snapshot.paramMap.get('server_id'); 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.serverService.get(parseInt(server_id, 10)).then((server: Server) => {
this.server = server; this.server = server;
@ -72,7 +74,12 @@ export class LoginComponent implements OnInit {
server.authToken = response.access_token; server.authToken = response.access_token;
await this.serverService.update(server); 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 => { }, error => {
this.loginError = true; this.loginError = true;
}); });

View File

@ -1,19 +1,20 @@
import { Injectable } from '@angular/core'; 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 { Server } from 'app/models/server';
import { ServerService } from '../services/server.service'; import { ServerService } from '../services/server.service';
@Injectable() @Injectable()
export class LoginGuard implements CanActivate { export class LoginGuard implements CanActivate {
constructor( constructor(
private serverService: ServerService private serverService: ServerService,
private router: Router
) {} ) {}
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) { canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const server_id = next.paramMap.get('server_id'); const server_id = next.paramMap.get('server_id');
return this.serverService.get(parseInt(server_id, 10)).then((server: Server) => { return this.serverService.get(parseInt(server_id, 10)).then((server: Server) => {
if (server.authToken) return true; if (server.authToken) return true;
return false this.router.navigate(['/server', server.id, 'login'], { queryParams: { returnUrl: state.url }});
}); });
} }
} }

View File

@ -21,6 +21,7 @@ export class DefaultLayoutComponent implements OnInit, OnDestroy {
public isInstalledSoftwareAvailable = false; public isInstalledSoftwareAvailable = false;
public uiVersion = version; public uiVersion = version;
public isLoginPage = false; public isLoginPage = false;
public routeSubscription;
serverStatusSubscription: Subscription; serverStatusSubscription: Subscription;
shouldStopServersOnClosing = true; shouldStopServersOnClosing = true;
@ -41,9 +42,10 @@ export class DefaultLayoutComponent implements OnInit, OnDestroy {
) {} ) {}
ngOnInit() { ngOnInit() {
if (this.router.url.includes("login")) { this.checkIfUserIsLoginPage();
this.isLoginPage = true; this.routeSubscription = this.router.events.subscribe((val) => {
}; if (val instanceof NavigationEnd) this.checkIfUserIsLoginPage();
});
this.recentlyOpenedServerId = this.recentlyOpenedProjectService.getServerId(); this.recentlyOpenedServerId = this.recentlyOpenedProjectService.getServerId();
this.recentlyOpenedProjectId = this.recentlyOpenedProjectService.getProjectId(); this.recentlyOpenedProjectId = this.recentlyOpenedProjectService.getProjectId();
@ -67,6 +69,14 @@ export class DefaultLayoutComponent implements OnInit, OnDestroy {
this.shouldStopServersOnClosing = this.electronService.isElectronApp; this.shouldStopServersOnClosing = this.electronService.isElectronApp;
} }
checkIfUserIsLoginPage() {
if (this.router.url.includes("login")) {
this.isLoginPage = true;
} else {
this.isLoginPage = false;
}
}
logout() { logout() {
let serverId = this.router.url.split("/server/")[1].split("/")[0]; let serverId = this.router.url.split("/server/")[1].split("/")[0];
this.serverService.get(+serverId).then((server: Server) => { this.serverService.get(+serverId).then((server: Server) => {
@ -104,5 +114,6 @@ export class DefaultLayoutComponent implements OnInit, OnDestroy {
ngOnDestroy() { ngOnDestroy() {
this.serverStatusSubscription.unsubscribe(); this.serverStatusSubscription.unsubscribe();
this.routeSubscription.unsubscribe();
} }
} }