@@ -16,14 +16,35 @@ @implementation FirestackAuth
16
16
17
17
RCT_EXPORT_MODULE (FirestackAuth);
18
18
19
+ RCT_EXPORT_METHOD (signInAnonymously:
20
+ (RCTResponseSenderBlock) callBack)
21
+ {
22
+
23
+ [[FIRAuth auth ] signInAnonymouslyWithCompletion
24
+ :^(FIRUser *user, NSError *error) {
25
+ if (!user) {
26
+ NSDictionary *evt = @{
27
+ @" eventName" : @" SignInAnonymouslyError" ,
28
+ @" msg" : [error localizedFailureReason ]
29
+ };
30
+
31
+ [self sendJSEvent: AUTH_CHANGED_EVENT props: evt];
32
+ callBack (@[evt]);
33
+ return ;
34
+ }
35
+ NSDictionary *userProps = [self userPropsFromFIRUser: user];
36
+ callBack (@[[NSNull null ], userProps]);
37
+ }];
38
+ }
39
+
19
40
RCT_EXPORT_METHOD (signInWithCustomToken:
20
41
(NSString *)customToken
21
42
callback:(RCTResponseSenderBlock) callback)
22
43
{
23
44
[[FIRAuth auth ]
24
45
signInWithCustomToken: customToken
25
46
completion: ^(FIRUser *user, NSError *error) {
26
-
47
+
27
48
if (user != nil ) {
28
49
NSDictionary *userProps = [self userPropsFromFIRUser: user];
29
50
callback (@[[NSNull null ], userProps]);
@@ -52,7 +73,7 @@ @implementation FirestackAuth
52
73
};
53
74
return callback (@[err]);
54
75
}
55
-
76
+
56
77
@try {
57
78
[[FIRAuth auth ] signInWithCredential: credential
58
79
completion: ^(FIRUser *user, NSError *error) {
@@ -98,7 +119,7 @@ @implementation FirestackAuth
98
119
self->authListenerHandle =
99
120
[[FIRAuth auth ] addAuthStateDidChangeListener: ^(FIRAuth *_Nonnull auth,
100
121
FIRUser *_Nullable user) {
101
-
122
+
102
123
if (user != nil ) {
103
124
// User is signed in.
104
125
[self userPropsFromFIRUserWithToken: user
@@ -146,7 +167,7 @@ @implementation FirestackAuth
146
167
RCT_EXPORT_METHOD (getCurrentUser:(RCTResponseSenderBlock)callback)
147
168
{
148
169
FIRUser *user = [FIRAuth auth ].currentUser ;
149
-
170
+
150
171
if (user != nil ) {
151
172
NSDictionary *userProps = [self userPropsFromFIRUser: user];
152
173
callback (@[[NSNull null ], userProps]);
@@ -191,7 +212,7 @@ @implementation FirestackAuth
191
212
completion: ^(FIRUser *user, NSError *error) {
192
213
if (user != nil ) {
193
214
NSDictionary *userProps = [self userPropsFromFIRUser: user];
194
-
215
+
195
216
callback (@[[NSNull null ], @{
196
217
@" user" : userProps
197
218
}]);
@@ -209,7 +230,7 @@ @implementation FirestackAuth
209
230
callback:(RCTResponseSenderBlock) callback)
210
231
{
211
232
FIRUser *user = [FIRAuth auth ].currentUser ;
212
-
233
+
213
234
[user updateEmail: email completion: ^(NSError *_Nullable error) {
214
235
if (error) {
215
236
// An error happened.
@@ -229,9 +250,9 @@ @implementation FirestackAuth
229
250
RCT_EXPORT_METHOD (updateUserPassword:(NSString *)newPassword
230
251
callback:(RCTResponseSenderBlock) callback)
231
252
{
232
-
253
+
233
254
FIRUser *user = [FIRAuth auth ].currentUser ;
234
-
255
+
235
256
[user updatePassword: newPassword completion: ^(NSError *_Nullable error) {
236
257
if (error) {
237
258
// An error happened.
@@ -251,7 +272,7 @@ @implementation FirestackAuth
251
272
RCT_EXPORT_METHOD (sendPasswordResetWithEmail:(NSString *)email
252
273
callback:(RCTResponseSenderBlock) callback)
253
274
{
254
-
275
+
255
276
[[FIRAuth auth ] sendPasswordResetWithEmail: email
256
277
completion: ^(NSError *_Nullable error) {
257
278
if (error) {
@@ -273,7 +294,7 @@ @implementation FirestackAuth
273
294
RCT_EXPORT_METHOD (deleteUser:(RCTResponseSenderBlock) callback)
274
295
{
275
296
FIRUser *user = [FIRAuth auth ].currentUser ;
276
-
297
+
277
298
[user deleteWithCompletion: ^(NSError *_Nullable error) {
278
299
if (error) {
279
300
NSDictionary *err =
@@ -290,7 +311,7 @@ @implementation FirestackAuth
290
311
RCT_EXPORT_METHOD (getToken:(RCTResponseSenderBlock) callback)
291
312
{
292
313
FIRUser *user = [FIRAuth auth ].currentUser ;
293
-
314
+
294
315
[user getTokenWithCompletion: ^(NSString *token, NSError *_Nullable error) {
295
316
if (error) {
296
317
NSDictionary *err =
@@ -308,7 +329,7 @@ @implementation FirestackAuth
308
329
RCT_EXPORT_METHOD (getTokenWithCompletion:(RCTResponseSenderBlock) callback)
309
330
{
310
331
FIRUser *user = [FIRAuth auth ].currentUser ;
311
-
332
+
312
333
[user getTokenWithCompletion: ^(NSString *token , NSError *_Nullable error) {
313
334
if (error) {
314
335
NSDictionary *err =
@@ -338,9 +359,9 @@ @implementation FirestackAuth
338
359
};
339
360
return callback (@[err]);
340
361
}
341
-
362
+
342
363
FIRUser *user = [FIRAuth auth ].currentUser ;
343
-
364
+
344
365
[user reauthenticateWithCredential: credential completion: ^(NSError *_Nullable error) {
345
366
if (error) {
346
367
NSDictionary *err =
@@ -360,7 +381,7 @@ @implementation FirestackAuth
360
381
{
361
382
FIRUser *user = [FIRAuth auth ].currentUser ;
362
383
FIRUserProfileChangeRequest *changeRequest = [user profileChangeRequest ];
363
-
384
+
364
385
NSMutableArray *allKeys = [[userProps allKeys ] mutableCopy ];
365
386
for (NSString *key in allKeys) {
366
387
// i.e. changeRequest.displayName = userProps[displayName];
@@ -405,12 +426,12 @@ - (NSDictionary *) userPropsFromFIRUser:(FIRUser *) user
405
426
@" refreshToken" : user.refreshToken ,
406
427
@" providerID" : user.providerID
407
428
} mutableCopy];
408
-
429
+
409
430
if ([user valueForKey: @" photoURL" ] != nil ) {
410
431
[userProps setValue: [NSString stringWithFormat: @" %@ " , user.photoURL]
411
432
forKey: @" photoURL" ];
412
433
}
413
-
434
+
414
435
return userProps;
415
436
}
416
437
@@ -422,7 +443,7 @@ - (void) userPropsFromFIRUserWithToken:(FIRUser *) user
422
443
if (error != nil ) {
423
444
return callback (nil , error);
424
445
}
425
-
446
+
426
447
[userProps setValue: token forKey: @" idToken" ];
427
448
callback (userProps, nil );
428
449
}];
0 commit comments