[android, ios] currentUser.getToken() - now uses promise and correctly returns a string
This commit is contained in:
parent
b6c406c7aa
commit
9f5b132f38
|
@ -407,6 +407,37 @@ public class RNFirebaseAuth extends ReactContextBaseJavaModule {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* getToken
|
||||
*
|
||||
* @param promise
|
||||
*/
|
||||
@ReactMethod
|
||||
public void getToken(final Promise promise) {
|
||||
FirebaseUser user = mAuth.getCurrentUser();
|
||||
Log.d(TAG, "getToken");
|
||||
|
||||
if (user != null) {
|
||||
user.getToken(true)
|
||||
.addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
|
||||
@Override
|
||||
public void onComplete(@NonNull Task<GetTokenResult> task) {
|
||||
if (task.isSuccessful()) {
|
||||
Log.d(TAG, "getToken:onComplete:success");
|
||||
promise.resolve(task.getResult().getToken());
|
||||
} else {
|
||||
Exception exception = task.getException();
|
||||
WritableMap error = authExceptionToMap(exception);
|
||||
Log.e(TAG, "getToken:onComplete:failure", exception);
|
||||
promise.reject(error.getString("code"), error.getString("message"), exception);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
promiseNoUser(promise, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ----------------------- CLEAN ME -----------------------------------------------------
|
||||
// ----------------------- CLEAN ME -----------------------------------------------------
|
||||
|
@ -510,39 +541,6 @@ public class RNFirebaseAuth extends ReactContextBaseJavaModule {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
@ReactMethod
|
||||
public void getToken(final Callback callback) {
|
||||
FirebaseUser user = mAuth.getCurrentUser();
|
||||
|
||||
if (user != null) {
|
||||
user.getToken(true)
|
||||
.addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
|
||||
@Override
|
||||
public void onComplete(@NonNull Task<GetTokenResult> task) {
|
||||
try {
|
||||
if (task.isSuccessful()) {
|
||||
String token = task.getResult().getToken();
|
||||
WritableMap resp = Arguments.createMap();
|
||||
resp.putString("status", "complete");
|
||||
resp.putString("token", token);
|
||||
callback.invoke(null, resp);
|
||||
} else {
|
||||
WritableMap err = Arguments.createMap();
|
||||
err.putInt("errorCode", ERROR_FETCHING_TOKEN);
|
||||
err.putString("errorMessage", task.getException().getMessage());
|
||||
callback.invoke(err);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
userExceptionCallback(ex, callback);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
callbackNoUser(callback, true);
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void updateUserProfile(ReadableMap props, final Callback callback) {
|
||||
FirebaseUser user = mAuth.getCurrentUser();
|
||||
|
|
|
@ -114,6 +114,29 @@ RCT_EXPORT_METHOD(delete:(RCTPromiseResolveBlock) resolve rejecter:(RCTPromiseRe
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
getToken
|
||||
|
||||
@param RCTPromiseResolveBlock resolve
|
||||
@param RCTPromiseRejectBlock reject
|
||||
@return
|
||||
*/
|
||||
RCT_EXPORT_METHOD(getToken:(RCTPromiseResolveBlock) resolve rejecter:(RCTPromiseRejectBlock) reject) {
|
||||
FIRUser *user = [FIRAuth auth].currentUser;
|
||||
|
||||
if (user) {
|
||||
[user getTokenWithCompletion:^(NSString *token, NSError *_Nullable error) {
|
||||
if (error) {
|
||||
// TODO authExceptionToDict
|
||||
reject(@"auth/unknown", @"An unknown error has occurred.", error);
|
||||
} else {
|
||||
resolve(token);
|
||||
}
|
||||
}];
|
||||
} else {
|
||||
[self promiseNoUser:resolve rejecter:reject isError:YES];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// TODO ------------------------------------------------------- CLEAN UP --------------------------
|
||||
|
@ -304,23 +327,6 @@ RCT_EXPORT_METHOD(sendPasswordResetWithEmail:(NSString *)email
|
|||
}];
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(getToken:(RCTResponseSenderBlock) callback)
|
||||
{
|
||||
FIRUser *user = [FIRAuth auth].currentUser;
|
||||
|
||||
if (user) {
|
||||
[user getTokenWithCompletion:^(NSString *token, NSError *_Nullable error) {
|
||||
if (error) {
|
||||
[self userErrorCallback:callback error:error user:user msg:@"getTokenError"];
|
||||
} else {
|
||||
callback(@[[NSNull null], token]);
|
||||
}
|
||||
}];
|
||||
} else {
|
||||
[self noUserCallback:callback isError:true];
|
||||
}
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(getTokenWithCompletion:(RCTResponseSenderBlock) callback)
|
||||
{
|
||||
FIRUser *user = [FIRAuth auth].currentUser;
|
||||
|
|
|
@ -127,6 +127,14 @@ export default class User {
|
|||
return this._auth._interceptUserValue(FirebaseAuth.reload());
|
||||
}
|
||||
|
||||
/**
|
||||
* get the token of current user
|
||||
* @return {Promise}
|
||||
*/
|
||||
getToken(): Promise<Object> {
|
||||
return FirebaseAuth.getToken();
|
||||
}
|
||||
|
||||
// TODO no RN android method yet, the SDK does have .getProviderData but returns as a List.
|
||||
// get providerData() {
|
||||
// return this._valueOrNull('providerData');
|
||||
|
@ -140,14 +148,6 @@ export default class User {
|
|||
return promisify('reauthenticate', FirebaseAuth, 'auth/')(credential.provider, credential.token, credential.secret);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the token of current user
|
||||
* @return {Promise}
|
||||
*/
|
||||
getToken(): Promise<Object> {
|
||||
return promisify('getToken', FirebaseAuth, 'auth/')();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the current user's email
|
||||
* @param {string} email The user's _new_ email
|
||||
|
|
Loading…
Reference in New Issue