Skip to content

Commit 4663833

Browse files
authored
refactor(login): use 302 method in agora sso login (#1557)
1 parent 390e371 commit 4663833

File tree

4 files changed

+17
-75
lines changed

4 files changed

+17
-75
lines changed

web/flat-web/src/api-middleware/flatServer/index.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,6 @@ export interface LoginProcessResult {
489489
userUUID: string;
490490
token: string;
491491
hasPhone: boolean;
492-
agoraSSOLoginID?: string;
493492
}
494493

495494
export async function loginProcess(authUUID: string): Promise<LoginProcessResult> {
@@ -498,24 +497,6 @@ export async function loginProcess(authUUID: string): Promise<LoginProcessResult
498497
});
499498
}
500499

501-
export interface AgoraSSOLoginCheckPayload {
502-
loginID: string;
503-
}
504-
505-
export interface AgoraSSOLoginCheckResult {
506-
jwtToken: string;
507-
}
508-
509-
// Only Web
510-
export async function agoraSSOLoginCheck(loginID: string): Promise<AgoraSSOLoginCheckResult> {
511-
return await postNotAuth<AgoraSSOLoginCheckPayload, AgoraSSOLoginCheckResult>(
512-
"login/agora/check",
513-
{
514-
loginID,
515-
},
516-
);
517-
}
518-
519500
export interface LoginPhoneSendCodePayload {
520501
phone: string; // +8612345678901
521502
}
Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { setAuthUUID, loginProcess } from "../../api-middleware/flatServer";
1+
import { setAuthUUID } from "../../api-middleware/flatServer";
22
import { v4 as uuidv4 } from "uuid";
33
import { LoginExecutor } from "./utils";
44
import { errorTips } from "../../components/Tips/ErrorTips";
55
import { FLAT_SERVER_LOGIN } from "../../api-middleware/flatServer/constants";
66
import { AGORA_OAUTH } from "../../constants/process";
77

8-
export const agoraLogin: LoginExecutor = onSuccess => {
9-
let timer = NaN;
8+
export const agoraLogin: LoginExecutor = () => {
109
const authUUID = uuidv4();
1110

1211
function getAgoraURL(authUUID: string): string {
@@ -21,27 +20,9 @@ export const agoraLogin: LoginExecutor = onSuccess => {
2120
errorTips(err);
2221
}
2322

24-
void window.open(getAgoraURL(authUUID));
25-
26-
const agoraLoginProcessRequest = async (): Promise<void> => {
27-
try {
28-
const data = await loginProcess(authUUID);
29-
30-
if (!data.name) {
31-
timer = window.setTimeout(agoraLoginProcessRequest, 2000);
32-
return;
33-
}
34-
35-
onSuccess(data);
36-
} catch (err) {
37-
errorTips(err);
38-
}
39-
};
40-
41-
void agoraLoginProcessRequest();
23+
window.location.href = getAgoraURL(authUUID);
4224
})();
4325

44-
return () => {
45-
window.clearTimeout(timer);
46-
};
26+
// eslint-disable-next-line @typescript-eslint/no-empty-function
27+
return () => {};
4728
};

web/flat-web/src/pages/LoginPage/index.tsx

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { PRIVACY_URL, PRIVACY_URL_CN, SERVICE_URL, SERVICE_URL_CN } from "../../
1717
import { useSafePromise } from "../../utils/hooks/lifecycle";
1818
import { NEED_BINDING_PHONE } from "../../constants/config";
1919
import {
20-
agoraSSOLoginCheck,
2120
bindingPhone,
2221
bindingPhoneSendCode,
2322
loginCheck,
@@ -59,41 +58,31 @@ export const LoginPage = observer(function LoginPage() {
5958
[globalStore, pushHistory],
6059
);
6160

61+
useEffect(() => {
62+
if (urlParams.utm_source === "agora") {
63+
handleLogin("agora");
64+
}
65+
}, [urlParams.utm_source]);
66+
6267
useEffect(() => {
6368
// Get login info through loginCheck().
6469
// But we don't want to goto home page if already logged in.
6570
// Instead, if we have `hasPhone: false`, we should show the binding phone page.
6671
const checkNormalLogin = async (): Promise<void> => {
67-
const userInfo = await sp(loginCheck());
72+
const userInfo = await sp(loginCheck(urlParams.token));
6873
if (NEED_BINDING_PHONE && !userInfo.hasPhone) {
6974
setLoginResult(userInfo);
7075
}
7176
};
7277

73-
const checkAgoraLogin = async (): Promise<void> => {
74-
const { jwtToken } = await sp(agoraSSOLoginCheck(globalStore.agoraSSOLoginID!));
75-
const userInfo = await sp(loginCheck(jwtToken));
76-
setLoginResult(userInfo);
77-
};
78-
79-
let checkLogin: Promise<unknown>;
80-
if (urlParams.utm_source === "agora" && globalStore.agoraSSOLoginID) {
81-
checkLogin = checkAgoraLogin();
82-
} else {
83-
checkLogin = checkNormalLogin();
84-
}
85-
86-
checkLogin.catch(error => {
78+
checkNormalLogin().catch(error => {
8779
// no handling required
8880
console.warn(error);
8981
});
90-
}, [globalStore, setLoginResult, sp, urlParams.utm_source]);
82+
}, [globalStore, setLoginResult, sp, urlParams.token]);
9183

9284
const onLoginResult = useCallback(
9385
async (authData: LoginProcessResult) => {
94-
if (authData.agoraSSOLoginID) {
95-
globalStore.updateAgoraSSOLoginID(authData.agoraSSOLoginID);
96-
}
9786
globalStore.updateUserInfo(authData);
9887
if (NEED_BINDING_PHONE && !authData.hasPhone) {
9988
setLoginResult(authData);
@@ -126,7 +115,7 @@ export const LoginPage = observer(function LoginPage() {
126115
}
127116
switch (loginChannel) {
128117
case "agora": {
129-
loginDisposer.current = agoraLogin(onLoginResult);
118+
agoraLogin(onLoginResult);
130119
return;
131120
}
132121
case "github": {
@@ -156,11 +145,7 @@ export const LoginPage = observer(function LoginPage() {
156145
bindingPhone={async (countryCode, phone, code) =>
157146
wrap(bindingPhone(countryCode + phone, Number(code)).then(onBoundPhone))
158147
}
159-
buttons={
160-
urlParams.utm_source === "agora"
161-
? ["agora"]
162-
: [process.env.FLAT_REGION === "US" ? "google" : "wechat", "github"]
163-
}
148+
buttons={[process.env.FLAT_REGION === "US" ? "google" : "wechat", "github"]}
164149
cancelBindingPhone={() => setLoginResult(null)}
165150
isBindingPhone={
166151
NEED_BINDING_PHONE && (loginResult ? !loginResult.hasPhone : false)

web/flat-web/src/stores/GlobalStore.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { UID } from "agora-rtc-sdk-ng";
66
// clear storage if not match
77
const LS_VERSION = 1;
88

9-
export type UserInfo = Omit<LoginProcessResult, "agoraSSOLoginID">;
9+
export type UserInfo = LoginProcessResult;
1010

1111
/**
1212
* Properties in Global Store are persisted and shared globally.
@@ -31,7 +31,6 @@ export class GlobalStore {
3131
} | null = null;
3232
public rtmToken: string | null = null;
3333
public lastLoginCheck: number | null = null;
34-
public agoraSSOLoginID: string | null = null;
3534
/**
3635
* To sync update the roomStore's room information data after call the begin of the class in the classRoomStore,
3736
* 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 {
5453
this.userInfo = userInfo;
5554
};
5655

57-
public updateAgoraSSOLoginID = (val: string | undefined | null): void => {
58-
this.agoraSSOLoginID = val ?? null;
59-
};
60-
6156
public updateLastLoginCheck = (val: number | null): void => {
6257
this.lastLoginCheck = val;
6358
};

0 commit comments

Comments
 (0)