Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(web,renderer-app,flat-component): add agora sso #1341

Merged
merged 11 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/.env.development
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ NETLESS_APP_IDENTIFIER=n9q1oBxDEeyuBMn1qc0iFw/fLgNSEvdwKjlig
AGORA_APP_ID=931b86d6781e49a2a255db4ce6e8e804
GITHUB_CLIENT_ID=9821657775fbc74773f1
WECHAT_APP_ID=wx1133c2153a45e9b8
AGORA_OAUTH_CLIENT_ID=flat-dev

CLOUD_STORAGE_OSS_ALIBABA_ACCESS_KEY=LTAI5t9Gb6tzQzzLmB6cTVf7
CLOUD_STORAGE_OSS_ALIBABA_BUCKET=flat-storage
Expand Down
1 change: 1 addition & 0 deletions config/.env.production
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ NETLESS_APP_IDENTIFIER=cFjxAJjiEeuUQ0211QCRBw/mO9uJB_DiCIqug
AGORA_APP_ID=931b86d6781e49a2a255db4ce6e8e804
GITHUB_CLIENT_ID=71a29285a437998bdfe0
WECHAT_APP_ID=wx96d522d69d384cce
AGORA_OAUTH_CLIENT_ID=flat

CLOUD_STORAGE_OSS_ALIBABA_ACCESS_KEY=LTAI5t9Gb6tzQzzLmB6cTVf7
CLOUD_STORAGE_OSS_ALIBABA_BUCKET=flat-storage
Expand Down
93 changes: 69 additions & 24 deletions desktop/renderer-app/src/pages/LoginPage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
import "./index.less";

import React, { useContext, useEffect, useRef, useState } from "react";
import React, { useCallback, useContext, useEffect, useRef, useState } from "react";
import { constants } from "flat-types";
import { observer } from "mobx-react-lite";
import { ipcAsyncByMainWindow, ipcSyncByApp } from "../../utils/ipc";
import { LoginChannelType, LoginPanel } from "flat-components";
import { LoginPanel, LoginButton, LoginButtonProviderType } from "flat-components";
import { LoginDisposer } from "./utils";
import { githubLogin } from "./githubLogin";
import { RouteNameType, usePushHistory } from "../../utils/routes";
import { GlobalStoreContext } from "../../components/StoreProvider";
import { AppUpgradeModal, AppUpgradeModalProps } from "../../components/AppUpgradeModal";
import { runtime } from "../../utils/runtime";
import { useSafePromise } from "../../utils/hooks/lifecycle";
import { WeChatLogin } from "./WeChatLogin";
import { useTranslation } from "react-i18next";
import {
PRIVACY_URL_EN,
PRIVACY_URL_CN,
SERVICE_URL_EN,
SERVICE_URL_CN,
} from "../../constants/process";
import { message } from "antd";
import WeChatLogin from "./WeChatLogin";

export const LoginPage = observer(function LoginPage() {
const { i18n } = useTranslation();
const pushHistory = usePushHistory();
const globalStore = useContext(GlobalStoreContext);
const loginDisposer = useRef<LoginDisposer>();
const [updateInfo, setUpdateInfo] = useState<AppUpgradeModalProps["updateInfo"]>(null);
const [isWeChatLogin, setWeChatLogin] = useState<boolean>(false);
const [agreementChecked, setAgreementChecked] = useState<boolean>(false);

const sp = useSafePromise();

useEffect(() => {
Expand Down Expand Up @@ -61,35 +65,76 @@ export const LoginPage = observer(function LoginPage() {
});
}, [sp]);

const handleLogin = (loginChannel: LoginChannelType): React.ReactElement | undefined => {
if (loginDisposer.current) {
loginDisposer.current();
loginDisposer.current = void 0;
}

switch (loginChannel) {
case "github": {
loginDisposer.current = githubLogin(authData => {
globalStore.updateUserInfo(authData);
pushHistory(RouteNameType.HomePage);
});
return;
}
case "wechat": {
return <WeChatLogin />;
const handleLogin = useCallback(
(loginChannel: LoginButtonProviderType) => {
if (loginDisposer.current) {
loginDisposer.current();
loginDisposer.current = void 0;
}
default: {
return;

const doLogin = (loginChannel: LoginButtonProviderType): void => {
switch (loginChannel) {
case "github": {
loginDisposer.current = githubLogin(authData => {
globalStore.updateUserInfo(authData);
pushHistory(RouteNameType.HomePage);
});
return;
}
case "wechat": {
setWeChatLogin(true);
return;
}
default: {
return;
}
}
};
if (agreementChecked) {
doLogin(loginChannel);
} else {
void message.info(i18n.t("agree-terms"));
}
}
};
},
[agreementChecked, globalStore, i18n, pushHistory],
);

const privacyURL = i18n.language.startsWith("zh") ? PRIVACY_URL_CN : PRIVACY_URL_EN;
const serviceURL = i18n.language.startsWith("zh") ? SERVICE_URL_CN : SERVICE_URL_EN;

function renderButtonList(): React.ReactNode {
return (
<>
<LoginButton
provider="wechat"
text={i18n.t("login-wechat")}
onLogin={handleLogin}
/>
<LoginButton
provider="github"
text={i18n.t("login-github")}
onLogin={handleLogin}
/>
</>
);
}

function renderQRCode(): React.ReactNode {
return <WeChatLogin />;
}

return (
<div className="login-page-container">
<LoginPanel privacyURL={privacyURL} serviceURL={serviceURL} onLogin={handleLogin} />
<LoginPanel
agreementChecked={agreementChecked}
handleClickAgreement={() => setAgreementChecked(!agreementChecked)}
handleHideQRCode={() => setWeChatLogin(false)}
privacyURL={privacyURL}
renderButtonList={renderButtonList}
renderQRCode={renderQRCode}
serviceURL={serviceURL}
showQRCode={isWeChatLogin}
/>
<AppUpgradeModal updateInfo={updateInfo} onClose={() => setUpdateInfo(null)} />
</div>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Meta, Story } from "@storybook/react";
import React from "react";
import { useTranslation } from "react-i18next";
import { LoginButton, LoginButtonProviderType, LoginButtonProps } from "..";
import { message } from "antd";

const storyMeta: Meta = {
title: "LoginPage/LoginButton",
component: LoginButton,
};

export default storyMeta;

export const Overview: Story<LoginButtonProps> = () => {
const { i18n } = useTranslation();

const handleLogin = (type: LoginButtonProviderType): void => {
void message.info(type);
};

return (
<>
<LoginButton provider="wechat" text={i18n.t("login-wechat")} onLogin={handleLogin} />
<LoginButton provider="github" text={i18n.t("login-github")} onLogin={handleLogin} />
</>
);
};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
.login-channel-container {
display: flex;
flex-direction: column;
align-items: center;
.login-channel-lg {

> .ant-btn {

&.ant-btn {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
width: 272px;
height: 44px;
color: #fff;
Expand All @@ -18,6 +20,21 @@
margin-right: 3px;
}
}


.login-channel-agora {
&.ant-btn {
background: #089CFD;

&:hover {
background: #45B4FD;
}
&:active {
background: #0675BE;
}
}
}

.login-channel-wechat {
margin-bottom: 12px;

Expand All @@ -34,6 +51,8 @@
}

.login-channel-github {
margin-bottom: 12px;

&.ant-btn {
background: #24292e;

Expand All @@ -44,4 +63,4 @@
background: #1b1f22;
}
}
}
}
Matrixbirds marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Button } from "antd";
import React from "react";
import wechatSVG from "./icons/wechat.svg";
import agoraSVG from "./icons/agora.svg";
import githubSVG from "./icons/github.svg";
import "./index.less";

export type LoginButtonProviderType = "wechat" | "github" | "agora";

export type LoginButtonProps = {
provider: LoginButtonProviderType;
onLogin: (type: LoginButtonProviderType) => void;
text: string;
};

const svgDict: Record<LoginButtonProviderType, string> = {
wechat: wechatSVG,
agora: agoraSVG,
github: githubSVG,
};

export const LoginButton: React.FC<LoginButtonProps> = ({ provider, onLogin, text }) => {
return (
<Button
className={`login-channel-lg login-channel-${provider}`}
onClick={() => onLogin(provider)}
>
<img src={svgDict[provider]} />
{text}
</Button>
);
};

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Meta, Story } from "@storybook/react";
import React from "react";
import { LoginContent, LoginContentProps } from ".";
import React, { useState, useCallback } from "react";
import { useTranslation } from "react-i18next";
import { LoginContent, LoginContentProps } from "./index";
import { message } from "antd";
import { LoginButton } from "..";

const storyMeta: Meta = {
title: "LoginPage/LoginContent",
Expand All @@ -9,8 +12,41 @@ const storyMeta: Meta = {

export default storyMeta;

export const Overview: Story<LoginContentProps> = args => (
<div className="vh-75">
<LoginContent {...args} />
</div>
);
export const Overview: Story<LoginContentProps> = () => {
const { i18n } = useTranslation();
const [showQRCode, updateShowQRCode] = useState<boolean>(false);

const [agreement, updateAgreement] = useState<boolean>(false);

const handleClickAgreement = (): void => {
updateAgreement(!agreement);
};

const handleLogin = useCallback(() => {
agreement === false && void message.info(i18n.t("agree-terms"));
}, [agreement, i18n]);


const { t } = useTranslation();

function renderButtonList(): React.ReactNode {
return (
<>
<LoginButton provider={"wechat"} text={t("login-wechat")} onLogin={handleLogin} />
<LoginButton provider={"github"} text={t("login-github")} onLogin={handleLogin} />
</>
);
}

return (
<div className="vh-75">
<LoginContent
agreementChecked={agreement}
handleClickAgreement={() => updateAgreement(!agreement)}
handleHideQRCode={handleHideQRCode}
renderButtonList={renderButtonList}
showQRCode={showQRCode}
/>
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
width: 21%;
margin: 0 auto;
margin-bottom: 24px;
display: flex;
align-items: center;
flex-direction: column;
}

.login-content-agreement {
Expand Down
Loading