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
相关产品推荐
相关产品推荐

