Skip to content

Commit

Permalink
Merge pull request arnesson#325 from jestcastro/master
Browse files Browse the repository at this point in the history
IOS-ANDROID PHONE AUTH
  • Loading branch information
robertarnesson authored Sep 8, 2017
2 parents 3c25a3b + 79dbf49 commit 6afd92e
Show file tree
Hide file tree
Showing 71 changed files with 3,141 additions and 319 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,47 @@ window.FirebasePlugin.setDefaults(defaults);
// or, specify a namespace
window.FirebasePlugin.setDefaults(defaults, "namespace");
```

### Phone Authentication
**BASED ON THE CONTRIBUTIONS OF**
IOS
https://github.com/silverio/cordova-plugin-

ANDROID
https://github.com/apptum/cordova-plugin-firebase

**((((IOS))): SETUP YOUR PUSH NOTIFICATIONS FIRST, AND VERIFY THAT THEY ARE ARRIVING TO YOUR PHYSICAL DEVICE BEFORE YOU TEST THIS METHOD. USE THE APNS AUTH KEY TO GENERATE THE .P8 FILE AND UPLOAD IT TO FIREBASE.
WHEN YOU CALL THIS METHOD, FCM SENDS A SILENT PUSH TO THE DEVICE TO VERIFY IT.**

This method sends an SMS to the user with the SMS_code and gets the verification id you need to continue the sign in process, with the Firebase JS SDK.

```
window.FirebasePlugin.getVerificationID("+573123456789",function(id) {
console.log("verificationID: "+id);
}, function(error) {
console.error(error);
});
```

Using Ionic2?
```
(<any>window).FirebasePlugin.getVerificationID("+573123456789", id => {
console.log("verificationID: " + id);
this.verificationId = id;
}, error => {
console.log("error: " + error);
});
```
Get the intermediate AuthCredential object
```
var credential = firebase.auth.PhoneAuthProvider.credential(verificationId, SMS_code);
```
Then, you can sign in the user with the credential:
```
firebase.auth().signInWithCredential(credential);
```
Or link to an account
```
firebase.auth().currentUser.linkWithCredential(credential)
```
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"cordova-browser",
"firebase",
"push",
"notifications"
"notifications",
"phone auth"
]
}
4 changes: 4 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,16 @@ xmlns:android="http://schemas.android.com/apk/res/android">

<framework custom="true" src="src/ios/Firebase/Analytics/FirebaseAnalytics.framework" />
<framework custom="true" src="src/ios/Firebase/Analytics/FirebaseCore.framework" />
<framework custom="true" src="src/ios/Firebase/Analytics/FirebaseCoreDiagnostics.framework" />
<framework custom="true" src="src/ios/Firebase/Analytics/FirebaseInstanceID.framework" />
<framework custom="true" src="src/ios/Firebase/Analytics/FirebaseNanoPB.framework" />
<framework custom="true" src="src/ios/Firebase/Analytics/GoogleToolboxForMac.framework" />
<framework custom="true" src="src/ios/Firebase/Messaging/Protobuf.framework" />
<framework custom="true" src="src/ios/Firebase/Messaging/FirebaseMessaging.framework" />
<framework custom="true" src="src/ios/Firebase/Crash/FirebaseCrash.framework" />
<framework custom="true" src="src/ios/Firebase/RemoteConfig/FirebaseRemoteConfig.framework" />
<framework custom="true" src="src/ios/Firebase/Auth/FirebaseAuth.framework" />
<framework custom="true" src="src/ios/Firebase/Auth/GTMSessionFetcher.framework" />
</platform>

<platform name="browser">
Expand Down
280 changes: 140 additions & 140 deletions scripts/after_prepare.js
Original file line number Diff line number Diff line change
@@ -1,141 +1,141 @@
#!/usr/bin/env node
'use strict';

/**
* This hook makes sure projects using [cordova-plugin-firebase](https://github.com/arnesson/cordova-plugin-firebase)
* will build properly and have the required key files copied to the proper destinations when the app is build on Ionic Cloud using the package command.
* Credits: https://github.com/arnesson.
*/
var fs = require('fs');
var path = require('path');

fs.ensureDirSync = function (dir) {
if (!fs.existsSync(dir)) {
dir.split(path.sep).reduce(function (currentPath, folder) {
currentPath += folder + path.sep;
if (!fs.existsSync(currentPath)) {
fs.mkdirSync(currentPath);
}
return currentPath;
}, '');
}
};

var config = fs.readFileSync('config.xml').toString();
var name = getValue(config, 'name');

var IOS_DIR = 'platforms/ios';
var ANDROID_DIR = 'platforms/android';

var PLATFORM = {
IOS: {
dest: [
IOS_DIR + '/' + name + '/Resources/GoogleService-Info.plist',
IOS_DIR + '/' + name + '/Resources/Resources/GoogleService-Info.plist'
],
src: [
'GoogleService-Info.plist',
IOS_DIR + '/www/GoogleService-Info.plist',
'www/GoogleService-Info.plist'
]
},
ANDROID: {
dest: [
ANDROID_DIR + '/google-services.json'
],
src: [
'google-services.json',
ANDROID_DIR + '/assets/www/google-services.json',
'www/google-services.json'
],
stringsXml: ANDROID_DIR + '/res/values/strings.xml'
}
};

function updateStringsXml(contents) {
var json = JSON.parse(contents);
var strings = fs.readFileSync(PLATFORM.ANDROID.stringsXml).toString();

// strip non-default value
strings = strings.replace(new RegExp('<string name="google_app_id">([^\@<]+?)</string>', 'i'), '');

// strip non-default value
strings = strings.replace(new RegExp('<string name="google_api_key">([^\@<]+?)</string>', 'i'), '');

// strip empty lines
strings = strings.replace(new RegExp('(\r\n|\n|\r)[ \t]*(\r\n|\n|\r)', 'gm'), '$1');

// replace the default value
strings = strings.replace(new RegExp('<string name="google_app_id">([^<]+?)</string>', 'i'), '<string name="google_app_id">' + json.client[0].client_info.mobilesdk_app_id + '</string>');

// replace the default value
strings = strings.replace(new RegExp('<string name="google_api_key">([^<]+?)</string>', 'i'), '<string name="google_api_key">' + json.client[0].api_key[0].current_key + '</string>');

fs.writeFileSync(PLATFORM.ANDROID.stringsXml, strings);
}

function copyKey(platform, callback) {
for (var i = 0; i < platform.src.length; i++) {
var file = platform.src[i];
if (fileExists(file)) {
try {
var contents = fs.readFileSync(file).toString();

try {
platform.dest.forEach(function (destinationPath) {
var folder = destinationPath.substring(0, destinationPath.lastIndexOf('/'));
fs.ensureDirSync(folder);
fs.writeFileSync(destinationPath, contents);
});
} catch (e) {
// skip
}

callback && callback(contents);
} catch (err) {
console.log(err)
}

break;
}
}
}

function getValue(config, name) {
var value = config.match(new RegExp('<' + name + '>(.*?)</' + name + '>', 'i'));
if (value && value[1]) {
return value[1]
} else {
return null
}
}

function fileExists(path) {
try {
return fs.statSync(path).isFile();
} catch (e) {
return false;
}
}

function directoryExists(path) {
try {
return fs.statSync(path).isDirectory();
} catch (e) {
return false;
}
}

module.exports = function(context) {
//get platform from the context supplied by cordova
var platforms = context.opts.platforms;
// Copy key files to their platform specific folders
if (platforms.indexOf('ios') !== -1 && directoryExists(IOS_DIR)) {
console.log('Preparing Firebase on iOS');
copyKey(PLATFORM.IOS);
}
if (platforms.indexOf('android') !== -1 && directoryExists(ANDROID_DIR)) {
console.log('Preparing Firebase on Android');
copyKey(PLATFORM.ANDROID, updateStringsXml)
}
#!/usr/bin/env node
'use strict';

/**
* This hook makes sure projects using [cordova-plugin-firebase](https://github.com/arnesson/cordova-plugin-firebase)
* will build properly and have the required key files copied to the proper destinations when the app is build on Ionic Cloud using the package command.
* Credits: https://github.com/arnesson.
*/
var fs = require('fs');
var path = require('path');

fs.ensureDirSync = function (dir) {
if (!fs.existsSync(dir)) {
dir.split(path.sep).reduce(function (currentPath, folder) {
currentPath += folder + path.sep;
if (!fs.existsSync(currentPath)) {
fs.mkdirSync(currentPath);
}
return currentPath;
}, '');
}
};

var config = fs.readFileSync('config.xml').toString();
var name = getValue(config, 'name');

var IOS_DIR = 'platforms/ios';
var ANDROID_DIR = 'platforms/android';

var PLATFORM = {
IOS: {
dest: [
IOS_DIR + '/' + name + '/Resources/GoogleService-Info.plist',
IOS_DIR + '/' + name + '/Resources/Resources/GoogleService-Info.plist'
],
src: [
'GoogleService-Info.plist',
IOS_DIR + '/www/GoogleService-Info.plist',
'www/GoogleService-Info.plist'
]
},
ANDROID: {
dest: [
ANDROID_DIR + '/google-services.json'
],
src: [
'google-services.json',
ANDROID_DIR + '/assets/www/google-services.json',
'www/google-services.json'
],
stringsXml: ANDROID_DIR + '/res/values/strings.xml'
}
};

function updateStringsXml(contents) {
var json = JSON.parse(contents);
var strings = fs.readFileSync(PLATFORM.ANDROID.stringsXml).toString();

// strip non-default value
strings = strings.replace(new RegExp('<string name="google_app_id">([^\@<]+?)</string>', 'i'), '');

// strip non-default value
strings = strings.replace(new RegExp('<string name="google_api_key">([^\@<]+?)</string>', 'i'), '');

// strip empty lines
strings = strings.replace(new RegExp('(\r\n|\n|\r)[ \t]*(\r\n|\n|\r)', 'gm'), '$1');

// replace the default value
strings = strings.replace(new RegExp('<string name="google_app_id">([^<]+?)</string>', 'i'), '<string name="google_app_id">' + json.client[0].client_info.mobilesdk_app_id + '</string>');

// replace the default value
strings = strings.replace(new RegExp('<string name="google_api_key">([^<]+?)</string>', 'i'), '<string name="google_api_key">' + json.client[0].api_key[0].current_key + '</string>');

fs.writeFileSync(PLATFORM.ANDROID.stringsXml, strings);
}

function copyKey(platform, callback) {
for (var i = 0; i < platform.src.length; i++) {
var file = platform.src[i];
if (fileExists(file)) {
try {
var contents = fs.readFileSync(file).toString();

try {
platform.dest.forEach(function (destinationPath) {
var folder = destinationPath.substring(0, destinationPath.lastIndexOf('/'));
fs.ensureDirSync(folder);
fs.writeFileSync(destinationPath, contents);
});
} catch (e) {
// skip
}

callback && callback(contents);
} catch (err) {
console.log(err)
}

break;
}
}
}

function getValue(config, name) {
var value = config.match(new RegExp('<' + name + '>(.*?)</' + name + '>', 'i'));
if (value && value[1]) {
return value[1]
} else {
return null
}
}

function fileExists(path) {
try {
return fs.statSync(path).isFile();
} catch (e) {
return false;
}
}

function directoryExists(path) {
try {
return fs.statSync(path).isDirectory();
} catch (e) {
return false;
}
}

module.exports = function(context) {
//get platform from the context supplied by cordova
var platforms = context.opts.platforms;
// Copy key files to their platform specific folders
if (platforms.indexOf('ios') !== -1 && directoryExists(IOS_DIR)) {
console.log('Preparing Firebase on iOS');
copyKey(PLATFORM.IOS);
}
if (platforms.indexOf('android') !== -1 && directoryExists(ANDROID_DIR)) {
console.log('Preparing Firebase on Android');
copyKey(PLATFORM.ANDROID, updateStringsXml)
}
};
2 changes: 1 addition & 1 deletion src/android/FirebasePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -693,4 +693,4 @@ public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingTo
}
});
}
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#import "FIRAnalytics.h"

NS_ASSUME_NONNULL_BEGIN

/**
* Provides App Delegate handlers to be used in your App Delegate.
*
Expand Down Expand Up @@ -29,14 +31,14 @@
* updated and a new snapshot can be taken.
*/
+ (void)handleEventsForBackgroundURLSession:(NSString *)identifier
completionHandler:(void (^)(void))completionHandler;
completionHandler:(nullable void (^)(void))completionHandler;

/**
* Handles the event when the app is launched by a URL.
*
* Call this method from [UIApplicationDelegate application:openURL:options:] (on iOS 9.0 and
* above), or [UIApplicationDelegate application:openURL:sourceApplication:annotation:] (on iOS 8.x
* and below) in your app.
* Call this method from [UIApplicationDelegate application:openURL:options:] &#40;on iOS 9.0 and
* above&#41;, or [UIApplicationDelegate application:openURL:sourceApplication:annotation:] &#40;on
* iOS 8.x and below&#41; in your app.
*
* @param url The URL resource to open. This resource can be a network resource or a file.
*/
Expand All @@ -55,3 +57,6 @@
+ (void)handleUserActivity:(id)userActivity;

@end

NS_ASSUME_NONNULL_END

Loading

0 comments on commit 6afd92e

Please sign in to comment.