我正在尝试使用 Angular Social Login npm 包实现 Angular Guard:
@Injectable({
providedIn: 'root',
})
export class LoginGuard implements CanActivate {
constructor(private router: Router, private authService: SocialAuthService) {}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean> {
console.log('guard');
return this.authService.authState.pipe(
map((socialUser: SocialUser) => {
console.log('map');
return !!socialUser;
}),
catchError((error) => {
console.log(error);
return of(false);
})
);
}
}
我可以看到guard
正在打印到控制台。但是,我看不到map
打印到控制台。为什么?
如果我做:
@Injectable({
providedIn: 'root',
})
export class LoginGuard implements CanActivate {
constructor(private router: Router, private authService: SocialAuthService) {}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean> {
console.log('guard');
return new Observable<boolean>((subscriber) => {
console.log('subscriber');
subscriber.next(true);
});
}
}
我可以看到subscriber
正在打印到控制台。
我究竟做错了什么?
我希望能够将此警卫添加到路线中。如果用户未登录,它将被重定向到登录页面(我知道我还没有实现该逻辑;我已经尝试过但失败了,因为我似乎没有得到带有值的 Observable?)。请注意,我使用 Google 作为提供者:
@NgModule({
declarations: [
AppComponent,
IndexPageComponent,
SearchPageComponent,
InputKeywordsComponent,
DisplayNewsListComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
ReactiveFormsModule,
HttpClientModule,
SocialLoginModule,
],
providers: [
{
provide: 'SocialAuthServiceConfig',
useValue: {
autoLogin: false,
providers: [
{
id: GoogleLoginProvider.PROVIDER_ID,
provider: new GoogleLoginProvider(
'superVerySecret'
),
},
],
} as SocialAuthServiceConfig,
},
],
bootstrap: [AppComponent],
})
export class AppModule {}
和路由模块:
const routes: Routes = [
{ path: '', component: IndexPageComponent },
{ path: 'search', component: SearchPageComponent, canActivate: [LoginGuard] },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: [LoginGuard]
})
export class AppRoutingModule {}
PS1:我可以登录就好了。
PS2:
- @角/核心@12.2.13
- @rxjs@6.6.7
- angularx-social-login@4.0.1