PromucFlow_constructor/app/client/src/pages/UserAuth/Login.tsx

195 lines
6.2 KiB
TypeScript
Raw Normal View History

2019-12-16 08:49:10 +00:00
import React from "react";
import { Link, useLocation } from "react-router-dom";
import { connect } from "react-redux";
import { InjectedFormProps, reduxForm, formValueSelector } from "redux-form";
import {
LOGIN_FORM_NAME,
LOGIN_FORM_EMAIL_FIELD_NAME,
LOGIN_FORM_PASSWORD_FIELD_NAME,
} from "constants/forms";
2019-12-16 08:49:10 +00:00
import { FORGOT_PASSWORD_URL, SIGN_UP_URL } from "constants/routes";
import {
LOGIN_PAGE_SUBTITLE,
LOGIN_PAGE_TITLE,
LOGIN_PAGE_EMAIL_INPUT_LABEL,
LOGIN_PAGE_PASSWORD_INPUT_LABEL,
LOGIN_PAGE_PASSWORD_INPUT_PLACEHOLDER,
LOGIN_PAGE_EMAIL_INPUT_PLACEHOLDER,
FORM_VALIDATION_EMPTY_EMAIL,
FORM_VALIDATION_EMPTY_PASSWORD,
FORM_VALIDATION_INVALID_EMAIL,
FORM_VALIDATION_INVALID_PASSWORD,
LOGIN_PAGE_LOGIN_BUTTON_TEXT,
LOGIN_PAGE_FORGOT_PASSWORD_TEXT,
LOGIN_PAGE_SIGN_UP_LINK_TEXT,
LOGIN_PAGE_INVALID_CREDS_ERROR,
LOGIN_PAGE_INVALID_CREDS_FORGOT_PASSWORD_LINK,
2020-02-25 11:33:07 +00:00
FORM_VALIDATION_PASSWORD_RULE,
2019-12-16 08:49:10 +00:00
} from "constants/messages";
import Divider from "components/editorComponents/Divider";
import FormMessage from "components/editorComponents/form/FormMessage";
import FormGroup from "components/editorComponents/form/FormGroup";
2020-02-25 11:33:07 +00:00
import FormTextField from "components/editorComponents/form/FormTextField";
2020-02-24 09:35:11 +00:00
import Button from "components/editorComponents/Button";
2019-12-16 08:49:10 +00:00
import ThirdPartyAuth, { SocialLoginTypes } from "./ThirdPartyAuth";
import { isEmail, isStrongPassword, isEmptyString } from "utils/formhelpers";
import { LoginFormValues } from "./helpers";
import {
AuthCardContainer,
SpacedSubmitForm,
FormActions,
AuthCardHeader,
AuthCardFooter,
AuthCardNavLink,
AuthCardBody,
} from "./StyledComponents";
2020-03-11 13:59:46 +00:00
import AnalyticsUtil from "utils/AnalyticsUtil";
Use injected configuration from Nginx at runtime instead of build time (#30) * Use envsubst and nginx templates to generate nginx configs which can substitute environment variables and inject into the index.html file * Fix path in dockerfile. Add .gitignore and .env.example files. Fix nginx-linux template. * Add all environment variables. Add prefix to all environment variables. Update scripts to attempt to substitute all environment variables with the prefix * Setup dockerfile to execute a bash script. use env.example for fetching environment variables in development * Toggle features based on injected configs. Fix nginx template substitution script. * Update env.example file * Remove debug code from start-nginx.sh * Fix nginx config templates by adding quotes by default. Fix sed regex to include numerals. Toggle social login buttons on Login page based on the config. * Update rapid api environment variable name. Toggle oauth buttons based on config in SignUp page. Update .env.example to be a union of server and client environment variables * Adding a Map disabled message on Map widget * Adding links to Privacy policy and TNC * Use REACT_APP_ env variables with higher priority over injected config variables for toggling features * Update netlify.toml by commenting out the build environment variables * Remove env variables not required by the client * Remove start-storybook entry from package.json * Fix netlify.toml. Fallback algolia configs * Add contexts to netlify.toml for successful deploys. Swith to using APPSMITH_MARKETPLACE_URL as the toggle for RapidAPI feature on the client. Remove comments in nginx config templates. Fix template used in dockerfile. Co-authored-by: Satbir Singh <apple@apples-MacBook-Pro.local> Co-authored-by: Satbir Singh <satbir121@gmail.com>
2020-07-07 10:22:17 +00:00
import { getAppsmithConfigs } from "configs";
import { TncPPLinks } from "./SignUp";
import { LOGIN_SUBMIT_PATH } from "constants/ApiConstants";
import PerformanceTracker, {
PerformanceTransactionName,
} from "utils/PerformanceTracker";
Use injected configuration from Nginx at runtime instead of build time (#30) * Use envsubst and nginx templates to generate nginx configs which can substitute environment variables and inject into the index.html file * Fix path in dockerfile. Add .gitignore and .env.example files. Fix nginx-linux template. * Add all environment variables. Add prefix to all environment variables. Update scripts to attempt to substitute all environment variables with the prefix * Setup dockerfile to execute a bash script. use env.example for fetching environment variables in development * Toggle features based on injected configs. Fix nginx template substitution script. * Update env.example file * Remove debug code from start-nginx.sh * Fix nginx config templates by adding quotes by default. Fix sed regex to include numerals. Toggle social login buttons on Login page based on the config. * Update rapid api environment variable name. Toggle oauth buttons based on config in SignUp page. Update .env.example to be a union of server and client environment variables * Adding a Map disabled message on Map widget * Adding links to Privacy policy and TNC * Use REACT_APP_ env variables with higher priority over injected config variables for toggling features * Update netlify.toml by commenting out the build environment variables * Remove env variables not required by the client * Remove start-storybook entry from package.json * Fix netlify.toml. Fallback algolia configs * Add contexts to netlify.toml for successful deploys. Swith to using APPSMITH_MARKETPLACE_URL as the toggle for RapidAPI feature on the client. Remove comments in nginx config templates. Fix template used in dockerfile. Co-authored-by: Satbir Singh <apple@apples-MacBook-Pro.local> Co-authored-by: Satbir Singh <satbir121@gmail.com>
2020-07-07 10:22:17 +00:00
const { enableGithubOAuth, enableGoogleOAuth } = getAppsmithConfigs();
2019-12-16 08:49:10 +00:00
const validate = (values: LoginFormValues) => {
const errors: LoginFormValues = {};
const email = values[LOGIN_FORM_EMAIL_FIELD_NAME];
const password = values[LOGIN_FORM_PASSWORD_FIELD_NAME];
if (!password || isEmptyString(password)) {
errors[LOGIN_FORM_PASSWORD_FIELD_NAME] = FORM_VALIDATION_EMPTY_PASSWORD;
} else if (!isStrongPassword(password)) {
errors[LOGIN_FORM_PASSWORD_FIELD_NAME] = FORM_VALIDATION_INVALID_PASSWORD;
2019-12-16 08:49:10 +00:00
}
if (!email || isEmptyString(email)) {
errors[LOGIN_FORM_EMAIL_FIELD_NAME] = FORM_VALIDATION_EMPTY_EMAIL;
} else if (!isEmail(email)) {
errors[LOGIN_FORM_EMAIL_FIELD_NAME] = FORM_VALIDATION_INVALID_EMAIL;
2019-12-16 08:49:10 +00:00
}
2020-02-25 11:33:07 +00:00
2019-12-16 08:49:10 +00:00
return errors;
};
type LoginFormProps = { emailValue: string } & InjectedFormProps<
LoginFormValues,
{ emailValue: string }
>;
Use injected configuration from Nginx at runtime instead of build time (#30) * Use envsubst and nginx templates to generate nginx configs which can substitute environment variables and inject into the index.html file * Fix path in dockerfile. Add .gitignore and .env.example files. Fix nginx-linux template. * Add all environment variables. Add prefix to all environment variables. Update scripts to attempt to substitute all environment variables with the prefix * Setup dockerfile to execute a bash script. use env.example for fetching environment variables in development * Toggle features based on injected configs. Fix nginx template substitution script. * Update env.example file * Remove debug code from start-nginx.sh * Fix nginx config templates by adding quotes by default. Fix sed regex to include numerals. Toggle social login buttons on Login page based on the config. * Update rapid api environment variable name. Toggle oauth buttons based on config in SignUp page. Update .env.example to be a union of server and client environment variables * Adding a Map disabled message on Map widget * Adding links to Privacy policy and TNC * Use REACT_APP_ env variables with higher priority over injected config variables for toggling features * Update netlify.toml by commenting out the build environment variables * Remove env variables not required by the client * Remove start-storybook entry from package.json * Fix netlify.toml. Fallback algolia configs * Add contexts to netlify.toml for successful deploys. Swith to using APPSMITH_MARKETPLACE_URL as the toggle for RapidAPI feature on the client. Remove comments in nginx config templates. Fix template used in dockerfile. Co-authored-by: Satbir Singh <apple@apples-MacBook-Pro.local> Co-authored-by: Satbir Singh <satbir121@gmail.com>
2020-07-07 10:22:17 +00:00
const SocialLoginList: string[] = [];
if (enableGithubOAuth) SocialLoginList.push(SocialLoginTypes.GITHUB);
if (enableGoogleOAuth) SocialLoginList.push(SocialLoginTypes.GOOGLE);
2019-12-16 08:49:10 +00:00
export const Login = (props: LoginFormProps) => {
2020-02-11 09:56:21 +00:00
const { error, valid } = props;
2019-12-16 08:49:10 +00:00
const location = useLocation();
const queryParams = new URLSearchParams(location.search);
let showError = false;
if (queryParams.get("error")) {
showError = true;
}
let loginURL = "/api/v1/" + LOGIN_SUBMIT_PATH;
let signupURL = SIGN_UP_URL;
if (queryParams.has("redirectTo")) {
loginURL += `?redirectUrl=${queryParams.get("redirectTo")}`;
signupURL += `?redirectTo=${queryParams.get("redirectTo")}`;
}
2019-12-16 08:49:10 +00:00
let forgotPasswordURL = `${FORGOT_PASSWORD_URL}`;
if (props.emailValue && !isEmptyString(props.emailValue)) {
forgotPasswordURL += `?email=${props.emailValue}`;
}
return (
<AuthCardContainer>
2020-02-11 09:56:21 +00:00
{showError && (
<FormMessage
intent="warning"
2019-12-16 08:49:10 +00:00
message={LOGIN_PAGE_INVALID_CREDS_ERROR}
actions={[
{
url: FORGOT_PASSWORD_URL,
text: LOGIN_PAGE_INVALID_CREDS_FORGOT_PASSWORD_LINK,
intent: "success",
},
]}
/>
)}
<AuthCardHeader>
<h1>{LOGIN_PAGE_TITLE}</h1>
<h5>{LOGIN_PAGE_SUBTITLE}</h5>
</AuthCardHeader>
<AuthCardBody>
<SpacedSubmitForm method="POST" action={loginURL}>
2019-12-16 08:49:10 +00:00
<FormGroup
intent={error ? "danger" : "none"}
label={LOGIN_PAGE_EMAIL_INPUT_LABEL}
>
2020-02-25 11:33:07 +00:00
<FormTextField
name={LOGIN_FORM_EMAIL_FIELD_NAME}
2019-12-16 08:49:10 +00:00
type="email"
placeholder={LOGIN_PAGE_EMAIL_INPUT_PLACEHOLDER}
autoFocus
2019-12-16 08:49:10 +00:00
/>
</FormGroup>
<FormGroup
intent={error ? "danger" : "none"}
label={LOGIN_PAGE_PASSWORD_INPUT_LABEL}
2020-02-25 11:33:07 +00:00
helperText={FORM_VALIDATION_PASSWORD_RULE}
2019-12-16 08:49:10 +00:00
>
2020-02-25 11:33:07 +00:00
<FormTextField
2019-12-16 08:49:10 +00:00
type="password"
name={LOGIN_FORM_PASSWORD_FIELD_NAME}
2019-12-16 08:49:10 +00:00
placeholder={LOGIN_PAGE_PASSWORD_INPUT_PLACEHOLDER}
/>
</FormGroup>
<Link to={forgotPasswordURL}>{LOGIN_PAGE_FORGOT_PASSWORD_TEXT}</Link>
<FormActions>
2020-02-24 09:35:11 +00:00
<Button
2019-12-16 08:49:10 +00:00
type="submit"
disabled={!valid}
2019-12-16 08:49:10 +00:00
text={LOGIN_PAGE_LOGIN_BUTTON_TEXT}
intent="primary"
2020-02-24 09:35:11 +00:00
filled
size="large"
2020-03-11 13:59:46 +00:00
onClick={() => {
PerformanceTracker.startTracking(
PerformanceTransactionName.LOGIN_CLICK,
);
2020-03-11 13:59:46 +00:00
AnalyticsUtil.logEvent("LOGIN_CLICK", {
loginMethod: "EMAIL",
});
}}
2019-12-16 08:49:10 +00:00
/>
</FormActions>
</SpacedSubmitForm>
{SocialLoginList.length > 0 && (
<>
<Divider />
<ThirdPartyAuth type={"SIGNIN"} logins={SocialLoginList} />
</>
)}
2019-12-16 08:49:10 +00:00
</AuthCardBody>
<AuthCardNavLink to={signupURL}>
2019-12-16 08:49:10 +00:00
{LOGIN_PAGE_SIGN_UP_LINK_TEXT}
</AuthCardNavLink>
<AuthCardFooter>
<TncPPLinks />
2019-12-16 08:49:10 +00:00
</AuthCardFooter>
</AuthCardContainer>
);
};
const selector = formValueSelector(LOGIN_FORM_NAME);
export default connect(state => ({
emailValue: selector(state, LOGIN_FORM_EMAIL_FIELD_NAME),
2019-12-16 08:49:10 +00:00
}))(
reduxForm<LoginFormValues, { emailValue: string }>({
validate,
2020-02-25 11:33:07 +00:00
touchOnBlur: true,
2019-12-16 08:49:10 +00:00
form: LOGIN_FORM_NAME,
})(Login),
);