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

refactor(login): use 302 method in agora sso login #1557

Merged
merged 1 commit into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 0 additions & 19 deletions web/flat-web/src/api-middleware/flatServer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,6 @@ export interface LoginProcessResult {
userUUID: string;
token: string;
hasPhone: boolean;
agoraSSOLoginID?: string;
}

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

export interface AgoraSSOLoginCheckPayload {
loginID: string;
}

export interface AgoraSSOLoginCheckResult {
jwtToken: string;
}

// Only Web
export async function agoraSSOLoginCheck(loginID: string): Promise<AgoraSSOLoginCheckResult> {
return await postNotAuth<AgoraSSOLoginCheckPayload, AgoraSSOLoginCheckResult>(
"login/agora/check",
{
loginID,
},
);
}

export interface LoginPhoneSendCodePayload {
phone: string; // +8612345678901
}
Expand Down
29 changes: 5 additions & 24 deletions web/flat-web/src/pages/LoginPage/agoraLogin.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -21,27 +20,9 @@ export const agoraLogin: LoginExecutor = onSuccess => {
errorTips(err);
}

void window.open(getAgoraURL(authUUID));

const agoraLoginProcessRequest = async (): Promise<void> => {
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 () => {};
};
37 changes: 11 additions & 26 deletions web/flat-web/src/pages/LoginPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<void> => {
const userInfo = await sp(loginCheck());
const userInfo = await sp(loginCheck(urlParams.token));
if (NEED_BINDING_PHONE && !userInfo.hasPhone) {
setLoginResult(userInfo);
}
};

const checkAgoraLogin = async (): Promise<void> => {
const { jwtToken } = await sp(agoraSSOLoginCheck(globalStore.agoraSSOLoginID!));
const userInfo = await sp(loginCheck(jwtToken));
setLoginResult(userInfo);
};

let checkLogin: Promise<unknown>;
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);
Expand Down Expand Up @@ -126,7 +115,7 @@ export const LoginPage = observer(function LoginPage() {
}
switch (loginChannel) {
case "agora": {
loginDisposer.current = agoraLogin(onLoginResult);
agoraLogin(onLoginResult);
return;
}
case "github": {
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 1 addition & 6 deletions web/flat-web/src/stores/GlobalStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<LoginProcessResult, "agoraSSOLoginID">;
export type UserInfo = LoginProcessResult;

/**
* Properties in Global Store are persisted and shared globally.
Expand All @@ -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.
Expand All @@ -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;
};
Expand Down