|
17 | 17 | #include <assert.h>
|
18 | 18 | #include <jni.h>
|
19 | 19 |
|
| 20 | +#include <string> |
| 21 | + |
20 | 22 | #include "app/src/assert.h"
|
21 | 23 | #include "app/src/embedded_file.h"
|
22 | 24 | #include "app/src/include/firebase/internal/mutex.h"
|
@@ -53,6 +55,7 @@ using util::JniStringToString;
|
53 | 55 | X(RemoveIdTokenListener, "removeIdTokenListener", \
|
54 | 56 | "(Lcom/google/firebase/auth/FirebaseAuth$IdTokenListener;)V"), \
|
55 | 57 | X(SignOut, "signOut", "()V"), \
|
| 58 | + X(UseEmulator, "useEmulator", "(Ljava/lang/String;I)V"), \ |
56 | 59 | X(FetchSignInMethodsForEmail, "fetchSignInMethodsForEmail", \
|
57 | 60 | "(Ljava/lang/String;)" \
|
58 | 61 | "Lcom/google/android/gms/tasks/Task;"), \
|
@@ -185,6 +188,31 @@ void UpdateCurrentUser(AuthData* auth_data) {
|
185 | 188 | }
|
186 | 189 | }
|
187 | 190 |
|
| 191 | +const char* const kEmulatorLocalHost = "10.0.2.2"; |
| 192 | +const char* const kEmulatorPort = "9099"; |
| 193 | +void CheckEmulator(AuthData* auth_data) { |
| 194 | + JNIEnv* env = Env(auth_data); |
| 195 | + |
| 196 | + // Use emulator as long as this env variable is set, regardless its value. |
| 197 | + if (std::getenv("USE_AUTH_EMULATOR") == nullptr) { |
| 198 | + LogDebug("Using Auth Prod for testing."); |
| 199 | + return; |
| 200 | + } |
| 201 | + |
| 202 | + // Use AUTH_EMULATOR_PORT if it is set to non empty string, |
| 203 | + // otherwise use the default port. |
| 204 | + uint32_t port = std::stoi(kEmulatorPort); |
| 205 | + if (std::getenv("AUTH_EMULATOR_PORT") != nullptr) { |
| 206 | + port = std::stoi(std::getenv("AUTH_EMULATOR_PORT")); |
| 207 | + } |
| 208 | + |
| 209 | + jstring j_host = env->NewStringUTF(kEmulatorLocalHost); |
| 210 | + env->CallVoidMethod(AuthImpl(auth_data), |
| 211 | + auth::GetMethodId(auth::kUseEmulator), j_host, port); |
| 212 | + env->DeleteLocalRef(j_host); |
| 213 | + firebase::util::CheckAndClearJniExceptions(env); |
| 214 | +} |
| 215 | + |
188 | 216 | // Release cached Java classes.
|
189 | 217 | static void ReleaseClasses(JNIEnv* env) {
|
190 | 218 | ReleaseAuthClasses(env);
|
@@ -269,6 +297,8 @@ void Auth::InitPlatformAuth(AuthData* auth_data) {
|
269 | 297 | // Ensure our User is in-line with underlying API's user.
|
270 | 298 | // It's possible for a user to already be logged-in on start-up.
|
271 | 299 | UpdateCurrentUser(auth_data);
|
| 300 | + |
| 301 | + CheckEmulator(auth_data); |
272 | 302 | }
|
273 | 303 |
|
274 | 304 | void Auth::DestroyPlatformAuth(AuthData* auth_data) {
|
|
0 commit comments