2019-12-16 08:49:10 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import styled from "styled-components";
|
|
|
|
|
import {
|
|
|
|
|
getSocialLoginButtonProps,
|
|
|
|
|
SocialLoginType,
|
2022-01-07 06:08:17 +00:00
|
|
|
} from "@appsmith/constants/SocialLogin";
|
2021-01-27 06:12:32 +00:00
|
|
|
import { getTypographyByKey } from "constants/DefaultTheme";
|
2020-03-11 13:59:46 +00:00
|
|
|
import AnalyticsUtil, { EventName } from "utils/AnalyticsUtil";
|
2020-08-03 14:18:48 +00:00
|
|
|
import { useLocation } from "react-router-dom";
|
2020-09-28 06:29:41 +00:00
|
|
|
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;
|
2021-01-27 06:12:32 +00:00
|
|
|
justify-content: center;
|
|
|
|
|
border: solid 1px ${(props) => props.theme.colors.auth.socialBtnBorder};
|
2022-01-07 06:08:17 +00:00
|
|
|
margin-bottom: ${(props) => props.theme.spaces[4]}px;
|
2021-01-27 06:12:32 +00:00
|
|
|
|
2022-01-07 06:08:17 +00:00
|
|
|
&:only-child, &:last-child {
|
2021-01-27 06:12:32 +00:00
|
|
|
margin-bottom: 0;
|
|
|
|
|
}
|
2019-12-16 08:49:10 +00:00
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
|
text-decoration: none;
|
2021-01-28 07:20:09 +00:00
|
|
|
background-color: ${(props) => props.theme.colors.auth.socialBtnHighlight};
|
2019-12-16 08:49:10 +00:00
|
|
|
}
|
2021-01-27 06:12:32 +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
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2021-01-27 06:12:32 +00:00
|
|
|
const ButtonLogo = styled.img`
|
|
|
|
|
margin: ${(props) => props.theme.spaces[2]}px;
|
2022-01-07 06:08:17 +00:00
|
|
|
width: 24px;
|
2021-01-27 06:12:32 +00:00
|
|
|
`;
|
|
|
|
|
|
2021-08-03 08:06:48 +00:00
|
|
|
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";
|
|
|
|
|
|
2021-04-28 10:28:39 +00:00
|
|
|
function SocialLoginButton(props: {
|
2019-12-16 08:49:10 +00:00
|
|
|
logo: string;
|
|
|
|
|
name: string;
|
|
|
|
|
url: string;
|
2022-01-07 06:08:17 +00:00
|
|
|
label?: string;
|
2020-03-11 13:59:46 +00:00
|
|
|
type: SignInType;
|
2021-04-28 10:28:39 +00:00
|
|
|
}) {
|
2020-08-03 14:18:48 +00:00
|
|
|
const location = useLocation();
|
|
|
|
|
const queryParams = new URLSearchParams(location.search);
|
|
|
|
|
let url = props.url;
|
2021-05-18 05:52:54 +00:00
|
|
|
const redirectUrl = queryParams.get("redirectUrl");
|
|
|
|
|
if (redirectUrl != null) {
|
|
|
|
|
url += `?redirectUrl=${encodeURIComponent(redirectUrl)}`;
|
2020-08-03 14:18:48 +00:00
|
|
|
}
|
2019-12-16 08:49:10 +00:00
|
|
|
return (
|
2020-03-11 13:59:46 +00:00
|
|
|
<StyledSocialLoginButton
|
2020-08-03 14:18:48 +00:00
|
|
|
href={url}
|
2020-03-11 13:59:46 +00:00
|
|
|
onClick={() => {
|
|
|
|
|
let eventName: EventName = "LOGIN_CLICK";
|
|
|
|
|
if (props.type === "SIGNUP") {
|
|
|
|
|
eventName = "SIGNUP_CLICK";
|
|
|
|
|
}
|
2020-09-28 06:29:41 +00:00
|
|
|
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(),
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
>
|
2021-01-27 06:12:32 +00:00
|
|
|
<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}`}>
|
2022-01-07 06:08:17 +00:00
|
|
|
{props.label ?? `continue with ${props.name}`}
|
|
|
|
|
</div>
|
2019-12-16 08:49:10 +00:00
|
|
|
</StyledSocialLoginButton>
|
|
|
|
|
);
|
2021-04-28 10:28:39 +00:00
|
|
|
}
|
2019-12-16 08:49:10 +00:00
|
|
|
|
2021-04-28 10:28:39 +00:00
|
|
|
export function ThirdPartyAuth(props: {
|
2020-03-11 13:59:46 +00:00
|
|
|
logins: SocialLoginType[];
|
|
|
|
|
type: SignInType;
|
2021-04-28 10:28:39 +00:00
|
|
|
}) {
|
2019-12-16 08:49:10 +00:00
|
|
|
const socialLoginButtons = getSocialLoginButtonProps(props.logins).map(
|
2020-12-24 04:32:25 +00:00
|
|
|
(item) => {
|
2020-08-03 14:18:48 +00:00
|
|
|
return <SocialLoginButton key={item.name} {...item} type={props.type} />;
|
2019-12-16 08:49:10 +00:00
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
return <ThirdPartyAuthWrapper>{socialLoginButtons}</ThirdPartyAuthWrapper>;
|
2021-04-28 10:28:39 +00:00
|
|
|
}
|
2019-12-16 08:49:10 +00:00
|
|
|
|
|
|
|
|
export default ThirdPartyAuth;
|