diff --git a/.gitignore b/.gitignore index 68b162ad..8de59eaf 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ npm-debug.log *.perspectivev3 *.xcuserstate project.xcworkspace/ +atlassian-ide-plugin xcuserdata/ # Example diff --git a/android/src/main/java/io/invertase/firebase/auth/RNFirebaseAuth.java b/android/src/main/java/io/invertase/firebase/auth/RNFirebaseAuth.java index 6524f349..0679b322 100644 --- a/android/src/main/java/io/invertase/firebase/auth/RNFirebaseAuth.java +++ b/android/src/main/java/io/invertase/firebase/auth/RNFirebaseAuth.java @@ -6,6 +6,7 @@ import android.util.Log; import android.net.Uri; import android.support.annotation.NonNull; +import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.HashMap; import java.util.List; @@ -1123,6 +1124,7 @@ class RNFirebaseAuth extends ReactContextBaseJavaModule { /** * fetchProvidersForEmail * + * @param appName * @param promise */ @ReactMethod @@ -1157,6 +1159,31 @@ class RNFirebaseAuth extends ReactContextBaseJavaModule { }); } + /** + * Set the language code for the auth module + * @param appName + * @param code + */ + @ReactMethod + public void setLanguageCode(String appName, String code) { + FirebaseApp firebaseApp = FirebaseApp.getInstance(appName); + FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp); + + firebaseAuth.setLanguageCode(code); + } + + /** + * Use the device language + * @param appName + */ + @ReactMethod + public void useDeviceLanguage(String appName) { + FirebaseApp firebaseApp = FirebaseApp.getInstance(appName); + FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp); + + firebaseAuth.useAppLanguage(); + } + /* ------------------ * INTERNAL HELPERS * ---------------- */ @@ -1431,4 +1458,29 @@ class RNFirebaseAuth extends ReactContextBaseJavaModule { eventMap.putMap("state", state); Utils.sendEvent(mReactContext, "phone_auth_state_changed", eventMap); } + + /** + * Constants bootstrapped on react native app boot + * + * @return + */ + @Override + public Map getConstants() { + Map constants = new HashMap<>(); + + List firebaseAppList = FirebaseApp.getApps(getReactApplicationContext()); + final Map appLanguage = new HashMap<>(); + + for (FirebaseApp app : firebaseAppList) { + String appName = app.getName(); + + FirebaseApp instance = FirebaseApp.getInstance(appName); + FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(instance); + + appLanguage.put(appName, firebaseAuth.getLanguageCode()); + } + + constants.put("APP_LANGUAGE", appLanguage); + return constants; + } } diff --git a/ios/RNFirebase/auth/RNFirebaseAuth.m b/ios/RNFirebase/auth/RNFirebaseAuth.m index 8d87f9d5..550417fe 100644 --- a/ios/RNFirebase/auth/RNFirebaseAuth.m +++ b/ios/RNFirebase/auth/RNFirebaseAuth.m @@ -902,6 +902,34 @@ RCT_EXPORT_METHOD(fetchProvidersForEmail: return credential; } +/** + setLanguageCode + + @param NSString code + @return + */ +RCT_EXPORT_METHOD(setLanguageCode: + (NSString *) appDisplayName + code: + (NSString *) code) { + FIRApp *firApp = [RNFirebaseUtil getApp:appDisplayName]; + + [FIRAuth authWithApp:firApp].languageCode = code; +} + +/** + useDeviceLanguage + + @param NSString code + @return + */ +RCT_EXPORT_METHOD(useDeviceLanguage: + (NSString *) appDisplayName) { + FIRApp *firApp = [RNFirebaseUtil getApp:appDisplayName]; + + [[FIRAuth authWithApp:firApp] useAppLanguage]; +} + // This is here to protect against bugs in the iOS SDK which don't // correctly refresh the user object when performing certain operations - (void)reloadAndReturnUser:(FIRUser *)user @@ -1128,6 +1156,25 @@ RCT_EXPORT_METHOD(fetchProvidersForEmail: return output; } +/** + * React native constant exports - exports native firebase apps mainly + * @return NSDictionary + */ +- (NSDictionary *)constantsToExport { + NSMutableDictionary *constants = [NSMutableDictionary new]; + NSDictionary *firApps = [FIRApp allApps]; + NSMutableDictionary *appLanguage = [NSMutableDictionary new]; + + for (id key in firApps) { + FIRApp *firApp = firApps[key]; + + appLanguage[firApp.name] = [FIRAuth authWithApp:firApp].languageCode; + } + + constants[@"APP_LANGUAGE"] = appLanguage; + return constants; +} + /** Converts a FIRUser instance into a dictionary to send via RNBridge diff --git a/lib/index.d.ts b/lib/index.d.ts index 204900da..628a806f 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -649,6 +649,11 @@ declare module "react-native-firebase" { */ currentUser: User | null + /** + * Gets/Sets the language for the app instance + */ + languageCode: string | null; + /** * Listen for changes in the users auth state (logging in and out). * This method returns a unsubscribe function to stop listening to events. diff --git a/lib/modules/auth/index.js b/lib/modules/auth/index.js index 75a64f19..ebdc05cc 100644 --- a/lib/modules/auth/index.js +++ b/lib/modules/auth/index.js @@ -49,6 +49,7 @@ export default class Auth extends ModuleBase { }); this._user = null; this._authResult = null; + this._languageCode = getNativeModule(this).APP_LANGUAGE[app._name] || getNativeModule(this).APP_LANGUAGE['[DEFAULT]']; SharedEventEmitter.addListener( // sub to internal native event - this fans out to @@ -327,6 +328,16 @@ export default class Auth extends ModuleBase { return getNativeModule(this).fetchProvidersForEmail(email); } + /** + * Sets the language for the auth module + * @param code + * @returns {*} + */ + set languageCode(code: string) { + this._languageCode = code; + getNativeModule(this).setLanguageCode(code); + } + /** * Get the currently signed in user * @return {Promise} @@ -335,6 +346,10 @@ export default class Auth extends ModuleBase { return this._user; } + get languageCode(): string { + return this._languageCode; + } + /** * KNOWN UNSUPPORTED METHODS */ @@ -358,6 +373,11 @@ export default class Auth extends ModuleBase { signInWithRedirect() { throw new Error(INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD('auth', 'signInWithRedirect')); } + + // firebase issue - https://github.com/invertase/react-native-firebase/pull/655#issuecomment-349904680 + useDeviceLanguage() { + throw new Error(INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD('auth', 'useDeviceLanguage')); + } } export const statics = { diff --git a/tests/src/tests/auth/authTests.js b/tests/src/tests/auth/authTests.js index ad838968..5848db26 100644 --- a/tests/src/tests/auth/authTests.js +++ b/tests/src/tests/auth/authTests.js @@ -356,6 +356,18 @@ function authTests({ tryCatch, describe, it, firebase }) { return firebase.native.auth().signOut().then(successCb).catch(failureCb); }); }); + + it('it should change the language code', () => { + firebase.native.auth().languageCode = 'en'; + if (firebase.native.auth().languageCode !== 'en') { + throw new Error('Expected language code to be "en".'); + } + firebase.native.auth().languageCode = 'fr'; + if (firebase.native.auth().languageCode !== 'fr') { + throw new Error('Expected language code to be "fr".'); + } + firebase.native.auth().languageCode = 'en'; + }); }); }