diff --git a/README.md b/README.md index f76de70..fdaa789 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Add: 1. `import com.google.smartlock.smartlockrn.SmartLockPackager;` 2. In the `getPackages()` method register the module: -`new SmartLockPackager(MainApplication.this)` +`new SmartLockPackager()` So `getPackages()` should look like: @@ -42,7 +42,7 @@ So `getPackages()` should look like: protected List getPackages() { return Arrays.asList( new MainReactPackage(), - new SmartLockPackager(MainApplication.this), + new SmartLockPackager(), //..... ); } @@ -67,4 +67,15 @@ smartLock.getCredentials() ``` +```javascript +smartLock.saveCredentials(username, JSON.stringify({'password': password})) + .then(() => { + console.warn('saved'); + }) + .catch(err => { + console.warn(err); + }); + +``` + diff --git a/android/build.gradle b/android/build.gradle index f426901..3de84d0 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -24,9 +24,9 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - testCompile 'junit:junit:4.12' - implementation 'com.google.android.gms:play-services-auth:11.8.0' + implementation fileTree(dir: 'libs', include: ['*.jar']) + testImplementation 'junit:junit:4.12' + implementation 'com.google.android.gms:play-services-auth:17.0.0' implementation 'com.android.support:appcompat-v7:23.4.0' implementation "com.facebook.react:react-native:${safeExtGet('reactNativeVersion', '+')}" } diff --git a/android/src/main/java/com/google/smartlock/SmartLockModule.java b/android/src/main/java/com/google/smartlock/SmartLockModule.java index eabb11b..201edde 100644 --- a/android/src/main/java/com/google/smartlock/SmartLockModule.java +++ b/android/src/main/java/com/google/smartlock/SmartLockModule.java @@ -10,6 +10,7 @@ import android.util.Log; import android.widget.Toast; +import com.google.android.gms.auth.api.Auth; import com.facebook.react.bridge.ActivityEventListener; import com.facebook.react.bridge.BaseActivityEventListener; import com.facebook.react.bridge.Promise; @@ -21,12 +22,15 @@ import com.google.android.gms.auth.api.credentials.CredentialRequestResponse; import com.google.android.gms.auth.api.credentials.Credentials; import com.google.android.gms.auth.api.credentials.CredentialsClient; +import com.google.android.gms.auth.api.credentials.CredentialsOptions; import com.google.android.gms.auth.api.credentials.IdToken; import com.google.android.gms.auth.api.credentials.IdentityProviders; import com.google.android.gms.common.api.ApiException; import com.google.android.gms.common.api.ResolvableApiException; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; +import com.google.android.gms.common.api.ResultCallback; +import com.google.android.gms.common.api.Status; import org.json.JSONException; import org.json.JSONObject; @@ -41,7 +45,7 @@ public class SmartLockModule extends ReactContextBaseJavaModule { private static final int RC_READ = 4; private static final int SUCCESS_CODE = -1; private static final int CANCEL_CODE = 1001; - + private static final int RC_SAVE = 1; public SmartLockModule(ReactApplicationContext reactContext, Application application) { @@ -65,6 +69,9 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode, System.out.println("----------RESULT CODE : "+resultCode+"-----------"); if (resultCode == SUCCESS_CODE) { + if(intent == null || Credential.EXTRA_KEY == null){ + return; + } Credential credential = intent.getParcelableExtra(Credential.EXTRA_KEY); if (credential == null) { return; @@ -152,11 +159,13 @@ private void handleSuccess(Credential credential) { String name = credential.getName(); String id = credential.getId(); + String password = credential.getPassword(); List tokens = credential.getIdTokens(); obj.put("name", name); obj.put("id", id); + obj.put("password", password); obj.put("tokens", tokens.toString()); sLPromise.resolve(obj.toString()); @@ -166,4 +175,47 @@ private void handleSuccess(Credential credential) { } } + + @ReactMethod + public void saveCredentials(final String name, final String password, final Promise promise) { + this.sLPromise = promise; + CredentialsClient mCredentialsClient; + CredentialsOptions options = + new CredentialsOptions.Builder().forceEnableSaveDialog().build(); + mCredentialsClient = Credentials.getClient(this.mContext, options); + Credential credential = new Credential.Builder(name) + .setPassword(password) + .build(); + mCredentialsClient.save(credential).addOnCompleteListener(new OnCompleteListener() { + @Override + public void onComplete(Task task) { + if (task.isSuccessful()) { + Log.d("SmartLockModule", "Credential saved"); + sLPromise.resolve("Credential saved"); + return; + } + Exception e = task.getException(); + if (e instanceof ResolvableApiException) { + // Try to resolve the save request. This will prompt the user if + // the credential is new. + ResolvableApiException rae = (ResolvableApiException) e; + Activity activity = getCurrentActivity(); + if (activity == null) { + sLPromise.reject("Activity is null", "Activity is null"); + return; + } + try { + rae.startResolutionForResult(activity, RC_SAVE); + sLPromise.resolve("Credential saved"); + } catch (IntentSender.SendIntentException ex) { + Log.e("SmartLockModule", "Failed to send Credentials intent.", ex); + sLPromise.reject("Failed to send Credentials intent.", "Failed to send Credentials intent."); + } + }else{ + Log.d("SmartLockModule", "Unsuccessful credential save."); + sLPromise.reject("Unsuccessful credential save." + e, "Unsuccessful credential save." + e); + } + } + }); + } } diff --git a/android/src/main/java/com/google/smartlock/SmartLockPackager.java b/android/src/main/java/com/google/smartlock/SmartLockPackager.java index 9f718e8..83e4842 100644 --- a/android/src/main/java/com/google/smartlock/SmartLockPackager.java +++ b/android/src/main/java/com/google/smartlock/SmartLockPackager.java @@ -17,10 +17,6 @@ public class SmartLockPackager implements ReactPackage { private Application application; - public SmartLockPackager(Application application) { - this.application = application; - } - @Override public List createNativeModules(ReactApplicationContext reactContext) { return Arrays.asList(new SmartLockModule(reactContext, this.application)); diff --git a/index.js b/index.js index 4b0d92e..c587cc8 100644 --- a/index.js +++ b/index.js @@ -12,5 +12,13 @@ smartLock.getCredentials = () => { }); } +smartLock.saveCredentials = (name, password) => { + return new Promise((resolve, reject) => { + SmartLockRN.saveCredentials(name, password) + .then(() => resolve()) + .catch(err => reject(err)); + }); +} + -export default smartLock; \ No newline at end of file +export default smartLock;