Merge pull request #655 from invertase/auth-language

Set auth language #654
This commit is contained in:
Michael Diarmid 2018-01-17 15:23:41 +00:00 committed by GitHub
commit c14d9292b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 137 additions and 0 deletions

1
.gitignore vendored
View File

@ -9,6 +9,7 @@ npm-debug.log
*.perspectivev3
*.xcuserstate
project.xcworkspace/
atlassian-ide-plugin
xcuserdata/
# Example

View File

@ -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<String, Object> getConstants() {
Map<String, Object> constants = new HashMap<>();
List<FirebaseApp> firebaseAppList = FirebaseApp.getApps(getReactApplicationContext());
final Map<String, Object> 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;
}
}

View File

@ -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

5
lib/index.d.ts vendored
View File

@ -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.

View File

@ -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 = {

View File

@ -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';
});
});
}