Skip to content

Commit

Permalink
Merge pull request sap-labs-france#600 from sap-labs-france/captcha
Browse files Browse the repository at this point in the history
Fixed captcha validity for SignUp and Retrieve Password
  • Loading branch information
LucasBrazi06 authored Jan 14, 2022
2 parents 900b97c + c62aaf1 commit f39ebaf
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 28 deletions.
3 changes: 3 additions & 0 deletions src/FormStyles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export default function computeStyleSheet(): StyleSheet.NamedStyles<any> {
marginBottom: '10@s',
backgroundColor: commonColor.buttonBg
},
buttonDisabled: {
opacity: 0.4
},
buttonText: {
width: '100%',
textAlign: 'center',
Expand Down
9 changes: 8 additions & 1 deletion src/config/Configuration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { EndpointCloud } from '../types/Tenant';

export default class Configuration {
public static readonly AWS_REST_ENDPOINT_PROD = 'https://rest.e-mobility-group.com';
public static readonly AWS_REST_ENDPOINT_QA = 'https://qa-e-mobility-group.com';


public static readonly SCP_CAPTCHA_BASE_URL = 'https://evse.cfapps.eu10.hana.ondemand.com';
public static readonly SCP_CAPTCHA_SITE_KEY = '6Lcmr6EUAAAAAIyn3LasUzk-0MpH2R1COXFYsxNw';

public static readonly DEFAULT_ENDPOINT_CLOUD_ID = 'aws';
Expand All @@ -23,7 +24,13 @@ export default class Configuration {
id: '10.0.2.2:8080',
name: 'android-local:8080',
endpoint: 'http://10.0.2.2:8080'
},
{
id: 'kubernetes',
name: 'QA',
endpoint: Configuration.AWS_REST_ENDPOINT_QA
}

];

public static readonly DEVELOPMENT_ENDPOINT_CLOUDS: EndpointCloud[] = [
Expand Down
2 changes: 1 addition & 1 deletion src/provider/CentralServerProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import SecurityProvider from './SecurityProvider';
export default class CentralServerProvider {
private axiosInstance: AxiosInstance;
private debug = false;
private captchaBaseUrl: string = Configuration.SCP_CAPTCHA_BASE_URL;
private captchaBaseUrl: string = Configuration.AWS_REST_ENDPOINT_PROD;
private captchaSiteKey: string = Configuration.SCP_CAPTCHA_SITE_KEY;

// Paste the token below
Expand Down
28 changes: 15 additions & 13 deletions src/screens/auth/retrieve-password/RetrievePassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ interface State {
captchaSiteKey?: string;
captchaBaseUrl?: string;
captcha?: string;
performRetrievePassword?: boolean;
loading?: boolean;
errorEmail?: Record<string, unknown>[];
}
Expand Down Expand Up @@ -51,7 +52,8 @@ export default class RetrievePassword extends BaseScreen<Props, State> {
captchaSiteKey: null,
captchaBaseUrl: null,
captcha: null,
loading: false
loading: false,
performRetrievePassword: false
};
}

Expand All @@ -76,21 +78,21 @@ export default class RetrievePassword extends BaseScreen<Props, State> {
this.centralServerProvider.setAutoLoginDisabled(true);
}

public recaptchaResponseToken = (captcha: string) => {
this.setState({ captcha });
public onCaptchaCreated = (captcha: string) => {
this.setState({ captcha }, this.state.performRetrievePassword ? () => this.retrievePassword() : () => {});
};

public retrievePassword = async () => {
// Check field
const formIsValid = Utils.validateInput(this, this.formValidationDef);
if (formIsValid) {
const { tenantSubDomain, email, captcha } = this.state;
const { tenantSubDomain, email, captcha } = this.state;
const formIsValid = Utils.validateInput(this, this.formValidationDef)
// Force captcha regeneration for next signUp click
if (formIsValid && captcha) {
try {
this.setState({ loading: true });
// Login
await this.centralServerProvider.retrievePassword(tenantSubDomain, email, captcha);
// Login Success
this.setState({ loading: false });
this.setState({ loading: false, performRetrievePassword: false });
// Show
Message.showSuccess(I18n.t('authentication.resetSuccess'));
// Navigate
Expand All @@ -110,7 +112,7 @@ export default class RetrievePassword extends BaseScreen<Props, State> {
);
} catch (error) {
// Login failed
this.setState({ loading: false });
this.setState({ loading: false, performRetrievePassword: false });
// Check request?
if (error.request) {
// Show error
Expand Down Expand Up @@ -169,21 +171,21 @@ export default class RetrievePassword extends BaseScreen<Props, State> {
{errorMessage}
</Text>
))}
{loading || !captcha ? (
{loading ? (
<Spinner style={formStyle.spinner} color="grey" />
) : (
<Button primary block style={formStyle.button} onPress={async () => this.retrievePassword()}>
<Button primary block style={formStyle.button} onPress={async () => this.setState({loading: true, performRetrievePassword: true, captcha: null})}>
<Text style={formStyle.buttonText} uppercase={false}>
{I18n.t('authentication.retrievePassword')}
</Text>
</Button>
)}
</Form>
</KeyboardAvoidingView>
{captchaSiteKey && captchaBaseUrl && (
{captchaSiteKey && captchaBaseUrl && !captcha && (
<ReactNativeRecaptchaV3
action="ResetPassword"
onHandleToken={this.recaptchaResponseToken}
onHandleToken={(captcha) => this.onCaptchaCreated(captcha)}
url={captchaBaseUrl}
siteKey={captchaSiteKey}
/>
Expand Down
29 changes: 16 additions & 13 deletions src/screens/auth/sign-up/SignUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ interface State {
captcha?: string;
loading?: boolean;
hideRepeatPassword?: boolean;
performSignUp?: boolean;
hidePassword?: boolean;
errorEula?: Record<string, unknown>[];
errorPassword?: Record<string, unknown>[];
Expand Down Expand Up @@ -121,7 +122,8 @@ export default class SignUp extends BaseScreen<Props, State> {
captcha: null,
loading: false,
hidePassword: true,
hideRepeatPassword: true
hideRepeatPassword: true,
performSignUp: false
};
}

Expand All @@ -146,18 +148,18 @@ export default class SignUp extends BaseScreen<Props, State> {
this.centralServerProvider.setAutoLoginDisabled(true);
}

public recaptchaResponseToken = (captcha: string) => {
this.setState({ captcha });
public onCaptchaCreated = (captcha: string) => {
this.setState({ captcha }, this.state.performSignUp ? () => this.signUp() : () => {});
};

public signUp = async () => {
// Check field
const { tenantSubDomain, name, firstName, email, password, eula, captcha } = this.state
const formIsValid = Utils.validateInput(this, this.formValidationDef);
if (formIsValid) {
const { tenantSubDomain, name, firstName, email, password, eula, captcha } = this.state;
// Force captcha regeneration for next signUp click
if (formIsValid && captcha && eula) {
try {
// Loading
this.setState({ loading: true });
// Register
await this.centralServerProvider.register(
tenantSubDomain,
Expand All @@ -170,7 +172,7 @@ export default class SignUp extends BaseScreen<Props, State> {
captcha
);
// Reset
this.setState({ loading: false });
this.setState({ loading: false, performSignUp: false });
// Show
Message.showSuccess(I18n.t('authentication.registerSuccess'));
// Navigate
Expand All @@ -190,7 +192,7 @@ export default class SignUp extends BaseScreen<Props, State> {
);
} catch (error) {
// Reset
this.setState({ loading: false });
this.setState({ loading: false, performSignUp: false });
// Check request?
if (error.request) {
// Show error
Expand All @@ -212,6 +214,7 @@ export default class SignUp extends BaseScreen<Props, State> {
}
}
}
this.setState({loading: false, performSignUp: false})
};

public onBack(): boolean {
Expand Down Expand Up @@ -370,7 +373,7 @@ export default class SignUp extends BaseScreen<Props, State> {
</Text>
))}
<View style={formStyle.formCheckboxContainer}>
<CheckBox style={formStyle.checkbox} checked={eula} onPress={() => this.setState({ eula: !eula, captcha: null })} />
<CheckBox style={formStyle.checkbox} checked={eula} onPress={() => this.setState({ eula: !eula})} />
<Text style={formStyle.checkboxText}>
{I18n.t('authentication.acceptEula')}
<Text onPress={() => navigation.navigate('Eula')} style={style.eulaLink}>
Expand All @@ -384,21 +387,21 @@ export default class SignUp extends BaseScreen<Props, State> {
{errorMessage}
</Text>
))}
{loading || (!captcha && this.state.eula) ? (
{loading ? (
<Spinner style={formStyle.spinner} color="grey" />
) : (
<Button primary block style={formStyle.button} onPress={async () => this.signUp()}>
<Button disabled={!eula} primary block style={[formStyle.button, !eula && formStyle.buttonDisabled]} onPress={async () => this.setState({captcha: null, performSignUp: true, loading: true })}>
<Text style={formStyle.buttonText} uppercase={false}>
{I18n.t('authentication.signUp')}
</Text>
</Button>
)}
</Form>
</KeyboardAvoidingView>
{this.state.eula && captchaSiteKey && captchaBaseUrl && (
{!captcha && captchaSiteKey && captchaBaseUrl && (
<ReactNativeRecaptchaV3
action="RegisterUser"
onHandleToken={this.recaptchaResponseToken}
onHandleToken={(captcha) => this.onCaptchaCreated(captcha)}
url={captchaBaseUrl}
siteKey={captchaSiteKey}
/>
Expand Down

0 comments on commit f39ebaf

Please sign in to comment.