如何从我的应用程序组件中的 auth.service 获取 isLoggedIn 布尔值
How to get isLoggedIn boolan value from auth.service in my app components
我正在尝试为我的应用创建 Firebase 身份验证。
这是auth.service.ts
private isloggedIn = false;
constructor(private af: AngularFire, private router: Router) {
// this.af.auth.subscribe((auth) => console.log(auth));
this.af.auth.subscribe(user => {
if (user) {
this.isloggedIn = 'true';
} else {
this.isloggedIn = 'false';
}
});
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.af.auth.map((auth) => {
if(auth == null) {
this.router.navigate(['/auth']);
return false;
} else {
return true;
}
}).first()
}
isLoggedIn () {
return this.isloggedIn;
}
我想做的是观察认证状态。如果发生变化,那么 this.isloggedIn 也会发生变化。
那么如何在我的组件中获取 isloggedin auth.service 值。
import { AuthService } from './auth.service';
export class SomeComponent implements OnInit {
constructor(private titleService: Title, private AuthService : AuthService, public toastr: ToastsManager, private af: AngularFire) {
}
ngOnInit() {
this.titleService.setTitle("some component title");
console.log(this.AuthService.isLoggedIn());
}
目前,当我使用 this.AuthService.isLoggedIn() 时,即使用户已登录,也会给我未定义的信息。我做错了什么?
这可能是因为在您的服务器响应可用之前调用了 isLoggedIn()
。您需要确保异步调用正确链接,否则您无法确定之前的调用是否已经完成。
private isloggedIn:Subject = new BehaviorSubject(false);
constructor(private af: AngularFire, private router: Router) {
this.isLoggedIn = this.af.auth.map(user => { // map instead of subscribe
if (user) {
this.isloggedIn = 'true';
} else {
this.isloggedIn = 'false';
}
});
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.isLoggedIn.map((auth) => {
if(auth == null) {
this.router.navigate(['/auth']);
return false;
} else {
return true;
}
}).first()
}
ngOnInit() {
this.titleService.setTitle("some component title");
console.log(this.AuthService.isLoggedIn.subscribe(value => console.log(value));
}
另见
我正在尝试为我的应用创建 Firebase 身份验证。
这是auth.service.ts
private isloggedIn = false;
constructor(private af: AngularFire, private router: Router) {
// this.af.auth.subscribe((auth) => console.log(auth));
this.af.auth.subscribe(user => {
if (user) {
this.isloggedIn = 'true';
} else {
this.isloggedIn = 'false';
}
});
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.af.auth.map((auth) => {
if(auth == null) {
this.router.navigate(['/auth']);
return false;
} else {
return true;
}
}).first()
}
isLoggedIn () {
return this.isloggedIn;
}
我想做的是观察认证状态。如果发生变化,那么 this.isloggedIn 也会发生变化。
那么如何在我的组件中获取 isloggedin auth.service 值。
import { AuthService } from './auth.service';
export class SomeComponent implements OnInit {
constructor(private titleService: Title, private AuthService : AuthService, public toastr: ToastsManager, private af: AngularFire) {
}
ngOnInit() {
this.titleService.setTitle("some component title");
console.log(this.AuthService.isLoggedIn());
}
目前,当我使用 this.AuthService.isLoggedIn() 时,即使用户已登录,也会给我未定义的信息。我做错了什么?
这可能是因为在您的服务器响应可用之前调用了 isLoggedIn()
。您需要确保异步调用正确链接,否则您无法确定之前的调用是否已经完成。
private isloggedIn:Subject = new BehaviorSubject(false);
constructor(private af: AngularFire, private router: Router) {
this.isLoggedIn = this.af.auth.map(user => { // map instead of subscribe
if (user) {
this.isloggedIn = 'true';
} else {
this.isloggedIn = 'false';
}
});
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.isLoggedIn.map((auth) => {
if(auth == null) {
this.router.navigate(['/auth']);
return false;
} else {
return true;
}
}).first()
}
ngOnInit() {
this.titleService.setTitle("some component title");
console.log(this.AuthService.isLoggedIn.subscribe(value => console.log(value));
}
另见