Skip to content

Commit

Permalink
feat:Hint Method Added
Browse files Browse the repository at this point in the history
  • Loading branch information
pushpender-singh-ap committed Jul 9, 2022
1 parent fe2937a commit 24e4f48
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ yarn add react-native-otp-verify-remastered
```javascript
import RNOtpVerify from 'react-native-otp-verify-remastered';


// THIS PACKAGE ONLY FOR ANDROID

getHash = () =>
RNOtpVerify.getHash()
.then(console.log)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,20 @@

import java.util.ArrayList;

public class RNOtpVerifyModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import com.facebook.react.bridge.ActivityEventListener;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.credentials.Credential;
import com.google.android.gms.auth.api.credentials.HintRequest;
import com.google.android.gms.common.api.GoogleApiClient;

public class RNOtpVerifyModule extends ReactContextBaseJavaModule implements LifecycleEventListener, ActivityEventListener {
private static final String TAG = RNOtpVerifyModule.class.getSimpleName();
private static final int RESOLVE_HINT = 10001;
private GoogleApiClient apiClient;
private Promise requestHintCallback;
private final ReactApplicationContext reactContext;
private BroadcastReceiver mReceiver;
private boolean isReceiverRegistered = false;
Expand All @@ -34,13 +46,39 @@ public RNOtpVerifyModule(ReactApplicationContext reactContext) {
mReceiver = new OtpBroadcastReceiver(reactContext);
getReactApplicationContext().addLifecycleEventListener(this);
registerReceiverIfNecessary(mReceiver);

reactContext.addActivityEventListener(this);
apiClient = new GoogleApiClient.Builder(reactContext)
.addApi(Auth.CREDENTIALS_API)
.build();
}

@Override
public String getName() {
return "RNOtpVerify";
}

@ReactMethod
public void requestHint(Promise promise) {
Activity currentActivity = getCurrentActivity();
requestHintCallback = promise;


if (currentActivity == null) {
requestHintCallback.reject("No Activity Found", "Current Activity Null.");
return;
}
try {
HintRequest hintRequest = new HintRequest.Builder().setPhoneNumberIdentifierSupported(true).build();
PendingIntent intent = Auth.CredentialsApi.getHintPickerIntent(apiClient, hintRequest);

currentActivity.startIntentSenderForResult(intent.getIntentSender(), RESOLVE_HINT, null, 0, 0, 0);

} catch (Exception e) {
requestHintCallback.reject(e);
}
}

@ReactMethod
public void getOtp(Promise promise) {
requestOtp(promise);
Expand Down Expand Up @@ -123,6 +161,22 @@ public void onHostDestroy() {
unregisterReceiver(mReceiver);
}

@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
if (requestCode == RESOLVE_HINT) {
if (resultCode == Activity.RESULT_OK) {
Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
// credential.getId(); <-- will need to process phone number string
requestHintCallback.resolve(credential.getId());
}
}
}

@Override
public void onNewIntent(Intent intent) {

}

@ReactMethod
public void addListener(String eventName) {
// Keep: Required for RN built in Event Emitter Calls.
Expand Down
1 change: 1 addition & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
interface OtpVerify {
getOtp: () => Promise<boolean>;
getHash: () => Promise<string[]>;
requestHint: () => Promise<string>;
addListener: (handler: (value: string) => any) => import("react-native").EmitterSubscription;
removeListener: () => void;
}
Expand Down
1 change: 1 addition & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var RNOtpVerify = react_native_1.NativeModules.RNOtpVerify;
var OtpVerify = {
getOtp: RNOtpVerify === null || RNOtpVerify === void 0 ? void 0 : RNOtpVerify.getOtp,
getHash: RNOtpVerify === null || RNOtpVerify === void 0 ? void 0 : RNOtpVerify.getHash,
requestHint: RNOtpVerify === null || RNOtpVerify === void 0 ? void 0 : RNOtpVerify.requestHint,
addListener: function (handler) {
return react_native_1.DeviceEventEmitter
.addListener('com.faizalshap.otpVerify:otpReceived', handler);
Expand Down
3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ const RNOtpVerify = NativeModules.RNOtpVerify;
interface OtpVerify {
getOtp: () => Promise<boolean>;
getHash: () => Promise<string[]>;
requestHint: () => Promise<string>;
addListener: (handler: (value: string) => any) => import("react-native").EmitterSubscription;
removeListener: () => void;
}

const OtpVerify: OtpVerify = {
getOtp: RNOtpVerify?.getOtp,
getHash: RNOtpVerify?.getHash,

requestHint: RNOtpVerify?.requestHint,
addListener: (handler) =>
DeviceEventEmitter
.addListener('com.faizalshap.otpVerify:otpReceived', handler),
Expand Down

0 comments on commit 24e4f48

Please sign in to comment.