Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 8 auth guard-确定用户是来自应用程序外部还是内部_Angular_Angular2 Routing_Angular8_Rxjs6_Angular Router Guards - Fatal编程技术网

Angular 8 auth guard-确定用户是来自应用程序外部还是内部

Angular 8 auth guard-确定用户是来自应用程序外部还是内部,angular,angular2-routing,angular8,rxjs6,angular-router-guards,Angular,Angular2 Routing,Angular8,Rxjs6,Angular Router Guards,这似乎是个愚蠢的问题,但由于某种原因我想不出答案 我有一个auth-guard,它检查本地存储中存储的JWT令牌的过期情况,并抛出“您的会话已过期”对话框,将用户重定向到登录onClose 在当前状态下,仅当浏览器在存储器中具有此令牌时,才会引发对话框;否则它会直接重定向到登录页面 canActivate( next: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { if (!this.

这似乎是个愚蠢的问题,但由于某种原因我想不出答案

我有一个auth-guard,它检查本地存储中存储的JWT令牌的过期情况,并抛出“您的会话已过期”对话框,将用户重定向到登录onClose

在当前状态下,仅当浏览器在存储器中具有此令牌时,才会引发对话框;否则它会直接重定向到登录页面

canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): boolean {
        if (!this._auth.isAuthenticated()) {
            if (this._auth.hasToken()) {
                this._dialogService.openErrorDialog('Your session has expired, please re-login', 0)
                    .afterClosed()
                    .subscribe(() => this.navigateToLogin(state.url));
            } else {
                this.navigateToLogin(state.url);
            }

            return false;
        }

    return true;
}

我想实现以下逻辑:当用户在某个应用页面上浏览时令牌过期->用户导航到另一个页面->抛出对话框->重定向到登录;但如果用户从外部导航到应用程序中的某个页面,它应该直接重定向登录页面,而不会抛出对话框,即使浏览器本地存储中已经有过期的令牌

我尝试了基于的一些解决方案,但没有成功-尽管我可以从canActivate函数返回一个
可观察的
,我不知道如何使rxjs
map
成对的结果一起工作,这样我就可以利用前一个URL存在的事实来确定用户来自何处

非常感谢您的帮助。

为您创建了示例。使用您在中共享的信息并创建了如下服务

@Injectable({ providedIn: "root" })
export class FirstVisitService {

  readonly isFirstVisit = new BehaviorSubject<boolean>(true);

  constructor(router: Router) {
    router.events
      .pipe(
        filter(event => event instanceof NavigationEnd),
        first()
      )
      .subscribe((e: NavigationEnd) => {
        this.isFirstVisit.next(false);
        this.isFirstVisit.complete();
      });
  }
}
因此,它会在任何事件发生之前发生并订阅路由器事件。

为您创建了示例。使用您在中共享的信息并创建了如下服务

@Injectable({ providedIn: "root" })
export class FirstVisitService {

  readonly isFirstVisit = new BehaviorSubject<boolean>(true);

  constructor(router: Router) {
    router.events
      .pipe(
        filter(event => event instanceof NavigationEnd),
        first()
      )
      .subscribe((e: NavigationEnd) => {
        this.isFirstVisit.next(false);
        this.isFirstVisit.complete();
      });
  }
}

因此,它会在任何事件发生之前发生并订阅路由器事件。

只需在
src/app/app.components.ts
中设置重定向即可
此“组件”是AppModule启动后第一个启动的组件。
所以只要把线放在那里,当用户来自外部时,它总是重定向


//src/app/app.components.ts
从'@angular/core'导入{Component};
从'@angular/Router'导入{Router};
@组成部分({
选择器:'应用程序根',
templateUrl:“./app.component.html”,
样式URL:['./app.component.scss'],
})
导出类AppComponent{
构造函数(专用路由器:路由器){
//无论用户要去哪里
//只需重定向到登录!
//这将仅在应用程序首次启动时运行

这个.router.navigateByUrl('/login');//只需在
src/app/app.components.ts
中设置重定向即可 此“组件”是AppModule启动后第一个启动的组件。
所以只要把线放在那里,当用户来自外部时,它总是重定向


//src/app/app.components.ts
从'@angular/core'导入{Component};
从'@angular/Router'导入{Router};
@组成部分({
选择器:'应用程序根',
templateUrl:“./app.component.html”,
样式URL:['./app.component.scss'],
})
导出类AppComponent{
构造函数(专用路由器:路由器){
//无论用户要去哪里
//只需重定向到登录!
//这将仅在应用程序首次启动时运行

this.router.navigateByUrl(“/login”);//用户从外部导航到应用程序中的某个页面-您的意思是要检测用户何时跟随深度链接访问您网站上的某个页面?“但是如果用户从外部导航到应用程序中的某个页面,则应直接重定向登录页面”-将该逻辑置于
app.component
或其他位置,用户一旦进入您的应用程序就会进行初始化。用户从外部导航到应用程序中的某个页面-您的意思是要检测用户何时跟随深度链接访问您网站上的某个页面?但如果用户从外部导航到应用程序中的某个页面,它应该直接将登录页面重定向给他"-将该逻辑放在
app.component
或其他用户在进入您的应用程序时进行初始化的地方。这不是我想要的。如果用户确实拥有有效令牌,则不应将其重定向到登录。虽然我可以将auth服务注入
AppComponent
并实现该逻辑,但我更喜欢Eldar的解决方案,因为它不支持ix在组件和保护逻辑之间。谢谢:)这不是我想要的。如果用户确实有有效的令牌,它不应该被重定向到登录。虽然我可以将身份验证服务注入
AppComponent
并实现此逻辑,但我更喜欢Eldar的解决方案,因为它不会在组件和保护逻辑之间混合。谢谢:)
export class AppModule {
  constructor(f : FirstVisitService) {}
}