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

Play Services Auth updated to 17.0.0 #4

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -42,7 +42,7 @@ So `getPackages()` should look like:
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new SmartLockPackager(MainApplication.this),
new SmartLockPackager(),
//.....
);
}
Expand All @@ -67,4 +67,15 @@ smartLock.getCredentials()

```

```javascript
smartLock.saveCredentials(username, JSON.stringify({'password': password}))
.then(() => {
console.warn('saved');
})
.catch(err => {
console.warn(err);
});

```


6 changes: 3 additions & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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', '+')}"
}
54 changes: 53 additions & 1 deletion android/src/main/java/com/google/smartlock/SmartLockModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -152,11 +159,13 @@ private void handleSuccess(Credential credential) {

String name = credential.getName();
String id = credential.getId();
String password = credential.getPassword();
List<IdToken> tokens = credential.getIdTokens();


obj.put("name", name);
obj.put("id", id);
obj.put("password", password);
obj.put("tokens", tokens.toString());

sLPromise.resolve(obj.toString());
Expand All @@ -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<Void>() {
@Override
public void onComplete(Task<Void> 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);
}
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ public class SmartLockPackager implements ReactPackage {

private Application application;

public SmartLockPackager(Application application) {
this.application = application;
}

@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(new SmartLockModule(reactContext, this.application));
Expand Down
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
export default smartLock;