Skip to content

Commit

Permalink
Update GooglePlus.m
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeno97 authored Dec 29, 2024
1 parent dd5f792 commit df694c2
Showing 1 changed file with 87 additions and 74 deletions.
161 changes: 87 additions & 74 deletions src/ios/GooglePlus.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,91 @@ - (void) isAvailable:(CDVInvokedUrlCommand*)command {
}

- (void) login:(CDVInvokedUrlCommand*)command {
[[self getGIDSignInObject:command] signIn];
NSDictionary* options = command.arguments[0];
NSString *reversedClientId = [self getreversedClientId];
NSString *clientId = [self reverseUrlScheme:reversedClientId];

NSString* scopesString = options[@"scopes"];
NSString* serverClientId = options[@"webClientId"];
NSString *loginHint = options[@"loginHint"];
BOOL offline = [options[@"offline"] boolValue];
NSArray* scopes = nil;
if (scopesString != nil) {
scopes = [scopesString componentsSeparatedByString:@" "];
//[signIn setScopes:scopes];
}
[[self getGIDSignInObject:command] signInWithPresentingViewController:self.viewController hint:loginHint additionalScopes:scopes completion:^(GIDSignInResult * _Nullable signInResult, NSError * _Nullable error) {
if (error) {
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.localizedDescription];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self->_callbackId];
} else {
NSString *email = signInResult.user.profile.email;
NSString *idToken = signInResult.user.idToken.tokenString;
NSString *accessToken = signInResult.user.accessToken.tokenString;
NSString *refreshToken = signInResult.user.refreshToken.tokenString;
NSString *userId = signInResult.user.userID;
NSString *serverAuthCode = signInResult.serverAuthCode != nil ? signInResult.serverAuthCode : @"";
NSURL *imageUrl = [signInResult.user.profile imageURLWithDimension:120]; // TODO pass in img size as param, and try to sync with Android
NSDictionary *result = @{
@"email" : email,
@"idToken" : idToken,
@"serverAuthCode" : serverAuthCode,
@"accessToken" : accessToken,
@"refreshToken" : refreshToken,
@"userId" : userId,
@"displayName" : signInResult.user.profile.name ? : [NSNull null],
@"givenName" : signInResult.user.profile.givenName ? : [NSNull null],
@"familyName" : signInResult.user.profile.familyName ? : [NSNull null],
@"imageUrl" : imageUrl ? imageUrl.absoluteString : [NSNull null],
};

CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self->_callbackId];
}
}];
}

/** Get Google Sign-In object
@date July 19, 2015
*/
- (void) trySilentLogin:(CDVInvokedUrlCommand*)command {
[[self getGIDSignInObject:command] restorePreviousSignIn];
[[self getGIDSignInObject:command] restorePreviousSignInWithCompletion:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) {
if (error) {
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.localizedDescription];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self->_callbackId];
} else {
NSString *email = user.profile.email;
NSString *idToken = user.idToken.tokenString;
NSString *accessToken = user.accessToken.tokenString;
NSString *refreshToken = user.refreshToken.tokenString;
NSString *userId = user.userID;
//NSString *serverAuthCode = signInResult.serverAuthCode != nil ? signInResult.serverAuthCode : @"";
NSURL *imageUrl = [user.profile imageURLWithDimension:120]; // TODO pass in img size as param, and try to sync with Android
NSDictionary *result = @{
@"email" : email,
@"idToken" : idToken,
@"serverAuthCode" : @"",//serverAuthCode
@"accessToken" : accessToken,
@"refreshToken" : refreshToken,
@"userId" : userId,
@"displayName" : user.profile.name ? : [NSNull null],
@"givenName" : user.profile.givenName ? : [NSNull null],
@"familyName" : user.profile.familyName ? : [NSNull null],
@"imageUrl" : imageUrl ? imageUrl.absoluteString : [NSNull null],
};

CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self->_callbackId];
}
}];
}

/** Get Google Sign-In object
@date July 19, 2015
@date updated March 15, 2015 (@author PointSource,LLC)
*/
- (GIDSignIn*) getGIDSignInObject:(CDVInvokedUrlCommand*)command {
//global assignment for completion
_callbackId = command.callbackId;
NSDictionary* options = command.arguments[0];
NSString *reversedClientId = [self getreversedClientId];
Expand All @@ -66,34 +136,20 @@ - (GIDSignIn*) getGIDSignInObject:(CDVInvokedUrlCommand*)command {

NSString *clientId = [self reverseUrlScheme:reversedClientId];

NSString* scopesString = options[@"scopes"];
NSString* serverClientId = options[@"webClientId"];
NSString *loginHint = options[@"loginHint"];
BOOL offline = [options[@"offline"] boolValue];
NSString* hostedDomain = options[@"hostedDomain"];


GIDSignIn *signIn = [GIDSignIn sharedInstance];
signIn.clientID = clientId;

[signIn setLoginHint:loginHint];

GIDConfiguration *configuration = [[GIDConfiguration alloc] initWithClientID:clientId];
if (serverClientId != nil && offline) {
signIn.serverClientID = serverClientId;
}

if (hostedDomain != nil) {
signIn.hostedDomain = hostedDomain;
}

signIn.presentingViewController = self.viewController;
signIn.delegate = self;

// default scopes are email and profile
if (scopesString != nil) {
NSArray* scopes = [scopesString componentsSeparatedByString:@" "];
[signIn setScopes:scopes];
configuration = [[GIDConfiguration alloc] initWithClientID:clientId serverClientID:serverClientId hostedDomain:hostedDomain openIDRealm:nil];
}

[signIn setConfiguration:configuration];


return signIn;
}

Expand Down Expand Up @@ -128,62 +184,19 @@ - (void) logout:(CDVInvokedUrlCommand*)command {
}

- (void) disconnect:(CDVInvokedUrlCommand*)command {
[[GIDSignIn sharedInstance] disconnect];
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"disconnected"];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
[[GIDSignIn sharedInstance] disconnectWithCompletion:^(NSError * _Nullable error) {
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"disconnected"];
if (error != nil) {
NSLog(@"Failed to disconnect: %@", error.localizedDescription);
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.localizedDescription];
}

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}

- (void) share_unused:(CDVInvokedUrlCommand*)command {
// for a rainy day.. see for a (limited) example https://github.com/vleango/GooglePlus-PhoneGap-iOS/blob/master/src/ios/GPlus.m
}

#pragma mark - GIDSignInDelegate
/** Google Sign-In SDK
@date July 19, 2015
*/
- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error {
if (error) {
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.localizedDescription];
[self.commandDelegate sendPluginResult:pluginResult callbackId:_callbackId];
} else {
NSString *email = user.profile.email;
NSString *idToken = user.authentication.idToken;
NSString *accessToken = user.authentication.accessToken;
NSString *refreshToken = user.authentication.refreshToken;
NSString *userId = user.userID;
NSString *serverAuthCode = user.serverAuthCode != nil ? user.serverAuthCode : @"";
NSURL *imageUrl = [user.profile imageURLWithDimension:120]; // TODO pass in img size as param, and try to sync with Android
NSDictionary *result = @{
@"email" : email,
@"idToken" : idToken,
@"serverAuthCode" : serverAuthCode,
@"accessToken" : accessToken,
@"refreshToken" : refreshToken,
@"userId" : userId,
@"displayName" : user.profile.name ? : [NSNull null],
@"givenName" : user.profile.givenName ? : [NSNull null],
@"familyName" : user.profile.familyName ? : [NSNull null],
@"imageUrl" : imageUrl ? imageUrl.absoluteString : [NSNull null],
};

CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result];
[self.commandDelegate sendPluginResult:pluginResult callbackId:_callbackId];
}
}

/** Google Sign-In SDK
@date July 19, 2015
*/
- (void)signIn:(GIDSignIn *)signIn presentViewController:(UIViewController *)viewController {
self.isSigningIn = YES;
[self.viewController presentViewController:viewController animated:YES completion:nil];
}

/** Google Sign-In SDK
@date July 19, 2015
*/
- (void)signIn:(GIDSignIn *)signIn dismissViewController:(UIViewController *)viewController {
[self.viewController dismissViewControllerAnimated:YES completion:nil];
}

@end

0 comments on commit df694c2

Please sign in to comment.