-
-
Notifications
You must be signed in to change notification settings - Fork 875
/
RNGoogleSignin.m
231 lines (195 loc) · 7.74 KB
/
RNGoogleSignin.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#import <React/RCTLog.h>
#import <React/RCTUtils.h>
#import "RNGoogleSignin.h"
#import "RNGSPromiseWrapper.h"
@interface RNGoogleSignin ()
@property (nonatomic) RNGSPromiseWrapper *promiseWrapper;
@end
@implementation RNGoogleSignin
RCT_EXPORT_MODULE();
static NSString *const PLAY_SERVICES_NOT_AVAILABLE = @"PLAY_SERVICES_NOT_AVAILABLE";
// The key in `GoogleService-Info.plist` client id.
// For more see https://developers.google.com/identity/sign-in/ios/start
static NSString *const kClientIdKey = @"CLIENT_ID";
- (NSDictionary *)constantsToExport
{
return @{
@"BUTTON_SIZE_ICON": @(kGIDSignInButtonStyleIconOnly),
@"BUTTON_SIZE_STANDARD": @(kGIDSignInButtonStyleStandard),
@"BUTTON_SIZE_WIDE": @(kGIDSignInButtonStyleWide),
@"BUTTON_COLOR_LIGHT": @(kGIDSignInButtonColorSchemeLight),
@"BUTTON_COLOR_DARK": @(kGIDSignInButtonColorSchemeDark),
@"SIGN_IN_CANCELLED": [@(kGIDSignInErrorCodeCanceled) stringValue],
@"SIGN_IN_REQUIRED": [@(kGIDSignInErrorCodeHasNoAuthInKeychain) stringValue],
@"IN_PROGRESS": ASYNC_OP_IN_PROGRESS,
PLAY_SERVICES_NOT_AVAILABLE: PLAY_SERVICES_NOT_AVAILABLE // this never happens on iOS
};
}
+ (BOOL)requiresMainQueueSetup
{
return NO;
}
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
- (instancetype)init {
self = [super init];
if (self != nil) {
self.promiseWrapper = [[RNGSPromiseWrapper alloc] init];
}
return self;
}
RCT_EXPORT_METHOD(configure:(NSDictionary *)options
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"GoogleService-Info" ofType:@"plist"];
if (!options[@"iosClientId"] && !path) {
RCTLogError(@"RNGoogleSignin: failed to determine clientID - GoogleService-Info.plist was not found and you did not provide iosClientId.");
reject(@"INTERNAL_MISSING_CONFIG", @"Missing GoogleService-Info.plist", nil);
return;
}
[GIDSignIn sharedInstance].delegate = self;
[GIDSignIn sharedInstance].scopes = options[@"scopes"];
[GIDSignIn sharedInstance].shouldFetchBasicProfile = YES; // email, profile
[GIDSignIn sharedInstance].loginHint = options[@"loginHint"];
if (options[@"iosClientId"]) {
[GIDSignIn sharedInstance].clientID = options[@"iosClientId"];
} else {
NSDictionary *plist = [[NSDictionary alloc] initWithContentsOfFile:path];
[GIDSignIn sharedInstance].clientID = plist[kClientIdKey];
}
if (options[@"hostedDomain"]) {
[GIDSignIn sharedInstance].hostedDomain = options[@"hostedDomain"];
}
if (options[@"webClientId"]) {
[GIDSignIn sharedInstance].serverClientID = options[@"webClientId"];
}
resolve([NSNull null]);
}
RCT_EXPORT_METHOD(signInSilently:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
if ([self.promiseWrapper setPromiseWithInProgressCheck:resolve rejecter:reject fromCallSite:@"signInSilently"]) {
[[GIDSignIn sharedInstance] restorePreviousSignIn];
}
}
RCT_EXPORT_METHOD(signIn:(RCTPromiseResolveBlock)resolve
signInReject:(RCTPromiseRejectBlock)reject)
{
[GIDSignIn sharedInstance].presentingViewController = RCTPresentedViewController();
if ([self.promiseWrapper setPromiseWithInProgressCheck:resolve rejecter:reject fromCallSite:@"signIn"]) {
[[GIDSignIn sharedInstance] signIn];
}
}
RCT_EXPORT_METHOD(signOut:(RCTPromiseResolveBlock)resolve
signOutReject:(RCTPromiseRejectBlock)reject)
{
[[GIDSignIn sharedInstance] signOut];
resolve([NSNull null]);
}
RCT_EXPORT_METHOD(revokeAccess:(RCTPromiseResolveBlock)resolve
revokeAccessReject:(RCTPromiseRejectBlock)reject)
{
if ([self.promiseWrapper setPromiseWithInProgressCheck:resolve rejecter:reject fromCallSite:@"revokeAccess"]) {
[[GIDSignIn sharedInstance] disconnect];
}
}
RCT_EXPORT_METHOD(isSignedIn:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
BOOL isSignedIn = [[GIDSignIn sharedInstance] hasPreviousSignIn];
resolve(@(isSignedIn));
}
- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error {
if (error) {
[self rejectWithSigninError: error];
} else {
[self resolveWithUserDetails: user];
}
}
RCT_EXPORT_METHOD(getCurrentUser:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
GIDGoogleUser *currentUser = [GIDSignIn sharedInstance].currentUser;
resolve(RCTNullIfNil([self createUserDictionary:currentUser]));
}
RCT_EXPORT_METHOD(getTokens:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
GIDGoogleUser *currentUser = [GIDSignIn sharedInstance].currentUser;
if (currentUser == nil) {
reject(@"getTokens", @"getTokens requires a user to be signed in", nil);
return;
}
GIDAuthenticationHandler handler = ^void(GIDAuthentication *authentication, NSError *error) {
if (error) {
reject(@"getTokens", error.localizedDescription, nil);
} else {
resolve(@{
@"idToken" : authentication.idToken,
@"accessToken" : authentication.accessToken,
});
}
};
[currentUser.authentication getTokensWithHandler:handler];
}
- (void)resolveWithUserDetails: (GIDGoogleUser *) user {
[self.promiseWrapper resolve:[self createUserDictionary:user]];
}
- (NSDictionary*)createUserDictionary: (nullable GIDGoogleUser *) user {
if (!user) {
return nil;
}
NSURL *imageURL = user.profile.hasImage ? [user.profile imageURLWithDimension:120] : nil;
NSDictionary *userInfo = @{
@"id": user.userID,
@"name": RCTNullIfNil(user.profile.name),
@"givenName": RCTNullIfNil(user.profile.givenName),
@"familyName": RCTNullIfNil(user.profile.familyName),
@"photo": imageURL ? imageURL.absoluteString : [NSNull null],
@"email": user.profile.email,
};
NSDictionary *params = @{
@"user": userInfo,
@"idToken": user.authentication.idToken,
@"serverAuthCode": RCTNullIfNil(user.serverAuthCode),
@"scopes": user.grantedScopes,
};
return params;
}
- (void)rejectWithSigninError: (NSError *) error {
NSString *errorMessage = @"Unknown error when signing in.";
switch (error.code) {
case kGIDSignInErrorCodeUnknown:
errorMessage = @"Unknown error when signing in.";
break;
case kGIDSignInErrorCodeKeychain:
errorMessage = @"A problem reading or writing to the application keychain.";
break;
case kGIDSignInErrorCodeHasNoAuthInKeychain:
errorMessage = @"The user has never signed in before with the given scopes, or they have since signed out.";
break;
case kGIDSignInErrorCodeCanceled:
errorMessage = @"The user canceled the sign in request.";
break;
case kGIDSignInErrorCodeEMM:
errorMessage = @"An Enterprise Mobility Management related error has occurred.";
break;
}
[self.promiseWrapper reject:errorMessage withError:error];
}
- (void)signIn:(GIDSignIn *)signIn didDisconnectWithUser:(GIDGoogleUser *)user withError:(NSError *)error {
if (error) {
[self.promiseWrapper reject:@"Error when revoking access." withError:error];
} else {
[self.promiseWrapper resolve:[NSNull null]];
}
}
+ (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
return [[GIDSignIn sharedInstance] handleURL:url];
}
@end