Firebase-如何使用Cloud Function禁用用户? [英] Firebase - How to disable user using Cloud Function?

查看:67
本文介绍了Firebase-如何使用Cloud Function禁用用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制定启用/禁用用户云功能.当使用admin SDK禁用用户时,我得到响应: disabled属性是只读的.有人可以帮我吗?传递给函数的数据是用户ID.

I am trying to work out an enable/disable user cloud function. When using the admin SDK to disable the user, I get the response: that the disabled property is read only. Can someone help me out? The data that is passed to the function, is the user ID.

 export const disableUser = functions.https.onCall((data, context) => {
    console.log(data);

    admin.auth().updateUser(data, {
        disabled: true
    }).then(() => {
        admin.firestore().collection('users').doc(data).update({ disabled: true})
            .then(() =>  {
                console.log(`Successfully disabled user: ${data}`);

                return 200;
            })
            .catch((error => console.log(error)));        
    }).catch( error => console.log(error));

    return 500;
});

推荐答案

我将TypeScript用于Cloud Functions.

I used typeScript for Cloud Functions.

index.ts

import * as functions from 'firebase-functions';

export const disableUser = functions.https.onCall((data, context) => {
  const userId = data.userId;
  return functions.app.admin.auth().updateUser(userId, {disabled: true});
});

在我的应用中,我这样调用了"disableUser"函​​数:

In my app, I called "disableUser" function like this:

import {AngularFireFunctions} from '@angular/fire/functions';

export class AppComponent {

data$: Observable<any>;
callable: (data: any) => Observable<any>;

constructor(private fns: AngularFireFunctions) {
  this.callable = fns.httpsCallable('disableUser');
  this.data$ = this.callable({userId: 'ZDxaTS0SoYNYqUVJuLLiXSLkd8A2'});
}

这篇关于Firebase-如何使用Cloud Function禁用用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆