2017-03-09 15:26:28 +00:00
# import "RNFirebaseAuth.h"
# import "RNFirebaseEvents.h"
2017-05-25 14:33:41 +00:00
# if __has _include ( < FirebaseAuth / FIRAuth . h > )
@ implementation RNFirebaseAuth
RCT_EXPORT _MODULE ( ) ;
2017-03-09 15:26:28 +00:00
2017-03-17 19:08:51 +00:00
/ * *
addAuthStateListener
2017-05-31 14:22:15 +00:00
2017-03-17 19:08:51 +00:00
* /
RCT_EXPORT _METHOD ( addAuthStateListener ) {
self -> listening = true ;
self -> authListenerHandle = [ [ FIRAuth auth ] addAuthStateDidChangeListener : ^ ( FIRAuth * _Nonnull auth , FIRUser * _Nullable user ) {
if ( user ! = nil ) {
[ self sendJSEvent : AUTH_CHANGED _EVENT props : @ { @ "authenticated" : @ ( true ) , @ "user" : [ self firebaseUserToDict : user ] } ] ;
} else {
[ self sendJSEvent : AUTH_CHANGED _EVENT props : @ { @ "authenticated" : @ ( false ) } ] ;
}
} ] ;
}
/ * *
removeAuthStateListener
2017-05-31 14:22:15 +00:00
2017-03-17 19:08:51 +00:00
* /
RCT_EXPORT _METHOD ( removeAuthStateListener ) {
if ( self -> authListenerHandle ! = nil ) {
[ [ FIRAuth auth ] removeAuthStateDidChangeListener : self -> authListenerHandle ] ;
self -> listening = false ;
}
}
2017-03-14 19:04:16 +00:00
/ * *
signOut
2017-05-31 14:22:15 +00:00
2017-03-14 19:04:16 +00:00
@ param RCTPromiseResolveBlock resolve
@ param RCTPromiseRejectBlock reject
@ return
* /
RCT_EXPORT _METHOD ( signOut : ( RCTPromiseResolveBlock ) resolve rejecter : ( RCTPromiseRejectBlock ) reject ) {
FIRUser * user = [ FIRAuth auth ] . currentUser ;
2017-05-31 14:22:15 +00:00
2017-03-14 19:04:16 +00:00
if ( user ) {
NSError * error ;
[ [ FIRAuth auth ] signOut : & error ] ;
if ( ! error ) [ self promiseNoUser : resolve rejecter : reject isError : NO ] ;
2017-03-17 21:41:17 +00:00
else [ self promiseRejectAuthException : reject error : error ] ;
2017-03-14 19:04:16 +00:00
} else {
[ self promiseNoUser : resolve rejecter : reject isError : YES ] ;
2017-03-09 15:26:28 +00:00
}
}
2017-03-14 19:04:16 +00:00
/ * *
signInAnonymously
2017-05-31 14:22:15 +00:00
2017-03-14 19:04:16 +00:00
@ param RCTPromiseResolveBlock resolve
@ param RCTPromiseRejectBlock reject
@ return
* /
RCT_EXPORT _METHOD ( signInAnonymously : ( RCTPromiseResolveBlock ) resolve rejecter : ( RCTPromiseRejectBlock ) reject ) {
[ [ FIRAuth auth ] signInAnonymouslyWithCompletion : ^ ( FIRUser * user , NSError * error ) {
if ( error ) {
2017-03-17 21:41:17 +00:00
[ self promiseRejectAuthException : reject error : error ] ;
2017-03-14 19:04:16 +00:00
} else {
[ self promiseWithUser : resolve rejecter : reject user : user ] ;
}
} ] ;
2017-05-31 14:22:15 +00:00
2017-03-14 19:04:16 +00:00
}
/ * *
signInWithEmailAndPassword
2017-05-31 14:22:15 +00:00
2017-03-14 19:04:16 +00:00
@ param NSString NSString email
@ param NSString NSString password
@ param RCTPromiseResolveBlock resolve
@ param RCTPromiseRejectBlock reject
@ return return
* /
RCT_EXPORT _METHOD ( signInWithEmailAndPassword : ( NSString * ) email pass : ( NSString * ) password resolver : ( RCTPromiseResolveBlock ) resolve rejecter : ( RCTPromiseRejectBlock ) reject ) {
[ [ FIRAuth auth ] signInWithEmail : email password : password completion : ^ ( FIRUser * user , NSError * error ) {
if ( error ) {
2017-03-17 21:41:17 +00:00
[ self promiseRejectAuthException : reject error : error ] ;
2017-03-14 19:04:16 +00:00
} else {
[ self promiseWithUser : resolve rejecter : reject user : user ] ;
}
} ] ;
}
/ * *
createUserWithEmailAndPassword
2017-05-31 14:22:15 +00:00
2017-03-14 19:04:16 +00:00
@ param NSString NSString email
@ param NSString NSString password
@ param RCTPromiseResolveBlock resolve
@ param RCTPromiseRejectBlock reject
@ return return
* /
RCT_EXPORT _METHOD ( createUserWithEmailAndPassword : ( NSString * ) email pass : ( NSString * ) password resolver : ( RCTPromiseResolveBlock ) resolve rejecter : ( RCTPromiseRejectBlock ) reject ) {
[ [ FIRAuth auth ] createUserWithEmail : email password : password completion : ^ ( FIRUser * user , NSError * error ) {
if ( error ) {
2017-03-17 21:41:17 +00:00
[ self promiseRejectAuthException : reject error : error ] ;
2017-03-14 19:04:16 +00:00
} else {
[ self promiseWithUser : resolve rejecter : reject user : user ] ;
}
} ] ;
}
/ * *
deleteUser
2017-05-31 14:22:15 +00:00
2017-03-14 19:04:16 +00:00
@ param RCTPromiseResolveBlock resolve
@ param RCTPromiseRejectBlock reject
@ return return
* /
RCT_EXPORT _METHOD ( delete : ( RCTPromiseResolveBlock ) resolve rejecter : ( RCTPromiseRejectBlock ) reject ) {
FIRUser * user = [ FIRAuth auth ] . currentUser ;
2017-05-31 14:22:15 +00:00
2017-03-14 19:04:16 +00:00
if ( user ) {
[ user deleteWithCompletion : ^ ( NSError * _Nullable error ) {
if ( error ) {
2017-03-17 21:41:17 +00:00
[ self promiseRejectAuthException : reject error : error ] ;
2017-03-14 19:04:16 +00:00
} else {
[ self promiseNoUser : resolve rejecter : reject isError : NO ] ;
}
} ] ;
} else {
[ self promiseNoUser : resolve rejecter : reject isError : YES ] ;
}
}
2017-03-18 00:31:02 +00:00
/ * *
reload
2017-05-31 14:22:15 +00:00
2017-03-18 00:31:02 +00:00
@ param RCTPromiseResolveBlock resolve
@ param RCTPromiseRejectBlock reject
@ return return
* /
RCT_EXPORT _METHOD ( reload : ( RCTPromiseResolveBlock ) resolve rejecter : ( RCTPromiseRejectBlock ) reject ) {
FIRUser * user = [ FIRAuth auth ] . currentUser ;
2017-05-31 14:22:15 +00:00
2017-03-18 00:31:02 +00:00
if ( user ) {
[ user reloadWithCompletion : ^ ( NSError * _Nullable error ) {
if ( error ) {
[ self promiseRejectAuthException : reject error : error ] ;
} else {
FIRUser * userAfterReload = [ FIRAuth auth ] . currentUser ;
[ self promiseWithUser : resolve rejecter : reject user : userAfterReload ] ;
}
} ] ;
} else {
[ self promiseNoUser : resolve rejecter : reject isError : YES ] ;
}
}
/ * *
sendEmailVerification
2017-05-31 14:22:15 +00:00
2017-03-18 00:31:02 +00:00
@ param RCTPromiseResolveBlock resolve
@ param RCTPromiseRejectBlock reject
@ return return
* /
RCT_EXPORT _METHOD ( sendEmailVerification : ( RCTPromiseResolveBlock ) resolve rejecter : ( RCTPromiseRejectBlock ) reject ) {
FIRUser * user = [ FIRAuth auth ] . currentUser ;
2017-05-31 14:22:15 +00:00
2017-03-18 00:31:02 +00:00
if ( user ) {
[ user sendEmailVerificationWithCompletion : ^ ( NSError * _Nullable error ) {
if ( error ) {
[ self promiseRejectAuthException : reject error : error ] ;
} else {
[ self promiseNoUser : resolve rejecter : reject isError : NO ] ;
}
} ] ;
} else {
[ self promiseNoUser : resolve rejecter : reject isError : YES ] ;
}
}
/ * *
updateEmail
2017-05-31 14:22:15 +00:00
2017-03-18 00:31:02 +00:00
@ param NSString email
@ param RCTPromiseResolveBlock resolve
@ param RCTPromiseRejectBlock reject
@ return return
* /
RCT_EXPORT _METHOD ( updateEmail : ( NSString * ) email resolver : ( RCTPromiseResolveBlock ) resolve rejecter : ( RCTPromiseRejectBlock ) reject ) {
FIRUser * user = [ FIRAuth auth ] . currentUser ;
2017-05-31 14:22:15 +00:00
2017-03-18 00:31:02 +00:00
if ( user ) {
[ user updateEmail : email completion : ^ ( NSError * _Nullable error ) {
if ( error ) {
[ self promiseRejectAuthException : reject error : error ] ;
} else {
FIRUser * userAfterUpdate = [ FIRAuth auth ] . currentUser ;
[ self promiseWithUser : resolve rejecter : reject user : userAfterUpdate ] ;
}
} ] ;
} else {
[ self promiseNoUser : resolve rejecter : reject isError : YES ] ;
}
}
2017-03-18 00:41:10 +00:00
/ * *
updatePassword
2017-05-31 14:22:15 +00:00
2017-03-18 00:41:10 +00:00
@ param NSString password
@ param RCTPromiseResolveBlock resolve
@ param RCTPromiseRejectBlock reject
@ return return
* /
RCT_EXPORT _METHOD ( updatePassword : ( NSString * ) password resolver : ( RCTPromiseResolveBlock ) resolve rejecter : ( RCTPromiseRejectBlock ) reject ) {
FIRUser * user = [ FIRAuth auth ] . currentUser ;
2017-05-31 14:22:15 +00:00
2017-03-18 00:41:10 +00:00
if ( user ) {
[ user updatePassword : password completion : ^ ( NSError * _Nullable error ) {
if ( error ) {
[ self promiseRejectAuthException : reject error : error ] ;
} else {
FIRUser * userAfterUpdate = [ FIRAuth auth ] . currentUser ;
[ self promiseWithUser : resolve rejecter : reject user : userAfterUpdate ] ;
}
} ] ;
} else {
[ self promiseNoUser : resolve rejecter : reject isError : YES ] ;
}
}
2017-03-14 19:04:16 +00:00
2017-03-18 01:10:22 +00:00
/ * *
updateProfile
2017-05-31 14:22:15 +00:00
2017-03-18 01:10:22 +00:00
@ param NSDictionary props
@ param RCTPromiseResolveBlock resolve
@ param RCTPromiseRejectBlock reject
@ return return
* /
RCT_EXPORT _METHOD ( updateProfile : ( NSDictionary * ) props resolver : ( RCTPromiseResolveBlock ) resolve rejecter : ( RCTPromiseRejectBlock ) reject ) {
FIRUser * user = [ FIRAuth auth ] . currentUser ;
2017-05-31 14:22:15 +00:00
2017-03-18 01:10:22 +00:00
if ( user ) {
FIRUserProfileChangeRequest * changeRequest = [ user profileChangeRequest ] ;
NSMutableArray * allKeys = [ [ props allKeys ] mutableCopy ] ;
2017-05-31 14:22:15 +00:00
2017-03-18 01:10:22 +00:00
for ( NSString * key in allKeys ) {
@ try {
if ( [ key isEqualToString : @ "photoURL" ] ) {
NSURL * url = [ NSURL URLWithString : [ props valueForKey : key ] ] ;
[ changeRequest setValue : url forKey : key ] ;
} else {
2017-05-25 14:33:41 +00:00
[ changeRequest setValue : props [ key ] forKey : key ] ;
2017-03-18 01:10:22 +00:00
}
} @ catch ( NSException * exception ) {
NSLog ( @ "Exception occurred while configuring: %@" , exception ) ;
}
}
2017-05-31 14:22:15 +00:00
2017-03-18 01:10:22 +00:00
[ changeRequest commitChangesWithCompletion : ^ ( NSError * _Nullable error ) {
if ( error ) {
[ self promiseRejectAuthException : reject error : error ] ;
} else {
FIRUser * userAfterUpdate = [ FIRAuth auth ] . currentUser ;
[ self promiseWithUser : resolve rejecter : reject user : userAfterUpdate ] ;
}
} ] ;
} else {
[ self promiseNoUser : resolve rejecter : reject isError : YES ] ;
}
}
2017-03-16 12:35:03 +00:00
/ * *
getToken
2017-05-31 14:22:15 +00:00
2017-03-16 12:35:03 +00:00
@ param RCTPromiseResolveBlock resolve
@ param RCTPromiseRejectBlock reject
@ return
* /
2017-03-30 09:55:16 +00:00
RCT_EXPORT _METHOD ( getToken : ( BOOL ) forceRefresh resolver : ( RCTPromiseResolveBlock ) resolve rejecter : ( RCTPromiseRejectBlock ) reject ) {
2017-03-16 12:35:03 +00:00
FIRUser * user = [ FIRAuth auth ] . currentUser ;
2017-05-31 14:22:15 +00:00
2017-03-16 12:35:03 +00:00
if ( user ) {
2017-06-13 05:21:45 +00:00
[ user getTokenForcingRefresh : ( BOOL ) forceRefresh completion : ^ ( NSString * token , NSError * _Nullable error ) {
2017-03-16 12:35:03 +00:00
if ( error ) {
2017-03-17 21:41:17 +00:00
[ self promiseRejectAuthException : reject error : error ] ;
2017-03-16 12:35:03 +00:00
} else {
resolve ( token ) ;
}
} ] ;
} else {
[ self promiseNoUser : resolve rejecter : reject isError : YES ] ;
}
}
2017-03-14 19:04:16 +00:00
2017-03-17 18:04:13 +00:00
/ * *
signInWithCredential
2017-05-31 14:22:15 +00:00
2017-03-17 18:04:13 +00:00
@ param NSString provider
@ param NSString authToken
@ param NSString authSecret
@ param RCTPromiseResolveBlock resolve
@ param RCTPromiseRejectBlock reject
@ return
* /
RCT_EXPORT _METHOD ( signInWithCredential : ( NSString * ) provider token : ( NSString * ) authToken secret : ( NSString * ) authSecret resolver : ( RCTPromiseResolveBlock ) resolve rejecter : ( RCTPromiseRejectBlock ) reject ) {
FIRAuthCredential * credential = [ self getCredentialForProvider : provider token : authToken secret : authSecret ] ;
2017-05-31 14:22:15 +00:00
2017-03-17 18:04:13 +00:00
if ( credential = = nil ) {
2017-03-17 23:33:55 +00:00
return reject ( @ "auth/invalid-credential" , @ "The supplied auth credential is malformed, has expired or is not currently supported." , nil ) ;
2017-03-17 18:04:13 +00:00
}
2017-05-31 14:22:15 +00:00
2017-03-17 18:04:13 +00:00
[ [ FIRAuth auth ] signInWithCredential : credential completion : ^ ( FIRUser * user , NSError * error ) {
2017-03-17 19:08:51 +00:00
if ( error ) {
2017-03-17 21:41:17 +00:00
[ self promiseRejectAuthException : reject error : error ] ;
2017-03-17 19:08:51 +00:00
} else {
[ self promiseWithUser : resolve rejecter : reject user : user ] ;
2017-03-17 18:04:13 +00:00
}
} ] ;
}
/ * *
sendPasswordResetEmail
2017-05-31 14:22:15 +00:00
2017-03-17 18:04:13 +00:00
@ param NSString email
@ param RCTPromiseResolveBlock resolve
@ param RCTPromiseRejectBlock reject
@ return
* /
RCT_EXPORT _METHOD ( sendPasswordResetEmail : ( NSString * ) email resolver : ( RCTPromiseResolveBlock ) resolve rejecter : ( RCTPromiseRejectBlock ) reject ) {
[ [ FIRAuth auth ] sendPasswordResetWithEmail : email completion : ^ ( NSError * _Nullable error ) {
if ( error ) {
2017-03-17 21:41:17 +00:00
[ self promiseRejectAuthException : reject error : error ] ;
2017-03-17 18:04:13 +00:00
} else {
[ self promiseNoUser : resolve rejecter : reject isError : NO ] ;
}
} ] ;
}
/ * *
getCurrentUser
2017-05-31 14:22:15 +00:00
2017-03-17 18:04:13 +00:00
@ param RCTPromiseResolveBlock resolve
@ param RCTPromiseRejectBlock reject
@ return
* /
RCT_EXPORT _METHOD ( getCurrentUser : ( RCTPromiseResolveBlock ) resolve rejecter : ( RCTPromiseRejectBlock ) reject ) {
FIRUser * user = [ FIRAuth auth ] . currentUser ;
2017-03-17 19:08:51 +00:00
[ self promiseWithUser : resolve rejecter : reject user : user ] ;
2017-03-17 18:04:13 +00:00
}
2017-03-14 19:04:16 +00:00
2017-03-17 19:08:51 +00:00
/ * *
signInWithCustomToken
2017-05-31 14:22:15 +00:00
2017-03-17 19:08:51 +00:00
@ param RCTPromiseResolveBlock resolve
@ param RCTPromiseRejectBlock reject
@ return
* /
RCT_EXPORT _METHOD ( signInWithCustomToken : ( NSString * ) customToken resolver : ( RCTPromiseResolveBlock ) resolve rejecter : ( RCTPromiseRejectBlock ) reject ) {
[ [ FIRAuth auth ] signInWithCustomToken : customToken completion : ^ ( FIRUser * user , NSError * error ) {
if ( error ) {
2017-03-17 21:41:17 +00:00
[ self promiseRejectAuthException : reject error : error ] ;
2017-03-09 15:26:28 +00:00
} else {
2017-03-17 19:08:51 +00:00
[ self promiseWithUser : resolve rejecter : reject user : user ] ;
2017-03-09 15:26:28 +00:00
}
} ] ;
}
2017-03-17 22:34:35 +00:00
/ * *
link - * insert zelda joke here *
2017-05-31 14:22:15 +00:00
2017-03-17 22:34:35 +00:00
@ param NSString provider
@ param NSString authToken
@ param NSString authSecret
@ param RCTPromiseResolveBlock resolve
@ param RCTPromiseRejectBlock reject
@ return
* /
RCT_EXPORT _METHOD ( link : ( NSString * ) provider authToken : ( NSString * ) authToken authSecret : ( NSString * ) authSecret resolver : ( RCTPromiseResolveBlock ) resolve rejecter : ( RCTPromiseRejectBlock ) reject ) {
FIRAuthCredential * credential = [ self getCredentialForProvider : provider token : authToken secret : authSecret ] ;
2017-05-31 14:22:15 +00:00
2017-03-17 22:34:35 +00:00
if ( credential = = nil ) {
return reject ( @ "auth/invalid-credential" , @ "The supplied auth credential is malformed, has expired or is not currently supported." , nil ) ;
}
2017-05-31 14:22:15 +00:00
2017-03-17 22:34:35 +00:00
FIRUser * user = [ FIRAuth auth ] . currentUser ;
2017-05-31 14:22:15 +00:00
2017-03-17 22:34:35 +00:00
if ( user ) {
2017-05-25 14:33:41 +00:00
[ user linkWithCredential : credential completion : ^ ( FIRUser * _Nullable _user , NSError * _Nullable error ) {
2017-03-17 22:34:35 +00:00
if ( error ) {
[ self promiseRejectAuthException : reject error : error ] ;
} else {
2017-05-25 14:33:41 +00:00
[ self promiseWithUser : resolve rejecter : reject user : _user ] ;
2017-03-17 22:34:35 +00:00
}
} ] ;
} else {
[ self promiseNoUser : resolve rejecter : reject isError : YES ] ;
}
}
2017-03-18 00:04:31 +00:00
/ * *
reauthenticate
2017-05-31 14:22:15 +00:00
2017-03-18 00:04:31 +00:00
@ param NSString provider
@ param NSString authToken
@ param NSString authSecret
@ param RCTPromiseResolveBlock resolve
@ param RCTPromiseRejectBlock reject
@ return
* /
RCT_EXPORT _METHOD ( reauthenticate : ( NSString * ) provider authToken : ( NSString * ) authToken authSecret : ( NSString * ) authSecret resolver : ( RCTPromiseResolveBlock ) resolve rejecter : ( RCTPromiseRejectBlock ) reject ) {
FIRAuthCredential * credential = [ self getCredentialForProvider : provider token : authToken secret : authSecret ] ;
2017-05-31 14:22:15 +00:00
2017-03-18 00:04:31 +00:00
if ( credential = = nil ) {
return reject ( @ "auth/invalid-credential" , @ "The supplied auth credential is malformed, has expired or is not currently supported." , nil ) ;
}
2017-05-31 14:22:15 +00:00
2017-03-18 00:04:31 +00:00
FIRUser * user = [ FIRAuth auth ] . currentUser ;
2017-05-31 14:22:15 +00:00
2017-03-18 00:04:31 +00:00
if ( user ) {
[ user reauthenticateWithCredential : credential completion : ^ ( NSError * _Nullable error ) {
if ( error ) {
[ self promiseRejectAuthException : reject error : error ] ;
} else {
FIRUser * userAfterAuth = [ FIRAuth auth ] . currentUser ;
[ self promiseWithUser : resolve rejecter : reject user : userAfterAuth ] ;
}
} ] ;
} else {
[ self promiseNoUser : resolve rejecter : reject isError : YES ] ;
}
}
2017-05-25 13:45:03 +00:00
/ * *
fetchProvidersForEmail
@ param NSString email
@ param RCTPromiseResolveBlock resolve
@ param RCTPromiseRejectBlock reject
@ return
* /
RCT_EXPORT _METHOD ( fetchProvidersForEmail : ( NSString * ) email resolver : ( RCTPromiseResolveBlock ) resolve rejecter : ( RCTPromiseRejectBlock ) reject ) {
[ [ FIRAuth auth ] fetchProvidersForEmail : email completion : ^ ( NSArray < NSString * > * _Nullable providers , NSError * _Nullable error ) {
if ( error ) {
[ self promiseRejectAuthException : reject error : error ] ;
} else if ( ! providers ) {
NSMutableArray * emptyResponse = [ [ NSMutableArray alloc ] init ] ;
resolve ( emptyResponse ) ;
} else {
resolve ( providers ) ;
}
} ] ;
}
2017-03-17 22:34:35 +00:00
/ * *
getCredentialForProvider
2017-05-31 14:22:15 +00:00
2017-03-17 22:34:35 +00:00
@ param provider
@ param authToken
@ param authTokenSecret
@ return FIRAuthCredential
* /
- ( FIRAuthCredential * ) getCredentialForProvider : ( NSString * ) provider token : ( NSString * ) authToken secret : ( NSString * ) authTokenSecret {
FIRAuthCredential * credential ;
2017-05-31 14:22:15 +00:00
2017-03-17 22:34:35 +00:00
if ( [ provider compare : @ "twitter" options : NSCaseInsensitiveSearch ] = = NSOrderedSame ) {
credential = [ FIRTwitterAuthProvider credentialWithToken : authToken secret : authTokenSecret ] ;
} else if ( [ provider compare : @ "facebook" options : NSCaseInsensitiveSearch ] = = NSOrderedSame ) {
credential = [ FIRFacebookAuthProvider credentialWithAccessToken : authToken ] ;
} else if ( [ provider compare : @ "google" options : NSCaseInsensitiveSearch ] = = NSOrderedSame ) {
credential = [ FIRGoogleAuthProvider credentialWithIDToken : authToken accessToken : authTokenSecret ] ;
} else if ( [ provider compare : @ "password" options : NSCaseInsensitiveSearch ] = = NSOrderedSame ) {
2017-06-13 05:21:45 +00:00
credential = [ FIREmailPasswordAuthProvider credentialWithEmail : authToken password : authTokenSecret ] ;
2017-03-17 22:34:35 +00:00
} else if ( [ provider compare : @ "github" options : NSCaseInsensitiveSearch ] = = NSOrderedSame ) {
credential = [ FIRGitHubAuthProvider credentialWithToken : authToken ] ;
} else {
NSLog ( @ "Provider not yet handled: %@" , provider ) ;
}
2017-05-31 14:22:15 +00:00
2017-03-17 22:34:35 +00:00
return credential ;
}
2017-03-14 19:04:16 +00:00
/ * *
Resolve or reject a promise based on isError value
2017-05-31 14:22:15 +00:00
2017-03-14 19:04:16 +00:00
@ param resolve RCTPromiseResolveBlock
@ param reject RCTPromiseRejectBlock
@ param isError BOOL
* /
- ( void ) promiseNoUser : ( RCTPromiseResolveBlock ) resolve rejecter : ( RCTPromiseRejectBlock ) reject isError : ( BOOL ) isError {
if ( isError ) {
2017-05-25 13:45:03 +00:00
reject ( @ "auth/no-current-user" , @ "No user currently signed in." , nil ) ;
2017-03-14 19:04:16 +00:00
} else {
resolve ( [ NSNull null ] ) ;
}
}
2017-03-17 19:08:51 +00:00
/ * *
Reject a promise with an auth exception
2017-05-31 14:22:15 +00:00
2017-03-17 19:08:51 +00:00
@ param reject RCTPromiseRejectBlock
@ param error NSError
* /
- ( void ) promiseRejectAuthException : ( RCTPromiseRejectBlock ) reject error : ( NSError * ) error {
2017-03-17 21:41:17 +00:00
NSString * code = @ "auth/unknown" ;
NSString * message = [ error localizedDescription ] ;
2017-05-31 14:22:15 +00:00
2017-03-17 21:41:17 +00:00
switch ( error . code ) {
case FIRAuthErrorCodeInvalidCustomToken :
code = @ "auth/invalid-custom-token" ;
message = @ "The custom token format is incorrect. Please check the documentation." ;
break ;
case FIRAuthErrorCodeCustomTokenMismatch :
code = @ "auth/custom-token-mismatch" ;
message = @ "The custom token corresponds to a different audience." ;
break ;
case FIRAuthErrorCodeInvalidCredential :
code = @ "auth/invalid-credential" ;
message = @ "The supplied auth credential is malformed or has expired." ;
break ;
case FIRAuthErrorCodeInvalidEmail :
code = @ "auth/invalid-email" ;
message = @ "The email address is badly formatted." ;
break ;
case FIRAuthErrorCodeWrongPassword :
code = @ "auth/wrong-password" ;
message = @ "The password is invalid or the user does not have a password." ;
break ;
case FIRAuthErrorCodeUserMismatch :
code = @ "auth/user-mismatch" ;
message = @ "The supplied credentials do not correspond to the previously signed in user." ;
break ;
case FIRAuthErrorCodeRequiresRecentLogin :
code = @ "auth/requires-recent-login" ;
message = @ "This operation is sensitive and requires recent authentication. Log in again before retrying this request." ;
break ;
case FIRAuthErrorCodeAccountExistsWithDifferentCredential :
code = @ "auth/account-exists-with-different-credential" ;
message = @ "An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address." ;
break ;
case FIRAuthErrorCodeEmailAlreadyInUse :
code = @ "auth/email-already-in-use" ;
message = @ "The email address is already in use by another account." ;
break ;
case FIRAuthErrorCodeCredentialAlreadyInUse :
code = @ "auth/credential-already-in-use" ;
message = @ "This credential is already associated with a different user account." ;
break ;
case FIRAuthErrorCodeUserDisabled :
code = @ "auth/user-disabled" ;
message = @ "The user account has been disabled by an administrator." ;
break ;
case FIRAuthErrorCodeUserTokenExpired :
code = @ "auth/user-token-expired" ;
message = @ "The user's credential is no longer valid. The user must sign in again." ;
break ;
case FIRAuthErrorCodeUserNotFound :
code = @ "auth/user-not-found" ;
message = @ "There is no user record corresponding to this identifier. The user may have been deleted." ;
break ;
case FIRAuthErrorCodeInvalidUserToken :
code = @ "auth/invalid-user-token" ;
message = @ "The user's credential is no longer valid. The user must sign in again." ;
break ;
case FIRAuthErrorCodeWeakPassword :
code = @ "auth/weak-password" ;
message = @ "The given password is invalid." ;
break ;
case FIRAuthErrorCodeOperationNotAllowed :
code = @ "auth/operation-not-allowed" ;
message = @ "This operation is not allowed. You must enable this service in the console." ;
break ;
case FIRAuthErrorCodeNetworkError :
code = @ "auth/network-error" ;
message = @ "A network error has occurred, please try again." ;
break ;
case FIRAuthErrorCodeInternalError :
code = @ "auth/internal-error" ;
message = @ "An internal error has occurred, please try again." ;
break ;
2017-05-31 14:22:15 +00:00
2017-03-17 22:00:13 +00:00
// unsure of the below codes so leaving them as the default error message
2017-03-17 21:41:17 +00:00
case FIRAuthErrorCodeTooManyRequests :
code = @ "auth/too-many-requests" ;
break ;
case FIRAuthErrorCodeProviderAlreadyLinked :
code = @ "auth/provider-already-linked" ;
break ;
case FIRAuthErrorCodeNoSuchProvider :
code = @ "auth/no-such-provider" ;
break ;
case FIRAuthErrorCodeInvalidAPIKey :
code = @ "auth/invalid-api-key" ;
break ;
case FIRAuthErrorCodeAppNotAuthorized :
code = @ "auth/app-not-authorised" ;
break ;
case FIRAuthErrorCodeExpiredActionCode :
code = @ "auth/expired-action-code" ;
break ;
case FIRAuthErrorCodeInvalidMessagePayload :
code = @ "auth/invalid-message-payload" ;
break ;
case FIRAuthErrorCodeInvalidSender :
code = @ "auth/invalid-sender" ;
break ;
case FIRAuthErrorCodeInvalidRecipientEmail :
code = @ "auth/invalid-recipient-email" ;
break ;
case FIRAuthErrorCodeKeychainError :
code = @ "auth/keychain-error" ;
break ;
default :
break ;
}
2017-05-31 14:22:15 +00:00
2017-03-17 21:41:17 +00:00
reject ( code , message , error ) ;
2017-03-17 19:08:51 +00:00
}
2017-03-14 19:04:16 +00:00
/ * *
Resolve or reject a promise based on FIRUser value existance
2017-05-31 14:22:15 +00:00
2017-03-14 19:04:16 +00:00
@ param resolve RCTPromiseResolveBlock
@ param reject RCTPromiseRejectBlock
@ param user FIRUser
* /
- ( void ) promiseWithUser : ( RCTPromiseResolveBlock ) resolve rejecter : ( RCTPromiseRejectBlock ) reject user : ( FIRUser * ) user {
if ( user ) {
NSDictionary * userDict = [ self firebaseUserToDict : user ] ;
resolve ( userDict ) ;
} else {
[ self promiseNoUser : resolve rejecter : reject isError : YES ] ;
}
2017-05-31 14:22:15 +00:00
2017-03-14 19:04:16 +00:00
}
2017-03-17 19:08:51 +00:00
/ * *
wrapper for sendEventWithName for auth events
2017-05-31 14:22:15 +00:00
2017-03-17 19:08:51 +00:00
@ param title sendEventWithName
@ param props < # props description # >
* /
- ( void ) sendJSEvent : ( NSString * ) title props : ( NSDictionary * ) props {
@ try {
if ( self -> listening ) {
[ self sendEventWithName : title body : props ] ;
}
}
@ catch ( NSException * error ) {
NSLog ( @ "An error occurred in sendJSEvent: %@" , [ error debugDescription ] ) ;
}
}
2017-05-12 12:58:16 +00:00
/ * *
2017-05-12 12:13:17 +00:00
Converts an array of FIRUserInfo instances into the correct format to match the web sdk
2017-05-31 14:22:15 +00:00
2017-05-12 12:13:17 +00:00
@ param providerData FIRUser . providerData
@ return NSArray
* /
- ( NSArray < NSObject * > * ) convertProviderData : ( NSArray < id < FIRUserInfo > > * ) providerData {
NSMutableArray * output = [ NSMutableArray array ] ;
2017-05-31 14:22:15 +00:00
2017-05-12 12:58:16 +00:00
for ( id < FIRUserInfo > userInfo in providerData ) {
2017-05-13 12:08:43 +00:00
NSMutableDictionary * pData = [ NSMutableDictionary dictionary ] ;
2017-05-31 14:22:15 +00:00
2017-05-13 00:46:27 +00:00
if ( userInfo . providerID ! = nil ) {
[ pData setValue : userInfo . providerID forKey : @ "providerId" ] ;
}
2017-05-31 14:22:15 +00:00
2017-05-13 00:46:27 +00:00
if ( userInfo . uid ! = nil ) {
[ pData setValue : userInfo . uid forKey : @ "uid" ] ;
}
2017-05-31 14:22:15 +00:00
2017-05-13 00:46:27 +00:00
if ( userInfo . displayName ! = nil ) {
[ pData setValue : userInfo . displayName forKey : @ "displayName" ] ;
}
2017-05-31 14:22:15 +00:00
2017-05-13 00:46:27 +00:00
if ( userInfo . photoURL ! = nil ) {
[ pData setValue : [ userInfo . photoURL absoluteString ] forKey : @ "photoURL" ] ;
}
2017-05-31 14:22:15 +00:00
2017-05-13 00:46:27 +00:00
if ( userInfo . email ! = nil ) {
[ pData setValue : userInfo . email forKey : @ "email" ] ;
}
2017-05-31 14:22:15 +00:00
2017-05-13 00:46:27 +00:00
[ output addObject : pData ] ;
2017-05-12 12:13:17 +00:00
}
2017-05-31 14:22:15 +00:00
2017-05-12 12:13:17 +00:00
return output ;
}
2017-03-14 19:04:16 +00:00
/ * *
Converts a FIRUser instance into a dictionary to send via RNBridge
2017-05-31 14:22:15 +00:00
2017-03-14 19:04:16 +00:00
@ param user FIRUser
@ return NSDictionary
* /
- ( NSDictionary * ) firebaseUserToDict : ( FIRUser * ) user {
NSMutableDictionary * userDict = [
2017-06-13 05:21:45 +00:00
@ { @ "uid" : user . uid ,
@ "email" : user . email ? user . email : [ NSNull null ] ,
@ "emailVerified" : @ ( user . emailVerified ) ,
@ "isAnonymous" : @ ( user . anonymous ) ,
@ "displayName" : user . displayName ? user . displayName : [ NSNull null ] ,
@ "refreshToken" : user . refreshToken ,
@ "providerId" : [ user . providerID lowercaseString ] ,
@ "providerData" : [ self convertProviderData : user . providerData ]
} mutableCopy
] ;
2017-05-31 14:22:15 +00:00
2017-03-14 19:04:16 +00:00
if ( [ user valueForKey : @ "photoURL" ] ! = nil ) {
2017-05-12 14:15:02 +00:00
[ userDict setValue : [ user . photoURL absoluteString ] forKey : @ "photoURL" ] ;
2017-03-14 19:04:16 +00:00
}
2017-05-31 14:22:15 +00:00
2017-03-14 19:04:16 +00:00
return userDict ;
}
2017-03-09 15:26:28 +00:00
2017-03-17 19:08:51 +00:00
- ( NSArray < NSString * > * ) supportedEvents {
return @ [ AUTH_CHANGED _EVENT , AUTH_ANONYMOUS _ERROR _EVENT , AUTH_ERROR _EVENT ] ;
}
2017-03-09 15:26:28 +00:00
@ end
2017-05-25 14:33:41 +00:00
# else
@ implementation RNFirebaseAuth
@ end
# endif