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

iOS implementation of firebase social signIn (google, facebook, twitter) #66

Open
wants to merge 2 commits into
base: firebaseAuth
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
14 changes: 12 additions & 2 deletions qtfirebase_target.pri
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ HEADERS += \
$$PWD/src/platformutils.h \
$$PWD/src/qtfirebase.h \
$$PWD/src/qtfirebaseservice.h \
\
$$PWD/src/ios/socialsignin.h

SOURCES += \
$$PWD/src/qtfirebase.cpp \
$$PWD/src/qtfirebaseservice.cpp \
\
$$PWD/src/ios/socialsignin.mm

OTHER_FILES += \
$$PWD/src/android/GoogleSignInActivity.java \
Expand Down Expand Up @@ -265,7 +265,17 @@ contains(DEFINES,QTFIREBASE_BUILD_GOOGLE_AUTH) {
-F$$QTFIREBASE_FRAMEWORKS_ROOT/Auth \
-framework FirebaseAuth \
-framework GTMSessionFetcher \
-framework SafariServices \ # Required for Firebase iOS >= 4.4.0
\

LIBS += \
-F$$QTFIREBASE_FRAMEWORKS_ROOT/Invites \
-framework GoogleSignIn\
-framework GTMOAuth2\
\

GOOGLEKIT_BUNDLE.files = $$QTFIREBASE_FRAMEWORKS_ROOT/Invites/Resources/GoogleSignIn.bundle
QMAKE_BUNDLE_DATA += GOOGLEKIT_BUNDLE
}

HEADERS += $$PWD/src/qtfirebasegoogleauth.h
Expand Down
4 changes: 2 additions & 2 deletions src/ios/AppDelegate.h
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#import <UIKit/UIKit.h>

#import "Firebase/Firebase.h"
#import <GoogleSignIn/GoogleSignIn.h>

@interface AppDelegate : UIResponder<UIApplicationDelegate>
@interface AppDelegate : UIResponder<UIApplicationDelegate, GIDSignInDelegate>

+(AppDelegate *)sharedAppDelegate;

Expand Down
97 changes: 95 additions & 2 deletions src/ios/appdelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,116 @@

#import "Firebase/Firebase.h"

@interface QIOSApplicationDelegate : UIResponder <UIApplicationDelegate>
#import "AppDelegate.h"
#import "qtfirebaseauth.h"

//google header
#import <GoogleSignIn/GoogleSignIn.h>

//facebook heder
//#import <FBSDKCoreKit/FBSDKCoreKit.h>

// Twitter header
//#import <TwitterKit/TwitterKit.h>

@interface QIOSApplicationDelegate : UIResponder <UIApplicationDelegate, GIDSignInDelegate>
@end

@interface QIOSApplicationDelegate(AppDelegate)
@end

@implementation QIOSApplicationDelegate (AppDelegate)

// Firebase project ID e.g. : Your-App-ID

// Google sign in essentials
static NSString * const googleClientID = @"Your-Google-Client-ID";

// Facebook signin essentails
// * On the Facebook for Developers site, get the App ID and an App Secret for your app.
// * Enable Facebook Login:
// * In the Firebase console, open the Auth section.
// * On the Sign in method tab, enable the Facebook sign-in method and specify the App ID and App Secret you got from Facebook.
// * Then, make sure your OAuth redirect URI (e.g. https://Your-App-ID.firebaseapp.com/__/auth/handler) is listed as one of your OAuth redirect URIs in your Facebook app's settings page on the Facebook for Developers site in the Product Settings > Facebook Login config.
// * Add folowing lines in Info.plist
// <key>LSApplicationQueriesSchemes</key>
// <array>
// <string>fbauth2</string>
// </array>
// * Add URL schemes in Info.plist with your facebook app id
// <key>CFBundleURLTypes</key>
// <array>
// <dict>
// <key>CFBundleURLSchemes</key>
// <array>
// <string><your fb id here eg. fbxxxxxx></string>
// </array>
// </dict>
// </array>
// * your AppID and App DisplayName
static NSString * const facebookAppID = @"Your-Facebook-App-ID";
static NSString * const facebookAppDisplayName = @"Your-Facebook-App-Display-Name";

// Twitter sign in essentials
// * Register your app as a developer application on Twitter and get your app's API Key and API Secret.
// * Enable Twitter Login: In the Firebase console, open the Auth section.
// * On the Sign in method tab, enable the Twitter sign-in method and specify the API Key and API Secret you got from Twitter.
// * Then, make sure your Firebase OAuth redirect URI (e.g. https://Your-App-ID.firebaseapp.com/__/auth/handler) is set as your Callback URL in your app's settings page on your Twitter app's config.
// * Add folowing lines in Info.plist, in the section: <key>LSApplicationQueriesSchemes</key>
// <string>twitter</string>
// <string>twitterauth</string>
// * Add folowing lines in Info.plist, in the section: <key>CFBundleURLSchemes</key>
// <string>twitterkit-<Your-Consumer-Key></string>
static NSString * const twitterApiKey = @"Your-Twitter-Api-Key";
static NSString * const twitterApiSecret = @"Your-Twitter-Api-Secret";


- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
Q_UNUSED(application);
Q_UNUSED(launchOptions);

// Use Firebase library to configure APIs
//[FIRApp configure];
[FIRApp configure];
[GIDSignIn sharedInstance].clientID = googleClientID;

// Facebook configure
//[FBSDKSettings setAppID:facebookAppID];
//[FBSDKSettings setDisplayName:facebookAppDisplayName];

// Twitter configure
//[[Twitter sharedInstance] startWithConsumerKey:twitterApiKey consumerSecret:twitterApiSecret];

NSLog(@"FireApp configured successfully");

return YES;
}

// [START google facebook twitter openurl method]
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {

BOOL googleHandled = [[GIDSignIn sharedInstance] handleURL:url
sourceApplication:sourceApplication
annotation:annotation];

//BOOL facebookHandled = [[FBSDKApplicationDelegate sharedInstance] handleURL:url
sourceApplication:sourceApplication
annotation:annotation];

// BOOL twitterHandled = [[Twitter sharedInstance] handleURL:url
// sourceApplication:sourceApplication
// annotation:annotation];

return googleHandled /*|| facebookHandled || twitterHandled*/;
}
// [END google facebook twitter openurl method]

/*- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
return [[Twitter sharedInstance] application:app openURL:url options:options];
}*/

@end
23 changes: 23 additions & 0 deletions src/ios/socialsignin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <QObject>
#include <QtQml>

#import <UIKit/UIKit.h>
#import <GoogleSignIn/GoogleSignIn.h>
#import "Firebase/Firebase.h"

#include "qtfirebaseauth.h"

/*
* social signIn interface for ios
*/

// [START viewcontroller_interfaces]
@interface socialsignin : UIResponder <UIPageViewControllerDelegate, GIDSignInDelegate, GIDSignInUIDelegate>
{

}
// [END viewcontroller_interfaces]

@end


162 changes: 162 additions & 0 deletions src/ios/socialsignin.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#import "UIKit/UIKit.h"
#include <QtCore>
#import "socialsignin.h"
#import "qtfirebaseauth.h"
#import "qtfirebasegoogleauth.h"
#import "Firebase/Firebase.h"

// Facebook header
//#import <FBSDKCoreKit/FBSDKCoreKit.h>
//#import <FBSDKLoginKit/FBSDKLoginKit.h>

// Twitter header
//#import <TwitterKit/TwitterKit.h>

@interface QIOSViewController : UIResponder <UIPageViewControllerDelegate, GIDSignInDelegate, GIDSignInUIDelegate>
@end

@interface QIOSViewController(socialsignin)
@end

@implementation QIOSViewController (socialsignin)

UIWindow* rootWindow;

- (void)viewDidLoad {
[super viewDidLoad];

rootWindow = [PlatformUtils::getNativeWindow() window];

[GIDSignIn sharedInstance].delegate = self;
[GIDSignIn sharedInstance].uiDelegate = self;
}

void QtFirebaseAuth::googleSignIn()
{
//[GIDSignIn sharedInstance].shouldFetchBasicProfile = YES;
[GIDSignIn sharedInstance].scopes = @[@"profile", @"email"];

if ([GIDSignIn sharedInstance].hasAuthInKeychain) {

NSLog(@"user signed in already, signing silently...");
GIDGoogleUser *user = [GIDSignIn sharedInstance].currentUser;
NSString *userId = user.userID;
NSString *fullName = user.profile.name;
NSString *email = user.profile.email;
//NSLog(@"------->>>>User : %@, %@, %@", userId, fullName, email);
if (user.profile.hasImage)
{
NSURL *url = [user.profile imageURLWithDimension:100];
//NSLog(@"url : %@",url);
}
if(!user)
[[GIDSignIn sharedInstance] signInSilently];
}
else {
NSLog(@"Not signed in, start signing procedure");
[[GIDSignIn sharedInstance] signIn];
}
}

/*void QtFirebaseAuth::facebookSignIn()
{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];

if ([FBSDKAccessToken currentAccessToken])
{
NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);
//signInwithCredential(firebase::auth::FacebookAuthProvider::GetCredential([[FBSDKAccessToken currentAccessToken].tokenString UTF8String]));
}
else
{
[login logInWithReadPermissions: @[@"public_profile",@"email"]
fromViewController: rootWindow.rootViewController handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error) {
NSLog(@"Process error");
} else if (result.isCancelled) {
NSLog(@"Cancelled");
} else {
NSLog(@"facebook signing in");
//QString accessToken = QString::fromNSString([FBSDKAccessToken currentAccessToken].tokenString);
//signInwithCredential(firebase::auth::FacebookAuthProvider::GetCredential([[FBSDKAccessToken currentAccessToken].tokenString UTF8String]));
}
}];
}

[login logOut];
}*/

/*void QtFirebaseAuth::twitterSignIn()
{
// https://developer.twitter.com/en/docs/basics/authentication/overview/application-permission-model#announcement
// Application permission model
// There are three levels of permission available to applications:
// read only
// read and write
// read, write and access Direct Messages

[[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error) {
if (session) {

std::string idtoken = session.authToken.UTF8String;
std::string secret = session.authTokenSecret.UTF8String;

//signInwithCredential(firebase::auth::TwitterAuthProvider::GetCredential(idtoken.c_str(),secret.c_str()));

TWTRSessionStore *store = [[Twitter sharedInstance] sessionStore];
NSString *userID = store.session.userID;

[store logOutUserID:userID];

} else {
NSLog(@"error: %@", [error localizedDescription]);
}
}];
}*/

- (void) signIn:(GIDSignIn *)signIn
didSignInForUser:(GIDGoogleUser *)user
withError:(NSError *)error {
if (error) {
NSLog(@"error at sign in %@", error);
/*qFirebaseGoogleAuth->setError(error.code, QString::fromNSString(error.localizedDescription));
qFirebaseGoogleAuth->setSignIn(false);
qFirebaseGoogleAuth->setComplete(true);*/
return;
}
else
{
// The OAuth2 access token to access Google services, needed to connect with firebase
GIDAuthentication *authentication = user.authentication;
std::string idtoken = authentication.idToken.UTF8String;

NSString *userId = user.userID;
NSString *fullName = user.profile.name;
NSString *email = user.profile.email;
//NSLog(@"------->>>>User : %@, %@, %@", userId, fullName, email);

if (user.profile.hasImage)
{
NSURL *url = [user.profile imageURLWithDimension:100];
qFirebaseGoogleAuth->photoUrl = QString::fromUtf8(url.absoluteString.UTF8String);
qDebug() << "----->>>> Hereeee is fine the photo_url: " << qFirebaseGoogleAuth->photoUrl;
}

qFirebaseGoogleAuth->firebaseSignIn(QString::fromUtf8(idtoken.c_str()));

[[GIDSignIn sharedInstance] signOut];
}
}

- (void)signIn:(GIDSignIn *)signIn
didDisconnectWithUser:(GIDGoogleUser *)user
withError:(NSError *)error {
if (error) {
NSLog(@"error at sign out %@", error);
// qFirebaseGoogleAuth->setError(error.code, QString::fromNSString(error.localizedDescription));
return;
}
}

@end
Empty file modified src/qtfirebaseauth.cpp
100644 → 100755
Empty file.
5 changes: 5 additions & 0 deletions src/qtfirebaseauth.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public slots:
void sendPasswordResetEmail(const QString& email);
void deleteUser();

// social sign in
void googleSignIn();
//void facebookSignIn();
//void twitterSignIn();

//Status
bool signedIn() const;
bool running() const;
Expand Down
Loading