From 5125e126bdc5c2a12914d1b239abd2d467a461f7 Mon Sep 17 00:00:00 2001 From: BlackHole1 <158blackhole@gmail.com> Date: Tue, 7 Jun 2022 15:08:31 +0800 Subject: [PATCH] refactor(login): use 302 method in agora sso login --- .../src/api-middleware/flatServer/index.ts | 19 ---------- .../src/pages/LoginPage/agoraLogin.ts | 29 +++------------ web/flat-web/src/pages/LoginPage/index.tsx | 37 ++++++------------- web/flat-web/src/stores/GlobalStore.ts | 7 +--- 4 files changed, 17 insertions(+), 75 deletions(-) diff --git a/web/flat-web/src/api-middleware/flatServer/index.ts b/web/flat-web/src/api-middleware/flatServer/index.ts index ff9792ade3d..adfa89f6b09 100644 --- a/web/flat-web/src/api-middleware/flatServer/index.ts +++ b/web/flat-web/src/api-middleware/flatServer/index.ts @@ -489,7 +489,6 @@ export interface LoginProcessResult { userUUID: string; token: string; hasPhone: boolean; - agoraSSOLoginID?: string; } export async function loginProcess(authUUID: string): Promise { @@ -498,24 +497,6 @@ export async function loginProcess(authUUID: string): Promise { - return await postNotAuth( - "login/agora/check", - { - loginID, - }, - ); -} - export interface LoginPhoneSendCodePayload { phone: string; // +8612345678901 } diff --git a/web/flat-web/src/pages/LoginPage/agoraLogin.ts b/web/flat-web/src/pages/LoginPage/agoraLogin.ts index f3f865e63ca..0f495259d33 100644 --- a/web/flat-web/src/pages/LoginPage/agoraLogin.ts +++ b/web/flat-web/src/pages/LoginPage/agoraLogin.ts @@ -1,12 +1,11 @@ -import { setAuthUUID, loginProcess } from "../../api-middleware/flatServer"; +import { setAuthUUID } from "../../api-middleware/flatServer"; import { v4 as uuidv4 } from "uuid"; import { LoginExecutor } from "./utils"; import { errorTips } from "../../components/Tips/ErrorTips"; import { FLAT_SERVER_LOGIN } from "../../api-middleware/flatServer/constants"; import { AGORA_OAUTH } from "../../constants/process"; -export const agoraLogin: LoginExecutor = onSuccess => { - let timer = NaN; +export const agoraLogin: LoginExecutor = () => { const authUUID = uuidv4(); function getAgoraURL(authUUID: string): string { @@ -21,27 +20,9 @@ export const agoraLogin: LoginExecutor = onSuccess => { errorTips(err); } - void window.open(getAgoraURL(authUUID)); - - const agoraLoginProcessRequest = async (): Promise => { - try { - const data = await loginProcess(authUUID); - - if (!data.name) { - timer = window.setTimeout(agoraLoginProcessRequest, 2000); - return; - } - - onSuccess(data); - } catch (err) { - errorTips(err); - } - }; - - void agoraLoginProcessRequest(); + window.location.href = getAgoraURL(authUUID); })(); - return () => { - window.clearTimeout(timer); - }; + // eslint-disable-next-line @typescript-eslint/no-empty-function + return () => {}; }; diff --git a/web/flat-web/src/pages/LoginPage/index.tsx b/web/flat-web/src/pages/LoginPage/index.tsx index 5d5ba08aa2c..06bf46f0d1f 100644 --- a/web/flat-web/src/pages/LoginPage/index.tsx +++ b/web/flat-web/src/pages/LoginPage/index.tsx @@ -17,7 +17,6 @@ import { PRIVACY_URL, PRIVACY_URL_CN, SERVICE_URL, SERVICE_URL_CN } from "../../ import { useSafePromise } from "../../utils/hooks/lifecycle"; import { NEED_BINDING_PHONE } from "../../constants/config"; import { - agoraSSOLoginCheck, bindingPhone, bindingPhoneSendCode, loginCheck, @@ -59,41 +58,31 @@ export const LoginPage = observer(function LoginPage() { [globalStore, pushHistory], ); + useEffect(() => { + if (urlParams.utm_source === "agora") { + handleLogin("agora"); + } + }, [urlParams.utm_source]); + useEffect(() => { // Get login info through loginCheck(). // But we don't want to goto home page if already logged in. // Instead, if we have `hasPhone: false`, we should show the binding phone page. const checkNormalLogin = async (): Promise => { - const userInfo = await sp(loginCheck()); + const userInfo = await sp(loginCheck(urlParams.token)); if (NEED_BINDING_PHONE && !userInfo.hasPhone) { setLoginResult(userInfo); } }; - const checkAgoraLogin = async (): Promise => { - const { jwtToken } = await sp(agoraSSOLoginCheck(globalStore.agoraSSOLoginID!)); - const userInfo = await sp(loginCheck(jwtToken)); - setLoginResult(userInfo); - }; - - let checkLogin: Promise; - if (urlParams.utm_source === "agora" && globalStore.agoraSSOLoginID) { - checkLogin = checkAgoraLogin(); - } else { - checkLogin = checkNormalLogin(); - } - - checkLogin.catch(error => { + checkNormalLogin().catch(error => { // no handling required console.warn(error); }); - }, [globalStore, setLoginResult, sp, urlParams.utm_source]); + }, [globalStore, setLoginResult, sp, urlParams.token]); const onLoginResult = useCallback( async (authData: LoginProcessResult) => { - if (authData.agoraSSOLoginID) { - globalStore.updateAgoraSSOLoginID(authData.agoraSSOLoginID); - } globalStore.updateUserInfo(authData); if (NEED_BINDING_PHONE && !authData.hasPhone) { setLoginResult(authData); @@ -126,7 +115,7 @@ export const LoginPage = observer(function LoginPage() { } switch (loginChannel) { case "agora": { - loginDisposer.current = agoraLogin(onLoginResult); + agoraLogin(onLoginResult); return; } case "github": { @@ -156,11 +145,7 @@ export const LoginPage = observer(function LoginPage() { bindingPhone={async (countryCode, phone, code) => wrap(bindingPhone(countryCode + phone, Number(code)).then(onBoundPhone)) } - buttons={ - urlParams.utm_source === "agora" - ? ["agora"] - : [process.env.FLAT_REGION === "US" ? "google" : "wechat", "github"] - } + buttons={[process.env.FLAT_REGION === "US" ? "google" : "wechat", "github"]} cancelBindingPhone={() => setLoginResult(null)} isBindingPhone={ NEED_BINDING_PHONE && (loginResult ? !loginResult.hasPhone : false) diff --git a/web/flat-web/src/stores/GlobalStore.ts b/web/flat-web/src/stores/GlobalStore.ts index 68b940106ca..0b5c51ff368 100644 --- a/web/flat-web/src/stores/GlobalStore.ts +++ b/web/flat-web/src/stores/GlobalStore.ts @@ -6,7 +6,7 @@ import type { UID } from "agora-rtc-sdk-ng"; // clear storage if not match const LS_VERSION = 1; -export type UserInfo = Omit; +export type UserInfo = LoginProcessResult; /** * Properties in Global Store are persisted and shared globally. @@ -31,7 +31,6 @@ export class GlobalStore { } | null = null; public rtmToken: string | null = null; public lastLoginCheck: number | null = null; - public agoraSSOLoginID: string | null = null; /** * To sync update the roomStore's room information data after call the begin of the class in the classRoomStore, * that for sure roomStore's begin time value of roomInfo is correct so that the classroom page's Timer component display correctly. @@ -54,10 +53,6 @@ export class GlobalStore { this.userInfo = userInfo; }; - public updateAgoraSSOLoginID = (val: string | undefined | null): void => { - this.agoraSSOLoginID = val ?? null; - }; - public updateLastLoginCheck = (val: number | null): void => { this.lastLoginCheck = val; };