PromucFlow_constructor/app/client/src/ce/pages/UserAuth/ThirdPartyAuth.tsx

109 lines
2.9 KiB
TypeScript
Raw Normal View History

2019-12-16 08:49:10 +00:00
import React from "react";
import styled from "styled-components";
import {
getSocialLoginButtonProps,
SocialLoginType,
} from "@appsmith/constants/SocialLogin";
import { getTypographyByKey } from "constants/DefaultTheme";
2020-03-11 13:59:46 +00:00
import AnalyticsUtil, { EventName } from "utils/AnalyticsUtil";
import { useLocation } from "react-router-dom";
import PerformanceTracker, {
PerformanceTransactionName,
} from "utils/PerformanceTracker";
2019-12-16 08:49:10 +00:00
const ThirdPartyAuthWrapper = styled.div`
display: flex;
flex-direction: column;
`;
//TODO(abhinav): Port this to use themes.
const StyledSocialLoginButton = styled.a`
display: flex;
align-items: center;
justify-content: center;
border: solid 1px ${(props) => props.theme.colors.auth.socialBtnBorder};
margin-bottom: ${(props) => props.theme.spaces[4]}px;
&:only-child, &:last-child {
margin-bottom: 0;
}
2019-12-16 08:49:10 +00:00
&:hover {
text-decoration: none;
background-color: ${(props) => props.theme.colors.auth.socialBtnHighlight};
2019-12-16 08:49:10 +00:00
}
& .login-method {
${(props) => getTypographyByKey(props, "btnLarge")}
color: ${(props) => props.theme.colors.auth.socialBtnText};
text-transform: uppercase;
2019-12-16 08:49:10 +00:00
}
`;
const ButtonLogo = styled.img`
margin: ${(props) => props.theme.spaces[2]}px;
width: 24px;
`;
export const SocialLoginTypes = {
2019-12-16 08:49:10 +00:00
GOOGLE: "google",
GITHUB: "github",
};
2020-03-11 13:59:46 +00:00
type SignInType = "SIGNIN" | "SIGNUP";
function SocialLoginButton(props: {
2019-12-16 08:49:10 +00:00
logo: string;
name: string;
url: string;
label?: string;
2020-03-11 13:59:46 +00:00
type: SignInType;
}) {
const location = useLocation();
const queryParams = new URLSearchParams(location.search);
let url = props.url;
const redirectUrl = queryParams.get("redirectUrl");
if (redirectUrl != null) {
url += `?redirectUrl=${encodeURIComponent(redirectUrl)}`;
}
2019-12-16 08:49:10 +00:00
return (
2020-03-11 13:59:46 +00:00
<StyledSocialLoginButton
href={url}
2020-03-11 13:59:46 +00:00
onClick={() => {
let eventName: EventName = "LOGIN_CLICK";
if (props.type === "SIGNUP") {
eventName = "SIGNUP_CLICK";
}
PerformanceTracker.startTracking(
eventName === "SIGNUP_CLICK"
? PerformanceTransactionName.SIGN_UP
: PerformanceTransactionName.LOGIN_CLICK,
{ name: props.name.toUpperCase() },
);
2020-03-11 13:59:46 +00:00
AnalyticsUtil.logEvent(eventName, {
loginMethod: props.name.toUpperCase(),
});
}}
>
<ButtonLogo alt={` ${props.name} login`} src={props.logo} />
2022-04-21 06:14:02 +00:00
<div className="login-method" data-testid={`login-with-${props.name}`}>
{props.label ?? `continue with ${props.name}`}
</div>
2019-12-16 08:49:10 +00:00
</StyledSocialLoginButton>
);
}
2019-12-16 08:49:10 +00:00
export function ThirdPartyAuth(props: {
2020-03-11 13:59:46 +00:00
logins: SocialLoginType[];
type: SignInType;
}) {
2019-12-16 08:49:10 +00:00
const socialLoginButtons = getSocialLoginButtonProps(props.logins).map(
2020-12-24 04:32:25 +00:00
(item) => {
return <SocialLoginButton key={item.name} {...item} type={props.type} />;
2019-12-16 08:49:10 +00:00
},
);
return <ThirdPartyAuthWrapper>{socialLoginButtons}</ThirdPartyAuthWrapper>;
}
2019-12-16 08:49:10 +00:00
export default ThirdPartyAuth;