Skip to content

Commit ea9e16a

Browse files
hyriousaderan
authored andcommitted
refactor: add {hasPassword: boolean} to login responses (#744)
1 parent 383726b commit ea9e16a

File tree

10 files changed

+43
-2
lines changed

10 files changed

+43
-2
lines changed

src/abstract/login/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { v4 } from "uuid";
1717
import { ServiceCloudStorageUserFiles } from "../../v1/service/cloudStorage/CloudStorageUserFiles";
1818
import { ServiceUserPhone } from "../../v1/service/user/UserPhone";
1919
import { FileConvertStep, FileResourceType } from "../../model/cloudStorage/Constants";
20+
import { ServiceUser } from "../../v1/service/user/User";
2021

2122
export abstract class AbstractLogin {
2223
protected readonly userUUID: string;
@@ -49,6 +50,7 @@ export abstract class AbstractLogin {
4950
...userInfo,
5051
userUUID: this.userUUID,
5152
hasPhone: await ServiceUserPhone.exist(this.userUUID),
53+
hasPassword: await ServiceUser.hasPassword(this.userUUID),
5254
}),
5355
60 * 60,
5456
);

src/v1/controller/login/Login.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export class Login extends AbstractController<RequestType, ResponseType> {
9090
}),
9191
userUUID: this.userUUID,
9292
hasPhone: await this.svc.userPhone.exist(),
93+
hasPassword: await this.svc.user.hasPassword(),
9394
},
9495
};
9596
}
@@ -138,4 +139,5 @@ interface ResponseType {
138139
token: string;
139140
userUUID: string;
140141
hasPhone: boolean;
142+
hasPassword: boolean;
141143
}

src/v1/controller/login/Process.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export class LoginProcess extends AbstractController<RequestType, ResponseType>
6060
userUUID: "",
6161
token: "",
6262
hasPhone: false,
63+
hasPassword: false,
6364
},
6465
};
6566
}
@@ -89,4 +90,5 @@ type ResponseType = {
8990
userUUID: string;
9091
token: string;
9192
hasPhone: boolean;
93+
hasPassword: boolean;
9294
};

src/v1/controller/login/apple/jwt.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { v4 } from "uuid";
88
import { LoginPlatform, Status } from "../../../../constants/Project";
99
import { Apple } from "../../../../constants/Config";
1010
import { ServiceUserPhone } from "../../../service/user/UserPhone";
11+
import { ServiceUser } from "../../../service/user/User";
1112

1213
@Controller<RequestType, ResponseType>({
1314
method: "post",
@@ -78,6 +79,7 @@ export class AppleJWT extends AbstractController<RequestType, ResponseType> {
7879
loginSource: LoginPlatform.Apple,
7980
}),
8081
hasPhone: await ServiceUserPhone.exist(this.userUUID),
82+
hasPassword: await ServiceUser.hasPassword(this.userUUID),
8183
},
8284
};
8385
}
@@ -100,4 +102,5 @@ interface ResponseType {
100102
userUUID: string;
101103
token: string;
102104
hasPhone: boolean;
105+
hasPassword: boolean;
103106
}

src/v1/controller/login/phone/Phone.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export class PhoneLogin extends AbstractController<RequestType, ResponseType> {
7979
loginSource: LoginPlatform.Phone,
8080
}),
8181
hasPhone: true,
82+
hasPassword: await loginPhone.svc.user.hasPassword(),
8283
},
8384
} as const;
8485

@@ -145,4 +146,5 @@ interface ResponseType {
145146
userUUID: string;
146147
token: string;
147148
hasPhone: true;
149+
hasPassword: boolean;
148150
}

src/v1/controller/login/weChat/mobile/Callback.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Status } from "../../../../../constants/Project";
88
import { parseError } from "../../../../../logger";
99
import { WeChat } from "../../../../../constants/Config";
1010
import { ServiceUserPhone } from "../../../../service/user/UserPhone";
11+
import { ServiceUser } from "../../../../service/user/User";
1112

1213
@Controller<RequestType, ResponseType>({
1314
method: "get",
@@ -42,6 +43,7 @@ export class WechatMobileCallback extends AbstractController<RequestType, Respon
4243
data: {
4344
...result,
4445
hasPhone: await ServiceUserPhone.exist(result.userUUID),
46+
hasPassword: await ServiceUser.hasPassword(result.userUUID),
4547
},
4648
};
4749
}
@@ -66,4 +68,5 @@ interface ResponseType {
6668
userUUID: string;
6769
token: string;
6870
hasPhone: boolean;
71+
hasPassword: boolean;
6972
}

src/v1/service/user/User.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ import { UpdateResult } from "typeorm/query-builder/result/UpdateResult";
88
export class ServiceUser {
99
constructor(private readonly userUUID: string) {}
1010

11+
public async hasPassword(): Promise<boolean> {
12+
return await ServiceUser.hasPassword(this.userUUID);
13+
}
14+
15+
public static async hasPassword(userUUID: string): Promise<boolean> {
16+
const result = await UserDAO().findOne(["user_password"], {
17+
user_uuid: userUUID,
18+
});
19+
20+
return Boolean(result && result.user_password);
21+
}
22+
1123
public async create(
1224
data: {
1325
userName: string;

src/v2/services/user/email.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,13 @@ export class UserEmailService {
120120

121121
await Promise.all([createUser, createUserEmail, setupGuidePPTX]);
122122

123-
const result = {
123+
const result: EmailRegisterReturn = {
124124
name: userName,
125125
avatarURL: "",
126126
userUUID,
127127
token: await jwtSign(userUUID),
128128
hasPhone: false,
129+
hasPassword: true,
129130
};
130131

131132
await UserEmailService.clearVerificationCode(email);
@@ -200,6 +201,7 @@ export class UserEmailService {
200201
userUUID: userUUIDByEmail,
201202
token: await jwtSign(userUUIDByEmail),
202203
hasPhone: await this.hasPhone(userUUIDByEmail),
204+
hasPassword: true,
203205
};
204206
}
205207

@@ -308,6 +310,7 @@ export type EmailRegisterReturn = {
308310
userUUID: string;
309311
token: string;
310312
hasPhone: boolean;
313+
hasPassword: boolean;
311314
};
312315

313316
export type EmailLoginReturn = EmailRegisterReturn;

src/v2/services/user/phone.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,13 @@ export class UserPhoneService {
117117

118118
await Promise.all([createUser, createUserPhone, setupGuidePPTX]);
119119

120-
const result = {
120+
const result: PhoneRegisterReturn = {
121121
name: userName,
122122
avatarURL: "",
123123
userUUID,
124124
token: await jwtSign(userUUID),
125125
hasPhone: true,
126+
hasPassword: true,
126127
};
127128

128129
await UserPhoneService.clearVerificationCode(safePhone);
@@ -200,6 +201,7 @@ export class UserPhoneService {
200201
userUUID: userUUIDByPhone,
201202
token: await jwtSign(userUUIDByPhone),
202203
hasPhone: true,
204+
hasPassword: true,
203205
};
204206
}
205207

@@ -272,6 +274,7 @@ export type PhoneRegisterReturn = {
272274
userUUID: string;
273275
token: string;
274276
hasPhone: boolean;
277+
hasPassword: boolean;
275278
};
276279

277280
export type PhoneLoginReturn = PhoneRegisterReturn;

src/v2/services/user/rebind-phone.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ export class UserRebindPhoneService {
172172
userUUID: original.user_uuid,
173173
token: await jwtSign(original.user_uuid),
174174
hasPhone: true,
175+
hasPassword: await this.hasPassword(original.user_uuid),
175176
rebind: status,
176177
};
177178
} else {
@@ -194,6 +195,13 @@ export class UserRebindPhoneService {
194195
return elapsedTime > MessageIntervalSecond;
195196
}
196197

198+
private async hasPassword(userUUID: string): Promise<boolean> {
199+
const user = await userDAO.findOne(this.DBTransaction, ["user_password"], {
200+
user_uuid: userUUID,
201+
});
202+
return Boolean(user?.user_password);
203+
}
204+
197205
private async tryUpdate(
198206
dao: DAO<UserPlatform>,
199207
user_uuid: string,
@@ -275,5 +283,6 @@ export type UserRebindReturn = {
275283
token: string;
276284
userUUID: string;
277285
hasPhone: boolean;
286+
hasPassword: boolean;
278287
rebind: RebindStatus;
279288
};

0 commit comments

Comments
 (0)