Skip to content

Commit a06cfc7

Browse files
authored
refactor(login): use 302 method in agora sso login (#559)
1 parent a5daa30 commit a06cfc7

File tree

6 files changed

+15
-153
lines changed

6 files changed

+15
-153
lines changed

src/utils/Redis.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export const RedisKey = {
55
agoraRTCRoomUserToken: (roomUUID: string, uid: string | number): string =>
66
`agora:rtc:room:${roomUUID}:uid:${uid}`,
77
agoraRTMUserToken: (userUUID: string): string => `agora:rtm:userUUID:${userUUID}`,
8-
agoraSSOLoginID: (loginID: string): string => `agora:sso:loginID:${loginID}`,
98
cloudStorageFileInfo: (userUUID: string, fileUUID: string): string =>
109
`cloudStorage:${userUUID}:${fileUUID}`,
1110
userAvatarFileInfo: (userUUID: string, fileUUID: string): string =>

src/v1/controller/login/Process.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,6 @@ export class LoginProcess extends AbstractController<RequestType, ResponseType>
6666

6767
const userInfo = JSON.parse(userInfoStr);
6868

69-
// Agora SSO
70-
if ("agoraSSOLoginID" in userInfo) {
71-
const thirtyDay = 60 * 60 * 24 * 30;
72-
RedisService.set(
73-
RedisKey.agoraSSOLoginID(userInfo.agoraSSOLoginID),
74-
userInfo.userUUID,
75-
thirtyDay,
76-
).catch(error => {
77-
this.logger.error(error);
78-
});
79-
}
80-
8169
return {
8270
status: Status.Success,
8371
data: userInfo,

src/v1/controller/login/Router.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { LoginProcess } from "./Process";
77
import { ControllerClass } from "../../../abstract/controller";
88
import { AppleJWT } from "./apple/jwt";
99
import { AgoraCallback } from "./agora/Callback";
10-
import { CheckAgoraSSOLoginID } from "./agora/Check";
1110
import { GoogleCallback } from "./google/Callback";
1211
import { SendMessage } from "./phone/SendMessage";
1312
import { PhoneLogin } from "./phone/Phone";
@@ -20,7 +19,6 @@ export const loginRouters: Readonly<Array<ControllerClass<any, any>>> = Object.f
2019
AppleJWT,
2120
GithubCallback,
2221
AgoraCallback,
23-
CheckAgoraSSOLoginID,
2422
GoogleCallback,
2523
SendMessage,
2624
PhoneLogin,

src/v1/controller/login/agora/Callback.ts

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class AgoraCallback extends AbstractController<RequestType> {
2121
public static readonly schema: FastifySchema<RequestType> = {
2222
querystring: {
2323
type: "object",
24-
required: ["state", "code", "loginId"],
24+
required: ["state", "code"],
2525
properties: {
2626
state: {
2727
type: "string",
@@ -30,19 +30,12 @@ export class AgoraCallback extends AbstractController<RequestType> {
3030
code: {
3131
type: "string",
3232
},
33-
loginId: {
34-
type: "string",
35-
},
3633
},
3734
},
3835
};
3936

4037
public async execute(): Promise<void> {
41-
void this.reply.headers({
42-
"content-type": "text/html",
43-
});
44-
45-
const { state: authUUID, code, loginId: loginID } = this.querystring;
38+
const { state: authUUID, code } = this.querystring;
4639

4740
await LoginAgora.assertHasAuthUUID(authUUID, this.logger);
4841

@@ -67,71 +60,46 @@ export class AgoraCallback extends AbstractController<RequestType> {
6760
});
6861
}
6962

70-
const { userName, avatarURL } = (await loginAgora.svc.user.nameAndAvatar())!;
71-
72-
await loginAgora.tempSaveUserInfo(authUUID, {
73-
name: userName,
74-
token: await this.reply.jwtSign({
75-
userUUID,
76-
loginSource: LoginPlatform.Agora,
77-
}),
78-
avatar: avatarURL,
79-
agoraSSOLoginID: loginID,
63+
const token = await this.reply.jwtSign({
64+
userUUID,
65+
loginSource: LoginPlatform.Agora,
8066
});
8167

82-
return this.reply.send(AgoraCallback.successHTML());
68+
void this.reply.redirect(`${Website}/login?token=${token}`);
8369
}
8470

8571
public async errorHandler(error: Error): Promise<ResponseError> {
8672
await redisService.set(RedisKey.authFailed(this.querystring.state), error.message, 60 * 60);
8773

8874
this.logger.error("request failed", parseError(error));
75+
76+
void this.reply.headers({
77+
"content-type": "text/html",
78+
});
8979
return this.reply.send(AgoraCallback.failedHTML);
9080
}
9181

92-
private static successHTML(): string {
82+
private static get failedHTML(): string {
9383
return `<!doctype html>
9484
<html lang="en">
9585
<head>
9686
<meta charset="UTF-8">
97-
<title>Login Success</title>
87+
<title>Login Failed</title>
9888
</head>
9989
<body>
10090
<svg style=max-width:80px;max-height:80px;position:fixed;top:50%;left:50%;transform:translate(-50%,-50%) viewBox="0 0 80 80" xmlns="http://www.w3.org/2000/svg">
101-
<path d="M40 0c22.0914 0 40 17.9086 40 40S62.0914 80 40 80 0 62.0914 0 40 17.9086 0 40 0zm0 4C20.1177 4 4 20.1177 4 40s16.1177 36 36 36 36-16.1177 36-36S59.8823 4 40 4zm22.6337 20.5395l2.7326 2.921-32.3889 30.2993L14.61 40.0046l2.78-2.876L33.022 52.24l29.6117-27.7005z" fill="#9FDF76" fill-rule="nonzero" />
91+
<path d="M40 0c22.0914 0 40 17.9086 40 40S62.0914 80 40 80 0 62.0914 0 40 17.9086 0 40 0zm0 4C20.1177 4 4 20.1177 4 40s16.1177 36 36 36 36-16.1177 36-36S59.8823 4 40 4zm21.0572 49.2345l.357.3513-2.8284 2.8284c-10.162-10.162-26.5747-10.2636-36.8617-.3048l-.3099.3048-2.8284-2.8284c11.7085-11.7085 30.619-11.8256 42.4714-.3513zM27 26c2.2091 0 4 1.7909 4 4 0 2.2091-1.7909 4-4 4-2.2091 0-4-1.7909-4-4 0-2.2091 1.7909-4 4-4zm26 0c2.2091 0 4 1.7909 4 4 0 2.2091-1.7909 4-4 4-2.2091 0-4-1.7909-4-4 0-2.2091 1.7909-4 4-4z" fill="#F45454" fill-rule="nonzero" />
10292
</svg>
103-
<div id="text" style=position:fixed;top:60%;left:50%;transform:translate(-50%,-50%)>Login Success. After 3s it will automatically jump to the home page</div>
104-
</body>
93+
<div id="text" style=position:fixed;top:60%;left:50%;transform:translate(-50%,-50%)>Login Failed. After 3s it will automatically jump to the home page</div>
10594
<script>
10695
if (navigator.language.startsWith("zh")) {
107-
document.getElementById("text").textContent = "登录成功,3秒后将自动跳转到首页"
96+
document.getElementById("text").textContent = "登录失败,3秒后将自动跳转到首页"
10897
}
10998
11099
setTimeout(() => {
111100
location.href = "${Website}";
112101
}, 3000);
113102
</script>
114-
</html>
115-
`;
116-
}
117-
118-
private static get failedHTML(): string {
119-
return `<!doctype html>
120-
<html lang="en">
121-
<head>
122-
<meta charset="UTF-8">
123-
<title>Login Failed</title>
124-
</head>
125-
<body>
126-
<svg style=max-width:80px;max-height:80px;position:fixed;top:50%;left:50%;transform:translate(-50%,-50%) viewBox="0 0 80 80" xmlns="http://www.w3.org/2000/svg">
127-
<path d="M40 0c22.0914 0 40 17.9086 40 40S62.0914 80 40 80 0 62.0914 0 40 17.9086 0 40 0zm0 4C20.1177 4 4 20.1177 4 40s16.1177 36 36 36 36-16.1177 36-36S59.8823 4 40 4zm21.0572 49.2345l.357.3513-2.8284 2.8284c-10.162-10.162-26.5747-10.2636-36.8617-.3048l-.3099.3048-2.8284-2.8284c11.7085-11.7085 30.619-11.8256 42.4714-.3513zM27 26c2.2091 0 4 1.7909 4 4 0 2.2091-1.7909 4-4 4-2.2091 0-4-1.7909-4-4 0-2.2091 1.7909-4 4-4zm26 0c2.2091 0 4 1.7909 4 4 0 2.2091-1.7909 4-4 4-2.2091 0-4-1.7909-4-4 0-2.2091 1.7909-4 4-4z" fill="#F45454" fill-rule="nonzero" />
128-
</svg>
129-
<div id="text" style=position:fixed;top:60%;left:50%;transform:translate(-50%,-50%)>Login Failed</div>
130-
<script>
131-
if (navigator.language.startsWith("zh")) {
132-
document.getElementById("text").textContent = "登录失败"
133-
}
134-
</script>
135103
</body>
136104
</html>
137105
`;
@@ -142,6 +110,5 @@ interface RequestType {
142110
querystring: {
143111
state: string;
144112
code: string;
145-
loginId: string;
146113
};
147114
}

src/v1/controller/login/agora/Check.ts

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/v1/controller/login/platforms/LoginAgora.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,6 @@ export class LoginAgora extends AbstractLogin {
7474
const token = await LoginAgora.getToken(code);
7575
return await LoginAgora.getUserInfoByAPI(token);
7676
}
77-
78-
public static async checkLoginID(loginID: string): Promise<boolean> {
79-
const result = await ax.post<{ isValid: boolean }>(
80-
"https://sso-power.agora.io/api/v1/visitor/check-login-id",
81-
{
82-
loginId: loginID,
83-
},
84-
{
85-
headers: {
86-
Authorization: Buffer.from(
87-
`${AgoraLogin.clientId}:${AgoraLogin.clientSecret}`,
88-
).toString("base64"),
89-
},
90-
},
91-
);
92-
93-
return result.data.isValid || false;
94-
}
9577
}
9678

9779
interface RegisterService {

0 commit comments

Comments
 (0)