如何在Flutter中使用Firebase Admin SDK?

编程入门 行业动态 更新时间:2024-10-22 10:45:39
本文介绍了如何在Flutter中使用Firebase Admin SDK?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在创建一个应该可以管理用户访问权限的应用程序.管理员应具有创建,删除和编辑用户帐户的权限.

I'm creating a app where I should be able of managing users access. The admin should have permissions of creating, deleting and editing users accounts.

我正在使用Firebase创建用户帐户.

I'm using firebase for creating users account.

现在,用户可以单独创建,编辑和删除他们的帐户,但是问题是管理员应该这样做,而不仅仅是用户.

Right now individually users can creating, editing and delete their accounts, but the problem is that the admin should do that, and not just the users.

import 'dart:async'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/services.dart'; import 'package:google_sign_in/google_sign_in.dart'; class UserLoader { final FirebaseAuth _auth = FirebaseAuth.instance; final GoogleSignIn googleSIgnIn = new GoogleSignIn(); static final UserLoader _singleton = new UserLoader._internal(); FirebaseUser user; factory UserLoader() { return _singleton; } UserLoader._internal(); Future<FirebaseUser> signInWithEmailAndPassword(email, password) async { if (user != null) return user; _signInAnonymously().then((value) { if (value != null) { user = value; } }).catchError((e) { return null; }); if (user == null) { FirebaseUser user = await _auth.signInWithEmailAndPassword( email: email, password: password).catchError( (onError) { print(onError); }); return user; } else { return null; } } Future<FirebaseUser> signInWithGoogle() async { if (user != null) return user; _signInAnonymously().then((value) { if (value != null) { user = value; } }).catchError((e) { print(e.toString()); }); if (user == null) { GoogleSignInAccount googleSignInAccount = await googleSIgnIn.signIn(); GoogleSignInAuthentication gSA = await googleSignInAccount.authentication; FirebaseUser user = await _auth.signInWithGoogle( idToken: gSA.idToken, accessToken: gSA.accessToken); return user; } else { return null; } } Future<FirebaseUser> _signInAnonymously() async { if (user != null) return user; user = await _auth.signInAnonymously(); return user; } Future signOut() async { await _auth.signOut(); await googleSIgnIn.signOut(); user = null; } Future changePassword(email) async{ await _auth.sendPasswordResetEmail(email: email); } Future createNewUser(email){ _auth.createUserWithEmailAndPassword(email: email, password: "new_pass"); } Future deleteUser(FirebaseUser firebaseUser){ firebaseUser.delete(); } }

我认为Firebase Admin应该可以解决问题,但我不确定.

I think that Firebase Admin should do the trick but I'm not sure.

推荐答案

Firebase Admin SDK仅可用于受信任的环境,例如您的开发计算机,您控制的服务器或Firebase的Cloud Functions. (有意)不适用于客户端应用程序(例如,在Android或iOS上部署的客户端应用程序),无论是使用本机代码构建的应用程序,还是通过Flutter构建的应用程序,均不可用.

The Firebase Admin SDK is only available for use in trusted environments, such as your development machine, a server you control, or Cloud Functions for Firebase. It is (intentionally) not available for use in client-side apps, such as those deployed on Android or iOS, neither when you build those with native code, nor when you build them through Flutter.

唯一的选择是在受信任的环境中使用Admin SDK来实现所需的功能,并将端点公开给Flutter应用.如果最终这样做,请确保保护对端点的访问,以便只有应用程序的管理员用户才能访问它.有关如何使用Cloud Functions保护访问的示例,请参见此在函数样本回购中采样.

The only option is to implement the functionality you want with the Admin SDK in a trusted environment, and expose an end-point to your Flutter app. If you end up doing this, make sure to secure access to the end-point so that only the admin users of your app can access it. For an example of how to secure access with Cloud Functions, see this sample in the functions-samples repo.

更多推荐

如何在Flutter中使用Firebase Admin SDK?

本文发布于:2023-11-28 07:23:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641457.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Flutter   Firebase   Admin   SDK

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!