2
0
mirror of synced 2025-01-13 07:35:06 +00:00

[auth] Add support for state in email actions

This commit is contained in:
Chris Bianca 2017-12-07 12:36:51 +00:00
parent b099d13730
commit a03c445e0c
5 changed files with 135 additions and 52 deletions
android/src/main/java/io/invertase/firebase/auth
ios/RNFirebase/auth
lib
modules/auth
types

@ -30,6 +30,7 @@ import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseException;
import com.google.firebase.auth.ActionCodeResult;
import com.google.firebase.auth.ActionCodeSettings;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException;
@ -321,25 +322,32 @@ class RNFirebaseAuth extends ReactContextBaseJavaModule {
* @param promise
*/
@ReactMethod
public void sendPasswordResetEmail(String appName, final String email, final Promise promise) {
public void sendPasswordResetEmail(String appName, final String email,
ReadableMap actionCodeSettings, final Promise promise) {
Log.d(TAG, "sendPasswordResetEmail");
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp);
firebaseAuth.sendPasswordResetEmail(email)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "sendPasswordResetEmail:onComplete:success");
promiseNoUser(promise, false);
} else {
Exception exception = task.getException();
Log.e(TAG, "sendPasswordResetEmail:onComplete:failure", exception);
promiseRejectAuthException(promise, exception);
}
OnCompleteListener<Void> listener = new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "sendPasswordResetEmail:onComplete:success");
promiseNoUser(promise, false);
} else {
Exception exception = task.getException();
Log.e(TAG, "sendPasswordResetEmail:onComplete:failure", exception);
promiseRejectAuthException(promise, exception);
}
});
}
};
if (actionCodeSettings == null) {
firebaseAuth.sendPasswordResetEmail(email).addOnCompleteListener(listener);
} else {
ActionCodeSettings settings = buildActionCodeSettings(actionCodeSettings);
firebaseAuth.sendPasswordResetEmail(email, settings).addOnCompleteListener(listener);
}
}
/**
@ -439,7 +447,7 @@ class RNFirebaseAuth extends ReactContextBaseJavaModule {
* @param promise
*/
@ReactMethod
public void sendEmailVerification(String appName, final Promise promise) {
public void sendEmailVerification(String appName, ReadableMap actionCodeSettings, final Promise promise) {
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
final FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp);
@ -450,20 +458,26 @@ class RNFirebaseAuth extends ReactContextBaseJavaModule {
promiseNoUser(promise, false);
Log.e(TAG, "sendEmailVerification:failure:noCurrentUser");
} else {
user.sendEmailVerification()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "sendEmailVerification:onComplete:success");
promiseWithUser(firebaseAuth.getCurrentUser(), promise);
} else {
Exception exception = task.getException();
Log.e(TAG, "sendEmailVerification:onComplete:failure", exception);
promiseRejectAuthException(promise, exception);
}
OnCompleteListener<Void> listener = new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "sendEmailVerification:onComplete:success");
promiseWithUser(firebaseAuth.getCurrentUser(), promise);
} else {
Exception exception = task.getException();
Log.e(TAG, "sendEmailVerification:onComplete:failure", exception);
promiseRejectAuthException(promise, exception);
}
});
}
};
if (actionCodeSettings == null) {
user.sendEmailVerification().addOnCompleteListener(listener);
} else {
ActionCodeSettings settings = buildActionCodeSettings(actionCodeSettings);
user.sendEmailVerification(settings).addOnCompleteListener(listener);
}
}
}
@ -1399,6 +1413,30 @@ class RNFirebaseAuth extends ReactContextBaseJavaModule {
return userMap;
}
private ActionCodeSettings buildActionCodeSettings(ReadableMap actionCodeSettings) {
ActionCodeSettings.Builder builder = ActionCodeSettings.newBuilder();
ReadableMap android = actionCodeSettings.getMap("android");
ReadableMap ios = actionCodeSettings.getMap("iOS");
String url = actionCodeSettings.getString("url");
if (android != null) {
boolean installApp = android.hasKey("installApp") ? android.getBoolean("installApp") : false;
String minimumVersion = android.hasKey("minimumVersion") ? android.getString("minimumVersion") : null;
String packageName = android.getString("packageName");
builder = builder.setAndroidPackageName(packageName, installApp, minimumVersion);
}
if (actionCodeSettings.hasKey("handleCodeInApp")) {
builder = builder.setHandleCodeInApp(actionCodeSettings.getBoolean("handleCodeInApp"));
}
if (ios != null && ios.hasKey("bundleId")) {
builder = builder.setIOSBundleId(ios.getString("bundleId"));
}
if (url != null) {
builder = builder.setUrl(url);
}
return builder.build();
}
/**
* @param appName
* @param requestKey

@ -262,24 +262,28 @@ RCT_EXPORT_METHOD(reload:
@param RCTPromiseRejectBlock reject
@return return
*/
RCT_EXPORT_METHOD(sendEmailVerification:
(NSString *) appName
resolver:
(RCTPromiseResolveBlock) resolve
rejecter:
(RCTPromiseRejectBlock) reject) {
RCT_EXPORT_METHOD(sendEmailVerification:(NSString *) appName
actionCodeSettings:(NSDictionary *) actionCodeSettings
resolver:(RCTPromiseResolveBlock) resolve
rejecter:(RCTPromiseRejectBlock) reject) {
FIRApp *firApp = [FIRApp appNamed:appName];
FIRUser *user = [FIRAuth authWithApp:firApp].currentUser;
if (user) {
[user sendEmailVerificationWithCompletion:^(NSError *_Nullable error) {
id handler = ^(NSError *_Nullable error) {
if (error) {
[self promiseRejectAuthException:reject error:error];
} else {
FIRUser *userAfterUpdate = [FIRAuth authWithApp:firApp].currentUser;
[self promiseWithUser:resolve rejecter:reject user:userAfterUpdate];
}
}];
};
if (actionCodeSettings) {
FIRActionCodeSettings *settings = [self buildActionCodeSettings:actionCodeSettings];
[user sendEmailVerificationWithActionCodeSettings:settings completion:handler];
} else {
[user sendEmailVerificationWithCompletion:handler];
}
} else {
[self promiseNoUser:resolve rejecter:reject isError:YES];
}
@ -582,23 +586,27 @@ RCT_EXPORT_METHOD(checkActionCode:
@param RCTPromiseRejectBlock reject
@return
*/
RCT_EXPORT_METHOD(sendPasswordResetEmail:
(NSString *) appName
email:
(NSString *) email
resolver:
(RCTPromiseResolveBlock) resolve
rejecter:
(RCTPromiseRejectBlock) reject) {
RCT_EXPORT_METHOD(sendPasswordResetEmail:(NSString *) appName
email:(NSString *) email
actionCodeSettings:(NSDictionary *) actionCodeSettings
resolver:(RCTPromiseResolveBlock) resolve
rejecter:(RCTPromiseRejectBlock) reject) {
FIRApp *firApp = [FIRApp appNamed:appName];
[[FIRAuth authWithApp:firApp] sendPasswordResetWithEmail:email completion:^(NSError *_Nullable error) {
id handler = ^(NSError *_Nullable error) {
if (error) {
[self promiseRejectAuthException:reject error:error];
} else {
[self promiseNoUser:resolve rejecter:reject isError:NO];
}
}];
};
if (actionCodeSettings) {
FIRActionCodeSettings *settings = [self buildActionCodeSettings:actionCodeSettings];
[[FIRAuth authWithApp:firApp] sendPasswordResetWithEmail:email actionCodeSettings:settings completion:handler];
} else {
[[FIRAuth authWithApp:firApp] sendPasswordResetWithEmail:email completion:handler];
}
}
/**
@ -1154,6 +1162,30 @@ RCT_EXPORT_METHOD(fetchProvidersForEmail:
return userDict;
}
- (FIRActionCodeSettings *)buildActionCodeSettings:(NSDictionary *)actionCodeSettings {
FIRActionCodeSettings *settings = [[FIRActionCodeSettings alloc] init];
NSDictionary *android = actionCodeSettings[@"android"];
BOOL handleCodeInApp = actionCodeSettings[@"handleCodeInApp"];
NSDictionary *ios = actionCodeSettings[@"iOS"];
NSString *url = actionCodeSettings[@"url"];
if (android) {
BOOL installApp = android[@"installApp"];
NSString *minimumVersion = android[@"minimumVersion"];
NSString *packageName = android[@"packageName"];
[settings setAndroidPackageName:packageName installIfNotAvailable:installApp minimumVersion:minimumVersion];
}
if (handleCodeInApp) {
[settings setHandleCodeInApp:handleCodeInApp];
}
if (ios && ios[@"bundleId"]) {
[settings setIOSBundleID:ios[@"bundleId"]];
}
if (url) {
[settings setURL:[NSURL URLWithString:url]];
}
return settings;
}
- (NSArray<NSString *> *)supportedEvents {
return @[AUTH_CHANGED_EVENT, AUTH_ID_TOKEN_CHANGED_EVENT, PHONE_AUTH_STATE_CHANGED_EVENT];
}

@ -5,7 +5,7 @@
import INTERNALS from '../../utils/internals';
import type Auth from './';
import type { AuthCredential } from '../../types';
import type { ActionCodeSettings, AuthCredential } from '../../types';
type NativeUser = {
displayName?: string,
@ -133,9 +133,9 @@ export default class User {
/**
* Send verification email to current user.
*/
sendEmailVerification(): Promise<void> {
sendEmailVerification(actionCodeSettings?: ActionCodeSettings): Promise<void> {
return this._auth
._interceptUndefinedUserValue(this._auth._native.sendEmailVerification());
._interceptUndefinedUserValue(this._auth._native.sendEmailVerification(actionCodeSettings));
}
toJSON(): Object {

@ -17,7 +17,7 @@ import FacebookAuthProvider from './providers/FacebookAuthProvider';
import PhoneAuthListener from './PhoneAuthListener';
import type { AuthCredential } from '../../types';
import type { ActionCodeSettings, AuthCredential } from '../../types';
import type FirebaseApp from '../core/firebase-app';
type AuthResult = {
@ -268,8 +268,8 @@ export default class Auth extends ModuleBase {
* Send reset password instructions via email
* @param {string} email The email to send password reset instructions
*/
sendPasswordResetEmail(email: string): Promise<void> {
return this._native.sendPasswordResetEmail(email);
sendPasswordResetEmail(email: string, actionCodeSettings?: ActionCodeSettings): Promise<void> {
return this._native.sendPasswordResetEmail(email, actionCodeSettings);
}
/**

@ -94,6 +94,19 @@ export type AuthModule = {
nativeModuleExists: boolean,
} & AuthStatics;
export type ActionCodeSettings = {
android: {
installApp?: boolean,
minimumVersion?: string,
packageName: string,
},
handleCodeInApp?: boolean,
iOS: {
bundleId?: string,
},
url: string,
}
/* Crash types */
export type CrashModule = {