标签: angularfire2

尝试使用 request.path 设置 Cloud Firestore 安全规则

我正在努力理解一些 Firestore 安全概念。我想根据request.path属性设置规则。

我的规则是这样的:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.path[0]=='appUsers';
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

...然后我使用AngularFire2添加这样的文档...

this.myAngularFirestore.doc('/appUsers/'+this.auth.auth.currentUser.uid).set({name: this.auth.auth.currentUser.displayName})
      .then(()=>console.log('Added user'))
      .catch(err=>console.log('Could not add user:', err.message));
Run Code Online (Sandbox Code Playgroud)

我认为根据文档这应该很简单但我得到的只是错误 - Missing or insufficient permissions.

我知道我已正确登录,并且如果我打开安全性,我知道查询有效,那么我allow read,write: if true;在这里没有得到什么?不应该request.path[0]appUsers这里的字符串求值,从而允许写入数据?

任何想法都被感激地接受,因为到目前为止我觉得这些规则在一起玩起来并不有趣。

干杯

firebase firebase-security angularfire2 angular google-cloud-firestore

2
推荐指数
1
解决办法
1676
查看次数

在 Angular/Typescript 中用整数和字母对数组进行排序

我打算做什么

我尝试对这样的数组进行排序...

  • 1
  • 2
  • 2(一)
  • 2(b)
  • 2(b) #AsimpleName
  • 2(b) #NameWithN
  • 3
  • 4
  • 4(一)
  • ...

...在 Angular2 中。

我当前的代码

成分

this.streetDetailRef = this.afDatabase.list('data/users/' + this.currentUserID + '/territory/' + this.sStreet.parentKey + '/' + this.sStreet.key + '/houseNumbers/');
this.streetDetailData = this.streetDetailRef.snapshotChanges().map(changes => {
  return changes.map(c => ({ key: c.payload.key, ...c.payload.val() })).sort();
});
Run Code Online (Sandbox Code Playgroud)

我认为的循环

<ion-item-sliding *ngFor="let house of streetDetailData | async | orderBy: 'number':false" #slidingItem>
<strong>{{ house.number }}</strong> ...
Run Code Online (Sandbox Code Playgroud)

'number'在这种情况下是没有字母的干净数字。我将这封信存储在一个单独的 firebase 条目中。但如果需要,肯定可以将它们存储在同一个中。

附加信息:我正在使用 VadimDez 的 ngx-orderBy-pipe:https : //github.com/VadimDez/ngx-order-pipe

目前的结果

在此处输入图片说明

firebase typescript ionic2 angularfire2 angular

2
推荐指数
1
解决办法
1万
查看次数

使用 AngularFire2 toPromise() 方法异步/等待不起作用?

我刚刚在使用 AngularFire2 Observables 时遇到了一个奇怪的问题,请查看此代码并告诉我您是否对正在发生的事情有所了解:

async save(){
 const client = await this.getClient(this.id);
 console.log(client); // ZoneAwarePromise blah blah
}

getClient(clientId) {
 return this.db.object('clients/' + clientId)
            .valueChanges()
            .toPromise()
            .then((client: any) => {key: clientId, name: client.name});
}
Run Code Online (Sandbox Code Playgroud)

所以这段代码不起作用,但如果我像下一个代码一样这样做,它将起作用:

async save(){
 const client = await this.getClient(this.id);
 console.log(client); // {key: 'blah', name: 'fooblah'}
}

getClient(clientId) {
 return new Promise((resolve, reject) => {
    this.db.object('clients/' + clientId)
            .valueChanges()
            .subscribe((client: any) => resolve({key: clientId, name: client.name}));
}
Run Code Online (Sandbox Code Playgroud)

那么如何在 .toPromise() 方法不起作用的情况下创建承诺并解析 Observable 数据有效呢?

这是正常行为吗?难道我做错了什么 ?让我知道 :-)

async-await angularfire2 angular2-observables angular

2
推荐指数
1
解决办法
1364
查看次数

如何在 angularfire2 中使用 Observable.Zip?

我正在使用 Angular 5 和 angularfire2 来处理 Firebase。我有一个字符串数组,它们表示进入 firebase 的路径。我目前正在做的是:

const pathList: string[] = ['path1', 'path2', 'path3'];
const observableList     = [];
pathList.map((location: string[]) => {
    observableList.push(this.db.object(`path_to_something/${location}).valueChanges());
});

const combined = zip(observableList);
combined.subscribe((values: any[]) => {
    console.log('DATA FROM ZIP', values);
}, err => {
    console.log('DATA FROM ZIP ERROR', err);
});
Run Code Online (Sandbox Code Playgroud)

这就是我一直在尝试的,但是 Observables 似乎没有触发。显示控制台上的任何结果。关于这如何工作的任何想法。

我试过 forkJoin 也没有。但是我尝试使用 combineList 并且它有效。问题是,我必须使用zip

rxjs rxjs5 angularfire2 angular

2
推荐指数
1
解决办法
410
查看次数

获取 BehaviorSubject 的值

我正在尝试获取 BehaviorSubject 的值。返回值,但我如何利用它们在 return true/false 语句中使用它们。

BehaviorSubject {_isScalar: false, observers: Array(0), closed: false, isStopped: false, hasError: false, …}
closed:false
hasError:false
isStopped:false
observers:[Subscriber]
thrownError:null
value:(...)
_isScalar:false
_value:Array(3)
0:"name@gmail.com"
1:"Bob Smith"
2:{adminUser: false, basicUser: true} length:3
__proto__:Array(0)
__proto__:Subject
Run Code Online (Sandbox Code Playgroud)

我的行为主题已发送给它以下内容。为什么我一开始就为空。

user: BehaviorSubject<User> = new BehaviorSubject<User>(null); 
Run Code Online (Sandbox Code Playgroud)

// 用户的行为角色。

 this.afAuth.authState
                .switchMap(auth => {
                if (auth) {
                    /// signed in
                    const authData = this.db.list<User>('users/' + auth.uid).valueChanges();
                    console.log(authData);
                    return this.db.list<User>('users/' + auth.uid).valueChanges();
                } else {
                    /// not signed in
                    return Observable.of(null);
                }
                })
                // .subscribe(console.log); // check …
Run Code Online (Sandbox Code Playgroud)

javascript rxjs firebase angularfire2 angular

2
推荐指数
1
解决办法
1万
查看次数

AngularFire - Firestore - 在执行查询时检查文档是否存在

我看过有关如何检查特定文档是否存在的示例。但是,是否可以在执行如下查询时检查文档是否存在?

private albumsCollection: AngularFirestoreCollection<any>;
albums: Observable<any[]>;

...


this.albumCollection = this.afs.collection<any>(`albums`, ref => {
  return ref.where('albumid', "==", this.albumid);
});
Run Code Online (Sandbox Code Playgroud)

firebase angularfire2 angular google-cloud-firestore

2
推荐指数
1
解决办法
4116
查看次数

无法使用 angularfire2 在 Angular 6 应用程序中访问多个 Google Firestore 数据库

我尝试通过 angularfire2 包从我的 Angular 6 应用程序访问多个 Google Firestore 数据库。

我在 app.module.ts 中初始化了多个 AngularFireModule 实例,但找不到访问两个数据库的方法:

  @NgModule({
  declarations: [
	...
  ],
  imports: [
	...
    AngularFireModule.initializeApp(coolStoreConfig, 'coolStore'),
    AngularFireModule.initializeApp(perfectStoreConfig, 'perfectStore'),
	...
  ],
  ...
 })
Run Code Online (Sandbox Code Playgroud)

任何的想法?

angularfire2 angular google-cloud-firestore angularfire5

2
推荐指数
1
解决办法
898
查看次数

Angular Firestore 通过查询获取单个文档

我正在使用 Google Firestore。有一个名为 Universities 的集合,其中每个文档代表一所大学。

如何按名称查找大学并将其 view_count 更新为 +1?

[Collection: Universities]:
  [Document: 0JsaLtcfdDaEtHLMlMrh]:{
                      name: The University of Sydney,
                      view_count: 1
            };
   [Document: K2z7DMhKreyDniGUpo2t]:{
                      name: University of New South Wales,
                      view_count: 2
            }
Run Code Online (Sandbox Code Playgroud)

angularfire angularfire2 angular google-cloud-firestore

2
推荐指数
1
解决办法
6672
查看次数

Firebase 自定义声明不会传播

我正在使用 angularfire2 使用 Angular6 应用程序。我在用户创建中将角色设置为自定义声明,但它似乎没有传播。

创建用户时,我将用户 ID、业务 ID 和角色发送到云功能:

出价 > 商业 ID

角色 > 角色

req.body.uid > 用户 ID

  const customClaims = {
    roles: { [bid]: urole }
  }
  admin.auth().setCustomUserClaims(req.body.uid, customClaims)
    .then(result => {
      res
        .status(200)
        .send()
    })
Run Code Online (Sandbox Code Playgroud)

问题是当对云函数的调用完成时,我想将用户重定向到需要用户设置自定义声明的路由,但它失败了。经过一些调试,我发现如果运行:

this.angularFireAuth.auth.currentUser.getIdTokenResult(true).then(result => {
      return result.claims.roles
    })
Run Code Online (Sandbox Code Playgroud)

在调用云函数“result.claims.roles”之后立即未定义,但是如果我刷新页面,“result.claims.roles”就会有我之前设置的数据。

我已经尝试过 reload 方法和 getIdToken(true) 但我遇到了同样的问题。

有没有办法避免刷新页面并获得自定义声明?

谢谢!

javascript firebase firebase-authentication angularfire2

2
推荐指数
1
解决办法
2161
查看次数

您如何确定用户是否已经在 Angular Firebase 中进行了身份验证?

如果用户未使用 AngularFireAuth 进行身份验证,我正在编写一个警卫来将用户重定向到登录页面。如果没有这个库,我会在本地/会话存储中存储一个令牌,当守卫触发时,我会检查存储以查看是否存在令牌。AngularFireAuth 在数据库中存储一个令牌(其中包含身份验证信息):

存储在 Firefox 开发工具索引数据库中的 AngularFireAuth 图像

然而,直接检查它是不明智的,因为 AngularFireAuth 可能会在未来的任何日期改变它的工作方式。它不完全是记录在案的公共 API 的一部分。

在旧版本的 AngularFireAuth 中,我可以在我的服务中包含这个方法来做这个检查:

import { AngularFireAuth } from '@angular/fire/auth';

export class AuthService {
  constructor(private readonly service: AngularFireAuth) {}

  public login(credentials: Credentials) {
    const { email, password } = credentials;
    return this.service.auth.signInWithEmailAndPassword(email, password);
  }

  public isLoggedIn(): boolean {
    return !!this.service.auth.currentUser;
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,这不会在刷新时持续存在。因此,当我在系统中导航时,它工作正常,但如果我刷新或离开并返回,系统会提示我再次登录。

我如何编写我的isLoggedIn方法可以在我的守卫中调用?

firebase firebase-authentication angularfire2 angular

2
推荐指数
1
解决办法
766
查看次数