slate/scenes/SceneAuth/index.js

197 lines
5.4 KiB
JavaScript
Raw Normal View History

2021-06-09 01:53:30 +03:00
import * as React from "react";
2021-06-09 01:53:30 +03:00
import WebsitePrototypeWrapper from "~/components/core/WebsitePrototypeWrapper";
import { AuthWrapper } from "~/components/core/Auth/components";
2021-06-09 01:53:30 +03:00
import { css } from "@emotion/react";
2021-07-29 22:01:41 +03:00
import {
Initial,
Signin,
Signup,
TwitterSignup,
TwitterLinking,
ResetPassword,
} from "~/components/core/Auth";
2021-06-09 01:53:30 +03:00
import {
useAuthFlow,
useTwitter,
useSignup,
useSignin,
usePasswordReset,
} from "~/scenes/SceneAuth/hooks";
const STYLES_ROOT = css`
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
text-align: center;
font-size: 1rem;
2021-07-07 23:50:57 +03:00
min-height: 100vh;
2021-06-09 01:53:30 +03:00
width: 100vw;
2021-07-29 22:01:41 +03:00
position: relative;
2021-06-09 01:53:30 +03:00
overflow: hidden;
background-repeat: no-repeat;
background-size: cover;
`;
const STYLES_MIDDLE = (theme) => css`
2021-06-09 01:53:30 +03:00
position: relative;
min-height: 10%;
flex-grow: 1;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
text-align: left;
padding: 24px;
@media (max-width: ${theme.sizes.mobile}px) {
padding: 24px 16px;
//NOTE(amine): additional scroll space for mobile
padding-bottom: 64px;
}
2021-06-09 01:53:30 +03:00
`;
const AuthScene = ({ onAuthenticate, onTwitterAuthenticate, page, onAction, ...props }) => {
2021-06-09 01:53:30 +03:00
const {
goToSigninScene,
goToSignupScene,
goToTwitterSignupScene,
2021-07-29 22:01:41 +03:00
goToTwitterLinkingScene,
2021-06-09 01:53:30 +03:00
goToResetPassword,
clearMessages,
goBack,
prevScene,
scene,
context,
} = useAuthFlow();
// NOTE(amine): if the redirectUrl is provided, redirect users to it when they authenticate
const redirectUrl = React.useRef(page?.params?.redirect && decodeURI(page?.params?.redirect));
const handleAuthentication = (...params) => onAuthenticate(redirectUrl.current, ...params);
const handleTwitterAuthentication = (...params) =>
onTwitterAuthenticate(redirectUrl.current, ...params);
2021-06-09 01:53:30 +03:00
const signinProvider = useSignin({
onAuthenticate: handleAuthentication,
2021-06-09 01:53:30 +03:00
});
const signupProvider = useSignup({ onAuthenticate: handleAuthentication });
2021-06-09 01:53:30 +03:00
const twitterProvider = useTwitter({
onTwitterAuthenticate: handleTwitterAuthentication,
onAuthenticate: handleAuthentication,
2021-06-09 01:53:30 +03:00
goToTwitterSignupScene,
});
const passwordResetProvider = usePasswordReset({
onAuthenticate: handleAuthentication,
2021-06-09 01:53:30 +03:00
});
// NOTE(amine): authenticate via params
const initialScreenRef = React.useRef();
React.useEffect(() => {
if (!initialScreenRef.current) return;
if (page?.params?.tab === "signup" && page?.params?.email)
initialScreenRef.current.submitSignupForm();
if (page?.params?.tab === "signin" && page?.params?.email)
initialScreenRef.current.submitSigninField();
}, []);
2021-06-09 01:53:30 +03:00
if (scene === "signin")
return (
<Signin
{...props}
emailOrUsername={context.emailOrUsername}
message={context.message}
signin={signinProvider.signin}
createVerification={signinProvider.createVerification}
migrateAccount={signinProvider.migrateAccount}
resendEmailVerification={signinProvider.resendVerification}
goToResetPassword={goToResetPassword}
goBack={goBack}
clearMessages={clearMessages}
/>
);
if (scene === "password_reset") {
return (
<ResetPassword
goBack={goBack}
createVerification={passwordResetProvider.createVerification}
verifyEmail={passwordResetProvider.verifyEmail}
resetPassword={passwordResetProvider.resetPassword}
resendEmailVerification={passwordResetProvider.resendVerification}
/>
);
}
if (scene === "signup")
return (
<Signup
verifyEmail={signupProvider.verifyEmail}
resendEmailVerification={signupProvider.resendVerification}
createUser={signupProvider.createUser}
/>
);
if (scene === "twitter_signup")
return (
<TwitterSignup
initialEmail={context.twitterEmail}
createVerification={twitterProvider.createVerification}
2021-07-29 22:01:41 +03:00
resendEmailVerification={twitterProvider.resendVerification}
goToTwitterLinkingScene={goToTwitterLinkingScene}
2021-06-09 01:53:30 +03:00
onSignupWithVerification={twitterProvider.signupWithVerification}
onSignup={twitterProvider.signup}
/>
);
2021-07-29 22:01:41 +03:00
if (scene === "twitter_linking") {
return (
<TwitterLinking
linkAccount={twitterProvider.linkAccount}
linkAccountWithVerification={twitterProvider.linkAccountWithVerification}
resendEmailVerification={twitterProvider.resendVerification}
createVerification={twitterProvider.createVerification}
/>
);
}
2021-06-09 01:53:30 +03:00
// NOTE(amine): if the user goes back, we should prefill the email
const initialEmail =
prevScene === "signin" && context.emailOrUsername
? context.emailOrUsername
: page?.params?.email || "";
2021-06-09 01:53:30 +03:00
return (
<Initial
ref={initialScreenRef}
page={page}
initialEmail={initialEmail}
2021-06-09 01:53:30 +03:00
isSigninViaTwitter={twitterProvider.isLoggingIn}
onTwitterSignin={twitterProvider.signin}
goToSigninScene={goToSigninScene}
goToSignupScene={goToSignupScene}
createVerification={signupProvider.createVerification}
onAction={onAction}
2021-06-09 01:53:30 +03:00
/>
);
};
2021-06-22 22:48:00 +03:00
2021-07-29 22:01:41 +03:00
const WithCustomWrapper = (Component) => (props) => {
return (
<WebsitePrototypeWrapper>
<AuthWrapper css={STYLES_ROOT} isMobile={props.isMobile}>
<div css={STYLES_MIDDLE}>
<Component {...props} />
</div>
</AuthWrapper>
</WebsitePrototypeWrapper>
);
2021-07-29 22:01:41 +03:00
};
2021-06-09 01:53:30 +03:00
export default WithCustomWrapper(AuthScene);