在我的角度应用程序中,我正在尝试使用google oAuth进行身份验证。我使用了 angular5-social-login 库。问题是在检查routeGuard服务中的凭据时,我的凭据为null。
app.module
const appRoutes: Routes = [
{ path: 'login/:reason', component: LoginComponent },
{
path: '', canActivate: [AuthGuardService], children: [
{ path: 'dashboard', loadChildren: './dashboard/dashboard.module#DashboardModule' },
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: '*', redirectTo: '/dashboard', pathMatch: 'full' }]
}];
export function getAuthServiceConfigs() {
let config = new AuthServiceConfig(
[
{
id: GoogleLoginProvider.PROVIDER_ID,
provider: new GoogleLoginProvider(environment.GoogleClientID)
},
]
);
return config;
}
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
SocialLoginModule, //for Google oAuth
BrowserAnimationsModule,
HttpClientModule,
RouterModule.forRoot(appRoutes)
],
providers: [
{
provide: AuthServiceConfig,
useFactory: getAuthServiceConfigs
},
AuthGuardService],
bootstrap: [AppComponent]
})
export class AppModule { }
AUTH-guard.service
@Injectable()
export class AuthGuardService implements CanActivate {
user: SocialUser;
constructor(private socialAuthService: AuthService, private _router: Router) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (localStorage.getItem('socialUser')) {
this.socialAuthService.authState.subscribe(user => this.user = user);
if (this.user != null) {
return true
}
}
}
下次调用时,用户变量不为空,此问题仅在启动时发生。如何使用此库使其工作?如果不可能,请建议我一个好的认证库?