Our React Native SDK offers the easiest way to integrate Cotter 's email/phone verification. You can simply call a function and it does most of the heavy lifting and authentication for you.
Pick a unique URL scheme for redirecting the user back to your app after the verification in the in-app browser is successful. For this example, we'll use myexample://auth_callback .
Try it again! You should see the in-app browser redirecting back after you've successfully verified.
Step 4: Receiving the Token in onSuccess or onError
onError
The onError function that you pass in will receive 2 parameters: errorMessage (string) and errorResponse (object). The errorResponse is an http response from attempt to verify the user's email/phone in Cotter's server.
onSuccess
The onSuccess function that you pass in will receive a response object that looks like this:
This JSON object contains 2 objects, identifier and token .
The identifier object contains information about the user's email or phone number, device type and name, and expiry.
The token contains the user's phone number, your API_KEY_ID in the receiver field, and a signature to ensure this is from Cotter. The token tells you that this identifier is verified.
You should include this JSON Object into your call to your backend for Login or Registration. Your backend should then verify that the signature of the token is valid.
For example, if you have an existing Register function:
Validating Cotter's Access Token
Checkout how to verify the OAuth Tokens from Cotter here:
Since you'll be using your API Key from a front-end website or mobile app, your API_KEY_ID is exposed to anyone inspecting your code. Here are some ways to prevent abuse:
// ADD Cotter's Verify Class
import { Verify } from 'react-native-cotter';
class Register extends PureComponent {
...
openCotterAuth = async () => {
var verify = new Verify(
'myexample://auth_callback', // (setup later) URL Scheme for deep linking
API_KEY_ID, // your API_KEY_ID
this.onError, // error callback Function, receives (errorMessage, errorObject)
this.onSuccess, // error callback Function, receives (errorMessage, errorObject)
);
await verify.openAuth('EMAIL'); // EMAIL or PHONE
// You will need balance to send SMS. Otherwise, use "EMAIL"
};
onError = (errorMessage, errorObject) => {
alert(errorMessage);
console.log(errorObject);
};
onSuccess = response => {
alert("Success");
console.log(response);
};
...
}
import { Verify } from 'react-native-cotter';
class Register extends PureComponent {
...
openCotterAuth = async () => {
var verify = new Verify(
'myexample://auth_callback', // (setup later) URL Scheme for deep linking
API_KEY_ID, // your API_KEY_ID
this.onError, // error callback Function, receives (errorMessage, errorObject)
this.onSuccess, // error callback Function, receives (errorMessage, errorObject)
);
await verify.openAuthWithInput('EMAIL', this.state.email); // EMAIL or PHONE
};
onError = (errorMessage, errorObject) => {
alert(errorMessage);
console.log(errorObject);
};
onSuccess = response => {
alert("Success");
console.log(response);
};
...
}
<activity
android:name=".MainActivity"
android:launchMode="singleTask"> <!-- Make launchMode to singleTask -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- Setup Deep Linking Here -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- This is for myexample://auth_callback -->
<!-- π Change this to your own URL scheme -->
<data android:scheme="myexample" android:host="auth_callback"/>
</intent-filter>
<!-- end -->
</activity>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>myexample</string> <!-- π Change this to your own URL Scheme -->
<key>CFBundleURLSchemes</key>
<array>
<string>myexample</string> <!-- π Change this to your own URL Scheme -->
</array>
</dict>
</array>
// Add the header at the top of the file:
#import <React/RCTLinkingManager.h>
// Add this above `@end`:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
return [RCTLinkingManager application:application openURL:url options:options];
}
// Add the header at the top of the file:
#import <React/RCTLinkingManager.h>
// Add this above `@end`:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [RCTLinkingManager application:application openURL:url
sourceApplication:sourceApplication annotation:annotation];
}