2021-06-09 01:53:30 +03:00
|
|
|
import * as React from "react";
|
2021-07-29 22:10:47 +03:00
|
|
|
|
2021-06-09 01:53:30 +03:00
|
|
|
import WebsitePrototypeWrapper from "~/components/core/WebsitePrototypeWrapper";
|
2021-11-11 20:34:42 +03:00
|
|
|
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;
|
|
|
|
`;
|
|
|
|
|
2021-12-13 20:21:43 +03:00
|
|
|
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;
|
2021-12-13 20:21:43 +03:00
|
|
|
@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
|
|
|
`;
|
|
|
|
|
2021-12-06 14:22:12 +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();
|
|
|
|
|
2021-12-06 14:22:12 +03:00
|
|
|
// NOTE(amine): if the redirectUrl is provided, redirect users to it when they authenticate
|
2021-12-20 13:27:35 +03:00
|
|
|
const redirectUrl = React.useRef(page?.params?.redirect && decodeURI(page?.params?.redirect));
|
|
|
|
|
2021-12-06 14:22:12 +03:00
|
|
|
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({
|
2021-12-06 14:22:12 +03:00
|
|
|
onAuthenticate: handleAuthentication,
|
2021-06-09 01:53:30 +03:00
|
|
|
});
|
2021-12-06 14:22:12 +03:00
|
|
|
|
|
|
|
const signupProvider = useSignup({ onAuthenticate: handleAuthentication });
|
|
|
|
|
2021-06-09 01:53:30 +03:00
|
|
|
const twitterProvider = useTwitter({
|
2021-12-06 14:22:12 +03:00
|
|
|
onTwitterAuthenticate: handleTwitterAuthentication,
|
|
|
|
onAuthenticate: handleAuthentication,
|
2021-06-09 01:53:30 +03:00
|
|
|
goToTwitterSignupScene,
|
|
|
|
});
|
|
|
|
|
|
|
|
const passwordResetProvider = usePasswordReset({
|
2021-12-06 14:22:12 +03:00
|
|
|
onAuthenticate: handleAuthentication,
|
2021-06-09 01:53:30 +03:00
|
|
|
});
|
|
|
|
|
2021-12-06 14:22:12 +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 =
|
2021-12-06 14:22:12 +03:00
|
|
|
prevScene === "signin" && context.emailOrUsername
|
|
|
|
? context.emailOrUsername
|
|
|
|
: page?.params?.email || "";
|
|
|
|
|
2021-06-09 01:53:30 +03:00
|
|
|
return (
|
|
|
|
<Initial
|
2021-12-06 14:22:12 +03:00
|
|
|
ref={initialScreenRef}
|
|
|
|
page={page}
|
|
|
|
initialEmail={initialEmail}
|
2021-06-09 01:53:30 +03:00
|
|
|
isSigninViaTwitter={twitterProvider.isLoggingIn}
|
|
|
|
onTwitterSignin={twitterProvider.signin}
|
|
|
|
goToSigninScene={goToSigninScene}
|
|
|
|
goToSignupScene={goToSignupScene}
|
2021-12-06 14:22:12 +03:00
|
|
|
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 (
|
2021-07-27 18:20:57 +03:00
|
|
|
<WebsitePrototypeWrapper>
|
2021-11-11 20:34:42 +03:00
|
|
|
<AuthWrapper css={STYLES_ROOT} isMobile={props.isMobile}>
|
2021-07-27 18:20:57 +03:00
|
|
|
<div css={STYLES_MIDDLE}>
|
|
|
|
<Component {...props} />
|
|
|
|
</div>
|
2021-11-11 20:34:42 +03:00
|
|
|
</AuthWrapper>
|
2021-07-27 18:20:57 +03:00
|
|
|
</WebsitePrototypeWrapper>
|
|
|
|
);
|
2021-07-29 22:01:41 +03:00
|
|
|
};
|
2021-06-09 01:53:30 +03:00
|
|
|
|
2021-12-06 14:22:12 +03:00
|
|
|
export default WithCustomWrapper(AuthScene);
|