gpt4 book ai didi

Angular/Firebase 身份验证 - 使 bool 值从服务到组件可观察?

转载 作者:行者123 更新时间:2023-12-04 17:45:58 25 4
gpt4 key购买 nike

我正在尝试根据存在于组件 Controller ,它与我的 auth.service 服务中定义的同名属性相匹配。

因此,当有经过身份验证的用户,并且“isAuthenticated”的 bool 值为真时,元素应该显示。

目前, bool 值默认设置为 false,并在成功登录后定义的同一 auth.service 文件中更新,但我需要在 中更新该值app.component.ts 文件也是如此,但由于“Observable”无法分配给 bool 值,因此无法执行此操作。

auth.service.ts

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import * as firebase from 'firebase/app';
import { AngularFireAuth } from 'angularfire2/auth';

@Injectable()

export class AuthService {

private user: Observable<firebase.User>;

isAuthenticated: boolean = false;

constructor(private firebaseAuth: AngularFireAuth, private router: Router) {
this.user = firebaseAuth.authState;
}

signIn(email: string, password: string) {
this.firebaseAuth
.auth
.signInWithEmailAndPassword(email, password)
.then(value => {
console.log('Signed In');
this.isAuthenticated = true;
this.router.navigateByUrl('/dashboard');
})
.catch(err => {
console.log('Sign-In Error: ', err.message);
});
}

signOut() {
this.firebaseAuth
.auth
.signOut();
this.isAuthenticated = false;
this.router.navigateByUrl('/login');
console.log('Signed Out');
}

register(email: string, password: string) {
this.firebaseAuth
.auth
.createUserWithEmailAndPassword(email, password)
.then(value => {
console.log('Registration Successful', value);
})
.catch(err => {
console.log('Registration Error: ', err.message);
});
}

}

can-activate-route.guard.ts

import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs/Observable';

import { AuthService } from './services/auth.service';

@Injectable()

export class CanActivateRouteGuard implements CanActivate {

constructor(private auth: AuthService) { }

canActivate (
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.auth.isAuthenticated;
}

}

app.component.ts

import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';

import { AngularFirestore } from 'angularfire2/firestore';

import { UsersService } from './services/users.service';
import { AuthService } from './services/auth.service';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})

export class AppComponent {

isAuthenticated: boolean;
titles: any[];

constructor(public db: AngularFirestore, private usersService: UsersService, public authService: AuthService, private router: Router) {
this.isAuthenticated = this.authService.isAuthenticated;
}

app.component.html

<button mat-button (click)="signOut()" [hidden]="isAuthenticated">Sign Out</button>

最佳答案

尝试改变你的组件(app.component.ts):

isAuthenticated: boolean;

对此:

export class AppComponent {

get isAuthenticated(): boolean {
return this.authService.isAuthenticated;
}

titles: any[];

constructor(public db: AngularFirestore,
private usersService: UsersService,
public authService: AuthService,
private router: Router) { }
}

这将确保随着服务值的变化,组件属性将始终获得最新的值。

关于Angular/Firebase 身份验证 - 使 bool 值从服务到组件可观察?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48591540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com