You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular 13添加withCredentials: true后登录导航失效问题

登录后无法跳转至/admin页面问题

问题现象

  • 登录流程可正常完成,但登录后执行页面导航代码(this.router.navigate(['/admin']))无实际跳转效果
  • 该功能此前运行正常,在给所有GET请求添加withCredentials: true并启用认证Cookie后出现异常

排查情况

  • 定位到AuthGuard服务的canActivate方法返回false,根源是this.sessionService.isUserSignedIn()返回false
  • 初步推测:会话服务未在登录成功后更新用户登录状态,后续将同步排查进展

相关代码

onSignInButtonClick(event: any) {
        this.hasError = false;
        this.errorMessage = '';
        this.isLoading = true;

        this.authService
            .signIn(this.username, this.password)
            .pipe(takeUntil(this.ngUnsubscribe.asObservable()))
            .subscribe(
                (user) => {
                    console.log('Sign in successful.');
                    if (this.authGuard.redirectUrl) {
                        console.log(
                            'Redirecting to ' +
                                this.authGuard.redirectUrl +
                                '...'
                        );
                        this.authGuard.usingAuthGuardUrl = false;
                        this.router.navigateByUrl(this.authGuard.redirectUrl);
                    }
                    else {
                            console.log('Redirecting to admin...');
                            this.router.navigate([`/admin`]);
                    }
                },
                (error) => {
                    this.isLoading = false;
                    this.hasError = true;
                    console.log('error:', error);
                    this.errorMessage =
                        "The username and password combination you entered didn't work :(";
                }
            );
    }

内容的提问来源于stack exchange,提问作者Chris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 08:37:41