[英]Angular Auth Guard is not navigating to new Route
我的 Auth Guard 在不登录的情况下尝试搜索 URL 时按预期工作。但是,登录时,URL 中的导航不会改变,用户也不会被路由到正确的页面。 但是,它运行正确的登录方法以重新路由到预期页面,所以我不确定为什么不是,或者在使用 Auth Guard 时我是否缺少某种潜在规则。 我也在使用延迟加载的主页,以防万一与它有关...
我的授权守卫...
import { Injectable } from '@angular/core';
import { CanLoad, Route, Router, UrlSegment } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from './auth.service';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanLoad {
constructor(
private authService: AuthService,
private router: Router
) {}
canLoad(route: Route, segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean {
if (this.authService.isLoggedIn !== true) {
this.router.navigateByUrl('/')
}
return true;
}
}
我的登录方法。 注意其中的 console.log()。 它在使用 Auth Guard 时运行,证明它应该可以工作......
signIn(email: string, password: string) {
return this.afAuth.signInWithEmailAndPassword(email, password)
.then((result) => {
this.ngZone.run(() => {
this.isLoggedIn$.next(true);
console.log('Sign in method...');
this.router.navigateByUrl('/home');
});
this.SetUserData(result.user);
}).catch((error) => {
window.alert(error.message)
})
}
我的 Getter 方法为 Auth Guard 返回一个布尔值......
get isLoggedIn(): boolean {
const user = JSON.parse(localStorage.getItem('user'));
return (user !== null) ? true : false;
}
我的懒加载路线...
const routes: Routes = [
{
path: 'home',
canLoad: [AuthGuard],
loadChildren: () =>
import('./home/home.module').then(m => m.HomeModule)
},
{
path: 'onTheirWay',
canLoad: [AuthGuard],
loadChildren: () =>
import('./on-their-way/on-their-way.module').then(m => m.OnTheirWayModule)
},
{
path: 'preorders',
canLoad: [AuthGuard],
loadChildren: () =>
import('./preorders/preorders.module').then(m => m.PreordersModule)
}
];
更新
我也应该澄清一下。 如果我去掉 Routes 文件中的 Auth Guard,登录方法会导航到正确的页面。 它与Auth Guard有关。 添加如上所示的代码会影响导航运行。
更新
这是我的 Auth 组件的路由文件,其中包含一个“/”路径...
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { SigninComponent } from './signin/signin.component';
import { SignoutComponent } from './signout/signout.component';
import { SignupComponent } from './signup/signup.component';
const routes: Routes = [
{ path: 'signout', component: SignoutComponent },
{ path: 'signup', component: SignupComponent },
{ path: '', component: SigninComponent }
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class AuthRoutingModule { }
更新
在 signIn 方法中,我尝试在构造函数中注入一个 route:ActivatedRoute 并替换
this.router.navigateByUrl('/home');
和
this.router.navigate(['/home'], { relativeTo: this.route });
它有时似乎有效*,但有时却无效。 所以我不确定是什么原因造成的。 任何精通路由及其与 Auth Guard 关系的人,或者知道为什么会发生这种情况?
重定向后return false
。
canLoad(route: Route, segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean {
if (this.authService.isLoggedIn !== true) {
this.router.navigateByUrl('/');
return false; // <-----------------------
}
return true;
}
它不是逐行执行。 将 return true 包装到 else 块中。
canLoad(route: Route, segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean {
if (this.authService.isLoggedIn !== true) {
this.router.navigateByUrl('/')
} else {
return true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.