Javascript Firebase身份验证与角度 我用的是什么 角度5 角火 Firebase&Firestore 我想要达到的目标

Javascript Firebase身份验证与角度 我用的是什么 角度5 角火 Firebase&Firestore 我想要达到的目标,javascript,firebase,firebase-authentication,angularfire2,angular5,Javascript,Firebase,Firebase Authentication,Angularfire2,Angular5,我正在尝试制作一个简单的身份验证/登录和注册系统。事实上,我已经制作了一个,尽管我遇到了一些问题,我想确保我正在使用最好的方法来设置身份验证 到目前为止我所拥有的 auth.service.ts 从'@angular/core'导入{Injectable}; 从'@angular/Router'导入{Router}; 从'angularfire2/auth'导入{AngularFireAuth}; @可注射() 导出类身份验证服务{ authState:any=null; 电子邮件=''; 用户

我正在尝试制作一个简单的身份验证/登录和注册系统。事实上,我已经制作了一个,尽管我遇到了一些问题,我想确保我正在使用最好的方法来设置身份验证

到目前为止我所拥有的 auth.service.ts
从'@angular/core'导入{Injectable};
从'@angular/Router'导入{Router};
从'angularfire2/auth'导入{AngularFireAuth};
@可注射()
导出类身份验证服务{
authState:any=null;
电子邮件='';
用户名=“”;
密码=“”;
errorMessage='';
错误:{name:string,message:string}={name:'',message:'';
构造函数(专用afAuth:AngularFireAuth,专用路由器:路由器){
this.afAuth.authState.subscribe((auth)=>{
this.authState=auth
});
}
get-isUserEmailLoggedIn():布尔值{
if(this.authState!==null){
返回真值
}否则{
返回错误
}
}
get currentUser():任何{
返回(this.authState!==null)?this.authState:null;
}
获取currentUserId():字符串{
返回(this.authState!==null)?this.authState.uid:“”
}
获取currentUserName():字符串{
返回此.authState['email']
}
使用电子邮件注册(电子邮件:字符串,密码:字符串){
返回此.afAuth.auth.createUserWithEmailAndPassword(电子邮件,密码)
。然后((用户)=>{
this.authState=user
})
.catch(错误=>{
console.log(错误)
抛出错误
});
}
使用电子邮件登录(电子邮件:字符串,密码:字符串){
使用email和password(电子邮件,密码)返回此.afAuth.auth.Signin
。然后((用户)=>{
this.authState=user
})
.catch(错误=>{
console.log(错误)
抛出错误
});
}
signOut():void{
this.afAuth.auth.signOut();
this.router.navigate(['/'])
}
onSignUp():void{
这个.clearErrorMessage()
if(this.validateForm(this.email,this.password)){
this.signUpWithEmail(this.email,this.password)
.然后(()=>{
this.router.navigate(['/home'])
}).catch(_error=>{
this.error=\u error
this.router.navigate(['/register'])
})
}
}
onLoginEmail():void{
这个.clearErrorMessage()
if(this.validateForm(this.email,this.password)){
this.loginWithEmail(this.email,this.password)
。然后(()=>this.router.navigate(['/home']))
.catch(_error=>{
this.error=\u error
this.router.navigate(['/login'])
})
}
}
validateForm(电子邮件:字符串,密码:字符串):布尔值{
如果(email.length==0){
this.errorMessage='请输入电子邮件!'
返回错误
}
如果(password.length==0){
this.errorMessage='请输入密码!'
返回错误
}
如果(密码长度<6){
this.errorMessage='密码应至少包含6个字符!'
返回错误
}
this.errorMessage=“”
返回真值
}
clearErrorMessage(){
this.errorMessage='';
this.error={name:'',消息:'};
}
}
link.service.ts
从'@angular/core'导入{Injectable,OnInit};
从“/auth.service”导入{AuthService};
从“angularfire2/firestore”导入{AngularFirestore、AngularFirestoreDocument、AngularFirestoreCollection};
从“rxjs/Observable”导入{Observable};
导入'rxjs/add/operator/map';
导出接口链接{uid:string;url:string;shortURL:string;clicks:number}
@可注射()
导出类LinkService实现OnInit{
url:string;
短URL:字符串;
showarert:布尔值;
链接:可观察;
构造函数(公共authService:authService,私有afs:AngularFirestore){
this.links=afs.collection('links').valueChanges();
}
恩戈尼尼特(){
}
createShortURL(){
var text='';
可能的变量='ABCDEFGHIjklmnopqrstuvxyzabCDEFGHIjklmnopqrstuvxyzo123456789';
变量长度=6;
对于(变量i=0;i
我被困的地方
根据提供的信息。我正在尝试获取link.service.ts中的currentUserID,尽管它返回时未定义。但是,对于addLink()函数,
this.authService.isUserEmailLoggedIn可以很好地工作,我不知道为什么它不能返回正确的值。

您需要像这样获取id

 this.items = this.itemCollection.snapshotChanges().map(changes => {
          return changes.map(a => {
            const data = a.payload.doc.data();
            data.id = a.payload.doc.id;
            return data;
          });
        });

currentUserId()
是一个您应该使用的函数
'uid]:this.authService.currentUserId(),

您能解释一下它到底在做什么吗,我不确定我是否100%理解,所以如果您能够提供它,我希望得到进一步的解释。简而言之,如果您需要访问id,您必须使用
snapshotChanges()
以下是docs所说的:
snapshotChanges()
当您需要文档数据但又想保留元数据时。此元数据为您提供了underyling DocumentReference和document id。使用文档id可以更轻松地使用数据操作方法。我正在尝试检索当前用户ID,它是Auth的一部分。我知道如何从文档中获取数据,但我想弄清楚的是何时运行例如'this.authService.currentUserId();'它被记录为未定义。我不知道为什么“if(this.authService.isUserEmailLoggedIn)”有效,但“this.authServi”有效
import { Injectable, OnInit } from '@angular/core';
import { AuthService } from './auth.service';
import { AngularFirestore, AngularFirestoreDocument, AngularFirestoreCollection } from 'angularfire2/firestore';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

export interface Link { uid: string; url: string; shortURL: string; clicks: number }

@Injectable()
export class LinkService implements OnInit {
  url: string;
  shortURL: string;
  showAlert: boolean;
  links: Observable<any>;

  constructor(public authService: AuthService, private afs: AngularFirestore) {
        this.links = afs.collection('Links').valueChanges();
  }
  ngOnInit() {
  }
  createShortURL() {
    var text = '';
    var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var length = 6;

    for(var i = 0; i < length; i++) {
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    }
    return this.shortURL = text;
  }
  addLink() {
    if (this.authService.isUserEmailLoggedIn) {
      this.createShortURL();
      this.afs.collection('Links').doc(this.shortURL).set({
        'uid': this.authService.currentUserId,
        'url': this.url,
        'shortURL': this.shortURL,
        'clicks': 0
      });
      this.clearFields();
      this.showAlert = false;
    } else {
      this.showAlert = true;
    }
  }
  clearFields() {
    return this.url = '';
  }
}
 this.items = this.itemCollection.snapshotChanges().map(changes => {
          return changes.map(a => {
            const data = a.payload.doc.data();
            data.id = a.payload.doc.id;
            return data;
          });
        });