mirror of
https://github.com/bitwarden/jslib
synced 2025-12-10 21:33:17 +00:00
Make routing guards redirect relative to root
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
import { Injectable } from "@angular/core";
|
import { Injectable } from "@angular/core";
|
||||||
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from "@angular/router";
|
import {
|
||||||
|
ActivatedRoute,
|
||||||
|
ActivatedRouteSnapshot,
|
||||||
|
CanActivate,
|
||||||
|
Router,
|
||||||
|
RouterStateSnapshot,
|
||||||
|
} from "@angular/router";
|
||||||
|
|
||||||
import { AuthService } from "jslib-common/abstractions/auth.service";
|
import { AuthService } from "jslib-common/abstractions/auth.service";
|
||||||
import { KeyConnectorService } from "jslib-common/abstractions/keyConnector.service";
|
import { KeyConnectorService } from "jslib-common/abstractions/keyConnector.service";
|
||||||
@@ -12,7 +18,8 @@ export class AuthGuard implements CanActivate {
|
|||||||
private authService: AuthService,
|
private authService: AuthService,
|
||||||
private router: Router,
|
private router: Router,
|
||||||
private messagingService: MessagingService,
|
private messagingService: MessagingService,
|
||||||
private keyConnectorService: KeyConnectorService
|
private keyConnectorService: KeyConnectorService,
|
||||||
|
private activatedRoute: ActivatedRoute
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async canActivate(route: ActivatedRouteSnapshot, routerState: RouterStateSnapshot) {
|
async canActivate(route: ActivatedRouteSnapshot, routerState: RouterStateSnapshot) {
|
||||||
@@ -27,14 +34,19 @@ export class AuthGuard implements CanActivate {
|
|||||||
if (routerState != null) {
|
if (routerState != null) {
|
||||||
this.messagingService.send("lockedUrl", { url: routerState.url });
|
this.messagingService.send("lockedUrl", { url: routerState.url });
|
||||||
}
|
}
|
||||||
return this.router.createUrlTree(["lock"], { queryParams: { promptBiometric: true } });
|
return this.router.createUrlTree(["lock"], {
|
||||||
|
queryParams: { promptBiometric: true },
|
||||||
|
relativeTo: this.activatedRoute.root,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!routerState.url.includes("remove-password") &&
|
!routerState.url.includes("remove-password") &&
|
||||||
(await this.keyConnectorService.getConvertAccountRequired())
|
(await this.keyConnectorService.getConvertAccountRequired())
|
||||||
) {
|
) {
|
||||||
return this.router.createUrlTree(["/remove-password"]);
|
return this.router.createUrlTree(["/remove-password"], {
|
||||||
|
relativeTo: this.activatedRoute.root,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Injectable } from "@angular/core";
|
import { Injectable } from "@angular/core";
|
||||||
import { CanActivate, Router } from "@angular/router";
|
import { ActivatedRoute, CanActivate, Router } from "@angular/router";
|
||||||
|
|
||||||
import { AuthService } from "jslib-common/abstractions/auth.service";
|
import { AuthService } from "jslib-common/abstractions/auth.service";
|
||||||
import { AuthenticationStatus } from "jslib-common/enums/authenticationStatus";
|
import { AuthenticationStatus } from "jslib-common/enums/authenticationStatus";
|
||||||
@@ -8,7 +8,11 @@ import { AuthenticationStatus } from "jslib-common/enums/authenticationStatus";
|
|||||||
export class LockGuard implements CanActivate {
|
export class LockGuard implements CanActivate {
|
||||||
protected homepage = "vault";
|
protected homepage = "vault";
|
||||||
protected loginpage = "login";
|
protected loginpage = "login";
|
||||||
constructor(private authService: AuthService, private router: Router) {}
|
constructor(
|
||||||
|
private authService: AuthService,
|
||||||
|
private router: Router,
|
||||||
|
private activatedRoute: ActivatedRoute
|
||||||
|
) {}
|
||||||
|
|
||||||
async canActivate() {
|
async canActivate() {
|
||||||
const authStatus = await this.authService.getAuthStatus();
|
const authStatus = await this.authService.getAuthStatus();
|
||||||
@@ -20,6 +24,6 @@ export class LockGuard implements CanActivate {
|
|||||||
const redirectUrl =
|
const redirectUrl =
|
||||||
authStatus === AuthenticationStatus.LoggedOut ? [this.loginpage] : [this.homepage];
|
authStatus === AuthenticationStatus.LoggedOut ? [this.loginpage] : [this.homepage];
|
||||||
|
|
||||||
return this.router.createUrlTree([redirectUrl]);
|
return this.router.createUrlTree([redirectUrl], { relativeTo: this.activatedRoute.root });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Injectable } from "@angular/core";
|
import { Injectable } from "@angular/core";
|
||||||
import { CanActivate, Router } from "@angular/router";
|
import { ActivatedRoute, CanActivate, Router } from "@angular/router";
|
||||||
|
|
||||||
import { AuthService } from "jslib-common/abstractions/auth.service";
|
import { AuthService } from "jslib-common/abstractions/auth.service";
|
||||||
import { AuthenticationStatus } from "jslib-common/enums/authenticationStatus";
|
import { AuthenticationStatus } from "jslib-common/enums/authenticationStatus";
|
||||||
@@ -7,7 +7,11 @@ import { AuthenticationStatus } from "jslib-common/enums/authenticationStatus";
|
|||||||
@Injectable()
|
@Injectable()
|
||||||
export class UnauthGuard implements CanActivate {
|
export class UnauthGuard implements CanActivate {
|
||||||
protected homepage = "vault";
|
protected homepage = "vault";
|
||||||
constructor(private authService: AuthService, private router: Router) {}
|
constructor(
|
||||||
|
private authService: AuthService,
|
||||||
|
private router: Router,
|
||||||
|
private activatedRoute: ActivatedRoute
|
||||||
|
) {}
|
||||||
|
|
||||||
async canActivate() {
|
async canActivate() {
|
||||||
const authStatus = await this.authService.getAuthStatus();
|
const authStatus = await this.authService.getAuthStatus();
|
||||||
@@ -17,9 +21,9 @@ export class UnauthGuard implements CanActivate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (authStatus === AuthenticationStatus.Locked) {
|
if (authStatus === AuthenticationStatus.Locked) {
|
||||||
return this.router.createUrlTree(["lock"]);
|
return this.router.createUrlTree(["lock"], { relativeTo: this.activatedRoute.root });
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.router.createUrlTree([this.homepage]);
|
return this.router.createUrlTree([this.homepage], { relativeTo: this.activatedRoute.root });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user