Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Set auth language #654 #655

Merged
merged 17 commits into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ npm-debug.log
*.perspectivev3
*.xcuserstate
project.xcworkspace/
atlassian-ide-plugin
xcuserdata/

# Example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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;
Expand Down Expand Up @@ -1143,6 +1144,7 @@ public void onComplete(@NonNull Task<GetTokenResult> task) {
/**
* fetchProvidersForEmail
*
* @param appName
* @param promise
*/
@ReactMethod
Expand Down Expand Up @@ -1177,6 +1179,31 @@ public void onComplete(@NonNull Task<ProviderQueryResult> task) {
});
}

/**
* 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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Salakar @chrisbianca should I leave the native ones here for the time being? JS currently flags as unsupported due to Firebase issue, but no harm in them being here?

FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp);

firebaseAuth.useAppLanguage();
}

/* ------------------
* INTERNAL HELPERS
* ---------------- */
Expand Down Expand Up @@ -1451,4 +1478,29 @@ private void sendPhoneStateEvent(String appName, String requestKey, String type,
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;
}
}
47 changes: 47 additions & 0 deletions ios/RNFirebase/auth/RNFirebaseAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,34 @@ - (FIRAuthCredential *)getCredentialForProvider:(NSString *)provider token:(NSSt
return credential;
}

/**
setLanguageCode

@param NSString code
@return
*/
RCT_EXPORT_METHOD(setLanguageCode:
(NSString *) appName
code:
(NSString *) code) {
FIRApp *firApp = [FIRApp appNamed:appName];

[FIRAuth authWithApp:firApp].languageCode = code;
}

/**
useDeviceLanguage

@param NSString code
@return
*/
RCT_EXPORT_METHOD(useDeviceLanguage:
(NSString *) appName) {
FIRApp *firApp = [FIRApp appNamed:appName];

[[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
Expand Down Expand Up @@ -1147,6 +1175,25 @@ - (void)promiseWithUser:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseReje
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

Expand Down
5 changes: 5 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,11 @@ declare module "react-native-firebase" {
*/
currentUser: User | null

/**
* Gets/Sets the language for the app instance
*/
languageCode: string | null;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @chrisbianca not sure how to do a getter/setter in TypeScript...? This works for the getter but not sure if it's moan when trying to set it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ehesp it is ok, languageCode is a read/write property, the implementation is hidden.


/**
* Listen for changes in the users auth state (logging in and out).
* This method returns a unsubscribe function to stop listening to events.
Expand Down
23 changes: 23 additions & 0 deletions lib/modules/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default class Auth extends ModuleBase {
});
this._user = null;
this._authResult = null;
this._languageCode = getNativeModule(this).APP_LANGUAGE[app._name] || null;

SharedEventEmitter.addListener(
// sub to internal native event - this fans out to
Expand Down Expand Up @@ -334,6 +335,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}
Expand All @@ -342,6 +353,14 @@ export default class Auth extends ModuleBase {
return this._user;
}

get namespace(): string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed that this is no longer required either.

return 'firebase:auth';
}

get languageCode(): string {
return this._languageCode;
}

/**
* KNOWN UNSUPPORTED METHODS
*/
Expand All @@ -365,6 +384,10 @@ export default class Auth extends ModuleBase {
signInWithRedirect() {
throw new Error(INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD('auth', 'signInWithRedirect'));
}

useDeviceLanguage() {
throw new Error(INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD('auth', 'useDeviceLanguage'));
}
}

export const statics = {
Expand Down
24 changes: 24 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,30 @@ export function nativeToJSError(code: string, message: string, additionalProps?:
return error;
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has moved to lib/utils/native.js so doesn't need to be here anymore - I assume this was from a merge?

* TODO is this needed?
* Prepends appName arg to all native method calls
* @param appName
* @param NativeModule
*/
export function nativeWithApp(appName: string, NativeModule: Object) {
const native = {};
const methods = Object.keys(NativeModule);

for (let i = 0, len = methods.length; i < len; i++) {
const method = methods[i];
if (isFunction(NativeModule[method])) {
native[method] = (...args) => {
return NativeModule[method](...[appName, ...args]);
};
} else {
native[method] = NativeModule[method];
}
}

return native;
}

/**
*
* @param object
Expand Down
12 changes: 12 additions & 0 deletions tests/src/tests/auth/authTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 !== 'en') {
throw new Error('Expected language code to be "fr".');
}
firebase.native.auth().languageCode = 'en';
});
});
}

Expand Down