我有功能:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const Firestore = require('@google-cloud/firestore');
const db = admin.firestore();
exports.createCollectionByPlayerID =
functions.https.onCall((data,context) => {
const citiesRef = db.collection(context.auth.uid);
let setSf = citiesRef.doc('1SF').set({
name: 'San Francisco',
state: 'CA',
country: 'USA',
capital: false,
population: 260
});
return "testID"
});
我希望此功能为每个玩家创建单独的集合(或文档),并以玩家的ID命名。 当我从一台设备调用此函数时,该函数将使用一个名称创建新集合:
当我从另一台设备调用同一函数时,该函数会创建另一个具有其他名称的集合。
我需要从一个不同的设备连接到一个玩家创建的一个收藏集,而无需创建新的收藏集。
我的Java代码:
private void hw_fromFunction() {
FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();
mUser.getIdToken(true)
.addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
public void onComplete(@NonNull Task<GetTokenResult> task) {
if (task.isSuccessful()) {
String idToken = task.getResult().getToken();
helloWorldFromFunc(idToken).addOnCompleteListener(new
OnCompleteListener<String>() {
@Override public void onComplete(@NonNull Task<String> task) {}
});
} else {}
}
});
}
private Task<String> helloWorldFromFunc(String idToken) {
Map<String, Object> data = new HashMap<>();
data.put("push", true);
return mFunctions
.getHttpsCallable("createCollectionByPlayerID")
.call(idToken)
.continueWith(new Continuation<HttpsCallableResult, String>()
{
@Override
public String then(@NonNull Task<HttpsCallableResult>
task) throws Exception {
return (String) task.getResult().getData();
}
});
}
玩家如何连接来自不同部门的以其ID命名的一个收藏?
===添加了一些Java代码:
//>>>>>>>>>>>>>>>>*****auth***********
GoogleSignInOptions gso = new
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
//.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
mAuth = FirebaseAuth.getInstance();
private void signIn() {Intent signInIntent
= Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
private void signOut() {
mGoogleSignInClient.signOut().addOnCompleteListener(this
, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
updateUI(false);
}
});
}
private void revokeAccess() {
mAuth.signOut();
mGoogleSignInClient.revokeAccess().addOnCompleteListener(this
, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
updateUI(false);
}
});
}
Firestore中的规则:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow create: if request.auth != null && request.auth.uid == uid;
}
}
}
然后登录:(很抱歉输入了这么多文字)
2019-07-05 10:04:21.134 8802-8844/com.admiral_odessa.firebaseadmiral1 V/FA: Connecting to remote service
2019-07-05 10:04:21.169 8802-8844/com.admiral_odessa.firebaseadmiral1 V/FA: Connection attempt already in progress
2019-07-05 10:04:21.281 8802-8802/com.admiral_odessa.firebaseadmiral1 W/zygote: Skipping duplicate class check due to unrecognized classloader
2019-07-05 10:04:21.283 8802-8802/com.admiral_odessa.firebaseadmiral1 I/DynamiteModule: Considering local module com.google.android.gms.signinbutton_dynamite:0 and remote module com.google.android.gms.signinbutton_dynamite:2
2019-07-05 10:04:21.283 8802-8802/com.admiral_odessa.firebaseadmiral1 I/DynamiteModule: Selected remote version of com.google.android.gms.signinbutton_dynamite, version >= 2
2019-07-05 10:04:21.336 8802-8802/com.admiral_odessa.firebaseadmiral1 W/zygote: Skipping duplicate class check due to unrecognized classloader
2019-07-05 10:04:21.338 8802-8846/com.admiral_odessa.firebaseadmiral1 D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2019-07-05 10:04:21.343 8802-8846/com.admiral_odessa.firebaseadmiral1 I/ProviderInstaller: Installed default security provider GmsCore_OpenSSL
2019-07-05 10:04:21.420 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: Do partial code cache collection, code=30KB, data=28KB
2019-07-05 10:04:21.478 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: After code cache collection, code=28KB, data=28KB
2019-07-05 10:04:21.478 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: Increasing code cache capacity to 128KB
2019-07-05 10:04:21.482 8802-8813/com.admiral_odessa.firebaseadmiral1 I/zygote: Background concurrent copying GC freed 11835(1178KB) AllocSpace objects, 1(20KB) LOS objects, 49% free, 1681KB/3MB, paused 781us total 110.398ms
2019-07-05 10:04:21.532 8802-8802/com.admiral_odessa.firebaseadmiral1 D/AutoManageHelper: starting AutoManage for client 0 false null
2019-07-05 10:04:21.545 8802-8802/com.admiral_odessa.firebaseadmiral1 D/AutoManageHelper: onStart true {0=com.google.android.gms.common.api.internal.zaj$zaa@d0b2a33}
2019-07-05 10:04:21.632 8802-8844/com.admiral_odessa.firebaseadmiral1 I/FA: Tag Manager is not found and thus will not be used
2019-07-05 10:04:21.643 8802-8849/com.admiral_odessa.firebaseadmiral1 D/OpenGLRenderer: HWUI GL Pipeline
2019-07-05 10:04:21.692 8802-8851/com.admiral_odessa.firebaseadmiral1 W/DynamiteModule: Local module descriptor class for providerinstaller not found.
2019-07-05 10:04:21.708 8802-8844/com.admiral_odessa.firebaseadmiral1 D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=3100986043983932118}]
2019-07-05 10:04:21.716 8802-8851/com.admiral_odessa.firebaseadmiral1 I/DynamiteModule: Considering local module providerinstaller:0 and remote module providerinstaller:0
2019-07-05 10:04:21.716 8802-8851/com.admiral_odessa.firebaseadmiral1 W/ProviderInstaller: Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
2019-07-05 10:04:21.755 8802-8849/com.admiral_odessa.firebaseadmiral1 I/OpenGLRenderer: Initialized EGL, version 1.4
2019-07-05 10:04:21.755 8802-8849/com.admiral_odessa.firebaseadmiral1 D/OpenGLRenderer: Swap behavior 1
2019-07-05 10:04:21.756 8802-8849/com.admiral_odessa.firebaseadmiral1 W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2019-07-05 10:04:21.756 8802-8849/com.admiral_odessa.firebaseadmiral1 D/OpenGLRenderer: Swap behavior 0
2019-07-05 10:04:21.777 8802-8849/com.admiral_odessa.firebaseadmiral1 D/EGL_emulation: eglCreateContext: 0xa0e9a580: maj 2 min 0 rcv 2
2019-07-05 10:04:21.832 8802-8849/com.admiral_odessa.firebaseadmiral1 D/EGL_emulation: eglMakeCurrent: 0xa0e9a580: ver 2 0 (tinfo 0xaccfb330)
2019-07-05 10:04:21.861 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: Do partial code cache collection, code=33KB, data=53KB
2019-07-05 10:04:21.886 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: After code cache collection, code=33KB, data=53KB
2019-07-05 10:04:21.886 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: Increasing code cache capacity to 256KB
2019-07-05 10:04:21.891 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: JIT allocated 71KB for compiled code of void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
2019-07-05 10:04:21.891 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: Compiler allocated 4MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
2019-07-05 10:04:21.915 8802-8844/com.admiral_odessa.firebaseadmiral1 V/FA: Connection attempt already in progress
2019-07-05 10:04:21.916 8802-8844/com.admiral_odessa.firebaseadmiral1 V/FA: Connection attempt already in progress
2019-07-05 10:04:21.928 8802-8844/com.admiral_odessa.firebaseadmiral1 V/FA: Activity resumed, time: 32836187
2019-07-05 10:04:22.022 8802-8849/com.admiral_odessa.firebaseadmiral1 D/EGL_emulation: eglMakeCurrent: 0xa0e9a580: ver 2 0 (tinfo 0xaccfb330)
2019-07-05 10:04:22.052 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: Do full code cache collection, code=125KB, data=66KB
2019-07-05 10:04:22.052 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: After code cache collection, code=118KB, data=47KB
2019-07-05 10:04:22.061 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: Do partial code cache collection, code=118KB, data=48KB
2019-07-05 10:04:22.061 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: After code cache collection, code=118KB, data=48KB
2019-07-05 10:04:22.061 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: Increasing code cache capacity to 512KB
2019-07-05 10:04:22.132 8802-8849/com.admiral_odessa.firebaseadmiral1 D/EGL_emulation: eglMakeCurrent: 0xa0e9a580: ver 2 0 (tinfo 0xaccfb330)
2019-07-05 10:04:22.195 8802-8844/com.admiral_odessa.firebaseadmiral1 D/FA: Connected to remote service
2019-07-05 10:04:22.206 8802-8844/com.admiral_odessa.firebaseadmiral1 V/FA: Processing queued up service tasks: 4
2019-07-05 10:04:22.208 8802-8849/com.admiral_odessa.firebaseadmiral1 D/EGL_emulation: eglMakeCurrent: 0xa0e9a580: ver 2 0 (tinfo 0xaccfb330)
2019-07-05 10:04:22.259 8802-8802/com.admiral_odessa.firebaseadmiral1 W/View: dispatchProvideAutofillStructure(): not laid out, ignoring
2019-07-05 10:04:22.259 8802-8802/com.admiral_odessa.firebaseadmiral1 W/View: dispatchProvideAutofillStructure(): not laid out, ignoring
2019-07-05 10:04:22.265 8802-8802/com.admiral_odessa.firebaseadmiral1 W/View: dispatchProvideAutofillStructure(): not laid out, ignoring
2019-07-05 10:04:22.276 8802-8802/com.admiral_odessa.firebaseadmiral1 I/AssistStructure: Flattened final assist data: 6212 bytes, containing 1 windows, 25 views
2019-07-05 10:04:23.633 8802-8849/com.admiral_odessa.firebaseadmiral1 D/EGL_emulation: eglMakeCurrent: 0xa0e9a580: ver 2 0 (tinfo 0xaccfb330)
2019-07-05 10:04:23.698 8802-8849/com.admiral_odessa.firebaseadmiral1 D/EGL_emulation: eglMakeCurrent: 0xa0e9a580: ver 2 0 (tinfo 0xaccfb330)
2019-07-05 10:04:25.666 8802-8849/com.admiral_odessa.firebaseadmiral1 D/EGL_emulation: eglMakeCurrent: 0xa0e9a580: ver 2 0 (tinfo 0xaccfb330)
2019-07-05 10:04:27.514 8802-8844/com.admiral_odessa.firebaseadmiral1 V/FA: Inactivity, disconnecting from the service
2019-07-05 10:05:29.632 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: Do full code cache collection, code=249KB, data=151KB
2019-07-05 10:05:29.633 8802-8807/com.admiral_odessa.firebaseadmiral1 I/zygote: After code cache collection, code=158KB, data=105KB
答案 0 :(得分:0)
注册用户后,您应该做的第一件事就是保存UID,集合ID和文档ID。任何后续权利或读取都应使用集合ID和文档ID作为路径,即collection \ Document ID。
应通过用户默认设置和/或共享存储使连接/文档ID对所有设备可用。由于每个设备上都存在用户默认值,因此您需要将用户默认值保存在某种共享存储中。 我使用了以下键\值-UID \ Collection ID和Collection ID / Document ID。
如果允许Firestore创建文档ID,它将是唯一的。因此在注册时,请保存以上键/值。然后,在进行后续登录时,请首先检查设备的用户默认设置中的连接ID /文档ID,然后将其用作路径。如果没有保存,请检查您的共享存储并更新设备的“用户默认设置”。
您还可以使用Firestore的命令获取用户的收藏ID。
Swift / iOS
let Database Firestore.firestore()
Database.collection(collection ID).getDocuments{(snapshot, err)}
使用现有帐户登录时,如果找不到“用户默认值”或共享存储中存储的键/值对,则需要根据您的应用程序采取适当的操作。