-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathPFUser+Digits.m
220 lines (174 loc) · 7.49 KB
/
PFUser+Digits.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
//
// PFUser+Digits.m
//
// Created by Felix Dumit on 11/6/14.
// Copyright (c) 2017 Felix Dumit. All rights reserved.
//
#import <Bolts/Bolts.h>
#import <DigitsKit/DigitsKit.h>
#import "PFUser+Digits.h"
//TODO: change to digits?
NSString *const PFDigitsAuthenticationType = @"twitter";
static NSString *const kDigitsAuthParamToken = @"auth_token";
static NSString *const kDigitsAuthParamTokenSecret = @"auth_token_secret";
static NSString *const kDigitsAuthParamId = @"id";
static NSString *const kDigitsAuthParamPhone = @"phone";
static NSString *const kDigitsAuthParamEmail = @"email";
static NSString *const kDigitsAuthParamEmailVerified = @"email_verified";
@interface PFDigitsDelegate : NSObject<PFUserAuthenticationDelegate, DGTSessionUpdateDelegate>
+(NSDictionary<NSString *,NSString *> *)authDataForSession:(nonnull DGTSession*)session;
+(instancetype)sharedInstance;
@end
@implementation PFUser (Digits)
+(void) enableDigitsLogin {
PFDigitsDelegate* del = [PFDigitsDelegate sharedInstance];
[PFUser registerAuthenticationDelegate:del forAuthType:PFDigitsAuthenticationType];
[Digits sharedInstance].sessionUpdateDelegate = del;
}
- (BOOL)isLinkedWithDigits
{
return [self isLinkedWithAuthType:PFDigitsAuthenticationType];
}
-(NSDictionary*)digitsAuthData {
return [self valueForKeyPath:@"authData"][PFDigitsAuthenticationType];
}
-(NSString *)digitsId {
return [self digitsAuthData][kDigitsAuthParamId];
}
-(NSString*)digitsEmail {
return [self digitsAuthData][kDigitsAuthParamEmail];
}
-(NSString*)digitsPhone {
return [self digitsAuthData][kDigitsAuthParamPhone];
}
-(BOOL)digitsEmailVerified {
return [self digitsAuthData][kDigitsAuthParamEmailVerified] != nil;
}
#pragma mark - Parse Digits Login
+ (void) cleanupDigitsSessionAfterLogout
{
[[Digits sharedInstance] logOut];
}
+ (void)loginWithDigitsInBackground:(void (^)(__kindof PFUser *user, NSError *error))block
{
[self loginWithDigitsInBackgroundWithConfiguration:nil completion:block];
}
+ (void)loginWithDigitsInBackgroundWithConfiguration:(DGTAuthenticationConfiguration *)configuration completion:(void (^)(__kindof PFUser *, NSError *))block
{
[[self loginWithDigitsInBackgroundWithConfiguration:configuration]
continueWithExecutor:[BFExecutor mainThreadExecutor]
withBlock: ^id (BFTask *task) {
if (block) {
block(task.result, task.error);
}
return nil;
}];
}
+ (BFTask<__kindof PFUser *> *)loginWithDigitsInBackground
{
return [self loginWithDigitsInBackgroundWithConfiguration:nil];
}
+ (BFTask<__kindof PFUser *> *)loginWithDigitsInBackgroundWithConfiguration:(DGTAuthenticationConfiguration *)configuration
{
return [[self authenticateWithDigitsWithConfiguration:configuration] continueWithSuccessBlock:^id _Nullable(BFTask<DGTSession *> * _Nonnull task) {
return [PFUser logInWithAuthTypeInBackground:PFDigitsAuthenticationType authData:[PFDigitsDelegate authDataForSession:task.result]];
}];
}
#pragma mark - Parse Digits link
- (void)linkWithDigitsInBackground:(void (^)(BOOL succeeded, NSError *error))block
{
[self linkWithDigitsInBackgroundWithConfiguration:nil completion:block];
}
- (void)linkWithDigitsInBackgroundWithConfiguration:(DGTAuthenticationConfiguration *)configuration completion:(void (^)(BOOL, NSError *))block
{
[[self linkWithDigitsInBackgroundWithConfiguration:configuration]
continueWithExecutor:[BFExecutor mainThreadExecutor]
withBlock: ^id (BFTask *task) {
if (block) {
block(task.error == nil, task.error);
}
return nil;
}];
}
- (BFTask<NSNumber *> *)linkWithDigitsInBackground
{
return [self linkWithDigitsInBackgroundWithConfiguration:nil];
}
- (BFTask<NSNumber *> *)linkWithDigitsInBackgroundWithConfiguration:(DGTAuthenticationConfiguration *)configuration
{
return [[[self class] authenticateWithDigitsWithConfiguration:configuration] continueWithSuccessBlock:^id _Nullable(BFTask<DGTSession *> * _Nonnull task) {
return [self linkWithDigitsSession:task.result];
}];
}
-(BFTask<NSNumber *> *)linkWithDigitsSession:(DGTSession*)session {
return [self linkWithAuthTypeInBackground:PFDigitsAuthenticationType authData:[PFDigitsDelegate authDataForSession:session]];
}
-(BFTask<NSNumber *> *)unlinkWithDigits {
return [self unlinkWithAuthTypeInBackground:PFDigitsAuthenticationType];
}
#pragma mark - Digits
+ (BFTask <DGTSession *> *)authenticateWithDigitsWithConfiguration:(DGTAuthenticationConfiguration*)configuration {
BFTaskCompletionSource* tcs = [BFTaskCompletionSource taskCompletionSource];
[[Digits sharedInstance] authenticateWithViewController:nil configuration:configuration completion:^(DGTSession *session, NSError *error) {
if(error){
if([error.domain isEqualToString:@"DigitsErrorDomain"] && error.code == 1) {
[tcs trySetCancelled];
} else {
[tcs trySetError:error];
}
} else {
[tcs trySetResult:session];
}
}];
return tcs.task;
}
@end
@implementation PFDigitsDelegate
+ (instancetype)sharedInstance {
static PFDigitsDelegate* _sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}
-(BOOL)restoreAuthenticationWithAuthData:(NSDictionary<NSString *,NSString *> *)authData {
if (![authData isKindOfClass:[NSDictionary class]]) {
return NO;
}
DGTSession* session = [Digits sharedInstance].session;
if(![session.userID isEqualToString:authData[kDigitsAuthParamId]] ||
![session.authToken isEqualToString:authData[kDigitsAuthParamToken]]) {
//TODO: figure out how to do something like this (similar to how FB auth works:
// [Digits sharedInstance].session = [[DGTSession alloc] initWithAuthToken:authData[kDigitsAuthParamToken]
// authTokenSecret:authData[kDigitsAuthParamTokenSecret]
// userID:authData[kDigitsAuthParamId]
// phoneNumber:authData[kDigitsAuthParamPhone]
// emailAddress:authData[kDigitsAuthParamEmail]
// emailAddressIsVerified:authData[kDigitsAuthParamEmailVerified]];
}
return YES;
}
+(NSDictionary<NSString *,NSString *> *)authDataForSession:(nonnull DGTSession*)session {
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
dict[kDigitsAuthParamId] = session.userID;
dict[kDigitsAuthParamToken] = session.authToken;
dict[kDigitsAuthParamTokenSecret] = session.authTokenSecret;
dict[kDigitsAuthParamPhone] = session.phoneNumber;
dict[kDigitsAuthParamEmailVerified] = @(session.emailAddressIsVerified);
if(session.emailAddress) dict[kDigitsAuthParamEmail] = session.emailAddress;
return [dict copy];
}
-(void)digitsSessionHasChanged:(DGTSession *)newSession {
if(newSession) {
[[PFUser currentUser] linkWithDigitsSession:newSession];
} else {
NSAssert(newSession != nil, @"session should not be nil");
}
}
-(void)digitsSessionExpiredForUserID:(NSString *)userID {
if([[PFUser currentUser].digitsId isEqualToString:userID]) {
[PFUser logOutInBackground];
}
}
@end