Skip to content

Commit e8a113a

Browse files
committed
Merge pull request #676 from ParsePlatform/nlutsenko.userAuthentication.async
Make UserAuthenticationController.logInUserAsync fully async.
2 parents 9c901d9 + 692c8f3 commit e8a113a

File tree

1 file changed

+45
-46
lines changed

1 file changed

+45
-46
lines changed

Parse/Internal/User/AuthenticationProviders/Controller/PFUserAuthenticationController.m

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
@interface PFUserAuthenticationController () {
2323
dispatch_queue_t _dataAccessQueue;
24-
NSMutableDictionary PF_GENERIC(NSString *, id<PFUserAuthenticationDelegate>) *_authenticationDelegates;
24+
NSMutableDictionary PF_GENERIC(NSString *, id<PFUserAuthenticationDelegate>)*_authenticationDelegates;
2525
}
2626

2727
@end
@@ -115,55 +115,54 @@ - (void)unregisterAuthenticationDelegateForAuthType:(NSString *)authType {
115115

116116
- (BFTask PF_GENERIC(PFUser *)*)logInUserAsyncWithAuthType:(NSString *)authType
117117
authData:(NSDictionary PF_GENERIC(NSString *, NSString *)*)authData {
118-
//TODO: (nlutsenko) Make it fully async.
119-
PFUser *currentUser = [PFUser currentUser];
120-
if (currentUser && [PFAnonymousUtils isLinkedWithUser:currentUser]) {
121-
if (currentUser.isLazy) {
122-
PFUser *user = currentUser;
123-
BFTask *resolveLaziness = nil;
124-
NSDictionary *oldAnonymousData = nil;
125-
@synchronized(user.lock) {
126-
oldAnonymousData = user.authData[PFAnonymousUserAuthenticationType];
127-
128-
// Replace any anonymity with the new linked authData
129-
[user stripAnonymity];
130-
131-
[user.authData setObject:authData forKey:authType];
132-
[user.linkedServiceNames addObject:authType];
133-
134-
resolveLaziness = [user resolveLazinessAsync:[BFTask taskWithResult:nil]];
135-
}
136-
137-
return [resolveLaziness continueAsyncWithBlock:^id(BFTask *task) {
138-
if (task.cancelled || task.faulted) {
139-
[user.authData removeObjectForKey:authType];
140-
[user.linkedServiceNames removeObject:authType];
141-
[user restoreAnonymity:oldAnonymousData];
142-
return task;
118+
return [[self.dataSource.currentUserController getCurrentUserAsyncWithOptions:0] continueWithSuccessBlock:^id(BFTask PF_GENERIC(PFUser *)*task) {
119+
PFUser *currentUser = task.result;
120+
if (currentUser && [PFAnonymousUtils isLinkedWithUser:currentUser]) {
121+
if (currentUser.isLazy) {
122+
BFTask *resolveLaziness = nil;
123+
NSDictionary *oldAnonymousData = nil;
124+
@synchronized(currentUser.lock) {
125+
oldAnonymousData = currentUser.authData[PFAnonymousUserAuthenticationType];
126+
127+
// Replace any anonymity with the new linked authData
128+
[currentUser stripAnonymity];
129+
130+
currentUser.authData[authType] = authData;
131+
[currentUser.linkedServiceNames addObject:authType];
132+
133+
resolveLaziness = [currentUser resolveLazinessAsync:[BFTask taskWithResult:nil]];
143134
}
144-
return task.result;
145-
}];
146-
} else {
147-
return [[currentUser linkWithAuthTypeInBackground:authType authData:authData] continueAsyncWithBlock:^id(BFTask *task) {
148-
NSError *error = task.error;
149-
if (error) {
150-
if (error.code == kPFErrorAccountAlreadyLinked) {
151-
// An account that's linked to the given authData already exists,
152-
// so log in instead of trying to claim.
153-
return [self.dataSource.userController logInCurrentUserAsyncWithAuthType:authType
154-
authData:authData
155-
revocableSession:[PFUser _isRevocableSessionEnabled]];
156-
} else {
135+
return [resolveLaziness continueWithBlock:^id(BFTask *task) {
136+
if (task.cancelled || task.faulted) {
137+
[currentUser.authData removeObjectForKey:authType];
138+
[currentUser.linkedServiceNames removeObject:authType];
139+
[currentUser restoreAnonymity:oldAnonymousData];
157140
return task;
158141
}
159-
}
160-
return currentUser;
161-
}];
142+
return task.result;
143+
}];
144+
} else {
145+
return [[currentUser linkWithAuthTypeInBackground:authType authData:authData] continueWithBlock:^id(BFTask *task) {
146+
NSError *error = task.error;
147+
if (error) {
148+
if (error.code == kPFErrorAccountAlreadyLinked) {
149+
// An account that's linked to the given authData already exists,
150+
// so log in instead of trying to claim.
151+
return [self.dataSource.userController logInCurrentUserAsyncWithAuthType:authType
152+
authData:authData
153+
revocableSession:[PFUser _isRevocableSessionEnabled]];
154+
} else {
155+
return task;
156+
}
157+
}
158+
return currentUser;
159+
}];
160+
}
162161
}
163-
}
164-
return [self.dataSource.userController logInCurrentUserAsyncWithAuthType:authType
165-
authData:authData
166-
revocableSession:[PFUser _isRevocableSessionEnabled]];
162+
return [self.dataSource.userController logInCurrentUserAsyncWithAuthType:authType
163+
authData:authData
164+
revocableSession:[PFUser _isRevocableSessionEnabled]];
165+
}];
167166
}
168167

169168
@end

0 commit comments

Comments
 (0)