chore: AuthenticationProvider component to function
This commit is contained in:
@@ -1,13 +1,13 @@
|
|||||||
import { EmailIcon } from "outline-icons";
|
import { EmailIcon } from "outline-icons";
|
||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import { WithTranslation, withTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import styled from "styled-components";
|
import styled from "styled-components";
|
||||||
import AuthLogo from "~/components/AuthLogo";
|
import AuthLogo from "~/components/AuthLogo";
|
||||||
import ButtonLarge from "~/components/ButtonLarge";
|
import ButtonLarge from "~/components/ButtonLarge";
|
||||||
import InputLarge from "~/components/InputLarge";
|
import InputLarge from "~/components/InputLarge";
|
||||||
import { client } from "~/utils/ApiClient";
|
import { client } from "~/utils/ApiClient";
|
||||||
|
|
||||||
type Props = WithTranslation & {
|
type Props = {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
authUrl: string;
|
authUrl: string;
|
||||||
@@ -15,111 +15,91 @@ type Props = WithTranslation & {
|
|||||||
onEmailSuccess: (email: string) => void;
|
onEmailSuccess: (email: string) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
type State = {
|
function AuthenticationProvider(props: Props) {
|
||||||
showEmailSignin: boolean;
|
const { t } = useTranslation();
|
||||||
isSubmitting: boolean;
|
const [showEmailSignin, setShowEmailSignin] = React.useState(false);
|
||||||
email: string;
|
const [isSubmitting, setSubmitting] = React.useState(false);
|
||||||
};
|
const [email, setEmail] = React.useState("");
|
||||||
|
const { isCreate, id, name, authUrl } = props;
|
||||||
|
|
||||||
class AuthenticationProvider extends React.Component<Props, State> {
|
const handleChangeEmail = (event: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
state = {
|
setEmail(event.target.value);
|
||||||
showEmailSignin: false,
|
|
||||||
isSubmitting: false,
|
|
||||||
email: "",
|
|
||||||
};
|
};
|
||||||
|
|
||||||
handleChangeEmail = (event: React.ChangeEvent<HTMLInputElement>) => {
|
const handleSubmitEmail = async (
|
||||||
this.setState({
|
event: React.SyntheticEvent<HTMLFormElement>
|
||||||
email: event.target.value,
|
) => {
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
handleSubmitEmail = async (event: React.SyntheticEvent<HTMLFormElement>) => {
|
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
if (this.state.showEmailSignin && this.state.email) {
|
if (showEmailSignin && email) {
|
||||||
this.setState({
|
setSubmitting(true);
|
||||||
isSubmitting: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await client.post(event.currentTarget.action, {
|
const response = await client.post(event.currentTarget.action, {
|
||||||
email: this.state.email,
|
email,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.redirect) {
|
if (response.redirect) {
|
||||||
window.location.href = response.redirect;
|
window.location.href = response.redirect;
|
||||||
} else {
|
} else {
|
||||||
this.props.onEmailSuccess(this.state.email);
|
props.onEmailSuccess(email);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
this.setState({
|
setSubmitting(false);
|
||||||
isSubmitting: false,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.setState({
|
setShowEmailSignin(true);
|
||||||
showEmailSignin: true,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
if (id === "email") {
|
||||||
const { isCreate, id, name, authUrl, t } = this.props;
|
if (isCreate) {
|
||||||
|
return null;
|
||||||
if (id === "email") {
|
|
||||||
if (isCreate) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Wrapper key="email">
|
|
||||||
<Form
|
|
||||||
method="POST"
|
|
||||||
action="/auth/email"
|
|
||||||
onSubmit={this.handleSubmitEmail}
|
|
||||||
>
|
|
||||||
{this.state.showEmailSignin ? (
|
|
||||||
<>
|
|
||||||
<InputLarge
|
|
||||||
type="email"
|
|
||||||
name="email"
|
|
||||||
placeholder="me@domain.com"
|
|
||||||
value={this.state.email}
|
|
||||||
onChange={this.handleChangeEmail}
|
|
||||||
disabled={this.state.isSubmitting}
|
|
||||||
autoFocus
|
|
||||||
required
|
|
||||||
short
|
|
||||||
/>
|
|
||||||
<ButtonLarge type="submit" disabled={this.state.isSubmitting}>
|
|
||||||
{t("Sign In")} →
|
|
||||||
</ButtonLarge>
|
|
||||||
</>
|
|
||||||
) : (
|
|
||||||
<ButtonLarge type="submit" icon={<EmailIcon />} fullwidth>
|
|
||||||
{t("Continue with Email")}
|
|
||||||
</ButtonLarge>
|
|
||||||
)}
|
|
||||||
</Form>
|
|
||||||
</Wrapper>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Wrapper key={id}>
|
<Wrapper>
|
||||||
<ButtonLarge
|
<Form method="POST" action="/auth/email" onSubmit={handleSubmitEmail}>
|
||||||
onClick={() => (window.location.href = authUrl)}
|
{showEmailSignin ? (
|
||||||
icon={<AuthLogo providerName={id} />}
|
<>
|
||||||
fullwidth
|
<InputLarge
|
||||||
>
|
type="email"
|
||||||
{t("Continue with {{ authProviderName }}", {
|
name="email"
|
||||||
authProviderName: name,
|
placeholder="me@domain.com"
|
||||||
})}
|
value={email}
|
||||||
</ButtonLarge>
|
onChange={handleChangeEmail}
|
||||||
|
disabled={isSubmitting}
|
||||||
|
autoFocus
|
||||||
|
required
|
||||||
|
short
|
||||||
|
/>
|
||||||
|
<ButtonLarge type="submit" disabled={isSubmitting}>
|
||||||
|
{t("Sign In")} →
|
||||||
|
</ButtonLarge>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<ButtonLarge type="submit" icon={<EmailIcon />} fullwidth>
|
||||||
|
{t("Continue with Email")}
|
||||||
|
</ButtonLarge>
|
||||||
|
)}
|
||||||
|
</Form>
|
||||||
</Wrapper>
|
</Wrapper>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Wrapper>
|
||||||
|
<ButtonLarge
|
||||||
|
onClick={() => (window.location.href = authUrl)}
|
||||||
|
icon={<AuthLogo providerName={id} />}
|
||||||
|
fullwidth
|
||||||
|
>
|
||||||
|
{t("Continue with {{ authProviderName }}", {
|
||||||
|
authProviderName: name,
|
||||||
|
})}
|
||||||
|
</ButtonLarge>
|
||||||
|
</Wrapper>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const Wrapper = styled.div`
|
const Wrapper = styled.div`
|
||||||
@@ -132,4 +112,4 @@ const Form = styled.form`
|
|||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
export default withTranslation()(AuthenticationProvider);
|
export default AuthenticationProvider;
|
||||||
|
|||||||
@@ -200,7 +200,7 @@ function Login() {
|
|||||||
)}
|
)}
|
||||||
</React.Fragment>
|
</React.Fragment>
|
||||||
)}
|
)}
|
||||||
{config.providers.map((provider: any) => {
|
{config.providers.map((provider) => {
|
||||||
if (defaultProvider && provider.id === defaultProvider.id) {
|
if (defaultProvider && provider.id === defaultProvider.id) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user