Skip to content

Commit bfb0124

Browse files
committed
feat(v2): add register/phone{/send-message} (#743)
feat(v2): add login/phone feat(v2): add user/password feat(v2): add reset/phone{/send-password} add unit test move mysql.salt to login.salt
1 parent 0413f94 commit bfb0124

File tree

25 files changed

+885
-9
lines changed

25 files changed

+885
-9
lines changed

config/defaults.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ oauth:
7979
- jpeg
8080

8181
login:
82+
salt:
8283
wechat:
8384
web:
8485
enable: false

config/test.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ oauth:
7474
- jpeg
7575

7676
login:
77+
salt: test
7778
wechat:
7879
web:
7980
enable: true

src/ErrorCode.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export enum ErrorCode {
2525
UserNotFound = 400000, // user not found
2626
UserRoomListNotEmpty, // user room list is not empty.
2727
UserAlreadyBinding, // user already binding
28+
UserPasswordIncorrect, // user password (for update) incorrect
2829

2930
RecordNotFound = 500000, // record info not found
3031

src/abstract/login/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export abstract class AbstractLogin {
108108
]);
109109
}
110110

111-
private static get guidePPTX(): string {
111+
public static get guidePPTX(): string {
112112
return "guide-pptx/guide.pptx";
113113
}
114114
}

src/constants/Config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export const MySQL = {
3131
db: config.mysql.db,
3232
};
3333

34+
export const Salt = config.login.salt;
35+
3436
export const Website = config.website;
3537

3638
export const WeChat = {

src/plugins/Ajv.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ const directoryPath: FormatDefinition<string> = {
101101
},
102102
};
103103

104+
const password: FormatDefinition<string> = {
105+
validate: str => {
106+
// 8..32 characters, at least one letter and one number
107+
return 8 <= str.length && str.length <= 32 && /[a-z]/i.test(str) && /\d/.test(str);
108+
},
109+
};
110+
104111
export const ajvSelfPlugin = (ajv: Ajv): void => {
105112
ajv.addFormat("unix-timestamp", unixTimestamp);
106113
ajv.addFormat("uuid-v4", uuidV4);
@@ -113,6 +120,7 @@ export const ajvSelfPlugin = (ajv: Ajv): void => {
113120
ajv.addFormat("phone", phone);
114121
ajv.addFormat("directory-name", directoryName);
115122
ajv.addFormat("directory-path", directoryPath);
123+
ajv.addFormat("password", password);
116124
};
117125

118126
export const validateDirectoryName = (str: string): void => {

src/utils/Hash.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { createHash } from "crypto";
2+
import { Salt } from "../constants/Config";
3+
4+
export function hash(data: string): string {
5+
return createHash("md5").update(data).update(Salt).digest("hex");
6+
}

src/utils/ParseConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ type Config = {
8989
};
9090
};
9191
login: {
92+
salt: string;
9293
wechat: {
9394
web: {
9495
enable: boolean;

src/utils/Redis.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,36 @@ export const RedisKey = {
22
authUUID: (uuid: string): string => `auth:uuid:${uuid}`,
33
authFailed: (authUUID: string): string => `auth:failed:${authUUID}`,
44
authUserInfo: (authUUID: string): string => `auth:userInfo:${authUUID}`,
5+
56
bindingAuthUUID: (uuid: string): string => `binding:auth:uuid:${uuid}`,
67
bindingAuthStatus: (authUUID: string): string => `binding:auth:status:${authUUID}`,
8+
79
agoraRTCRoomUserToken: (roomUUID: string, uid: string | number): string =>
810
`agora:rtc:room:${roomUUID}:uid:${uid}`,
911
agoraRTMUserToken: (userUUID: string): string => `agora:rtm:userUUID:${userUUID}`,
12+
1013
cloudStorageFileInfo: (userUUID: string, fileUUID: string): string =>
1114
`cloudStorage:${userUUID}:${fileUUID}`,
1215
cloudStorageTempPhotoInfo: (userUUID: string, fileUUID: string): string =>
1316
`cloudStorage:tempPhoto:${userUUID}:${fileUUID}`,
17+
1418
userAvatarFileInfo: (userUUID: string, fileUUID: string): string =>
1519
`user:avatar:${userUUID}:${fileUUID}`,
20+
1621
roomInviteCode: (inviteCode: string): string => `room:invite:${inviteCode}`,
1722
roomInviteCodeReverse: (roomUUID: string): string => `room:inviteReverse:${roomUUID}`,
23+
1824
phoneLogin: (phone: string): string => `phone:login:${phone}`,
1925
phoneTryLoginCount: (phone: string): string => `phone:count:login:${phone}`,
26+
2027
phoneBinding: (phone: string): string => `phone:binding:${phone}`,
2128
phoneTryBindingCount: (phone: string): string => `phone:count:binding:${phone}`,
29+
2230
userDelete: (userUUID: string): string => `user:delete:${userUUID}`,
31+
2332
videoIllegalCount: (roomUUID: string): string => `illegal:video:${roomUUID}`,
2433
voiceIllegalCount: (roomUUID: string): string => `illegal:voice:${roomUUID}`,
34+
2535
oauthLogoFileInfo: (oauthUUID: string, fileUUID: string): string =>
2636
`oauth:logo:${oauthUUID}:${fileUUID}`,
2737
oauthAccessToken: (accessToken: string): string => `oauth:accessToken:${accessToken}`,
@@ -36,4 +46,7 @@ export const RedisKey = {
3646
`oauth:authorize:refreshToken:${refreshToken}`,
3747
oauthAuthorizeTokenByUserUUID: (clientID: string, userUUID: string) =>
3848
`oauth:authorize:clientID:${clientID}:user:${userUUID}`,
49+
50+
phoneRegisterOrReset: (phone: string): string => `phone:register:${phone}`,
51+
phoneTryRegisterOrResetCount: (phone: string): string => `phone:count:register:${phone}`,
3952
};

src/v2/__tests__/helpers/db/user.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,24 @@ export class CreateUser {
2121
return info;
2222
}
2323

24-
public async quick() {
25-
const info = {
26-
userUUID: v4(),
27-
userName: v4(),
28-
userPassword: v4(),
29-
avatarURL: v4(),
24+
public async quick(
25+
info: {
26+
userUUID?: string;
27+
userName?: string;
28+
userPassword?: string;
29+
avatarURL?: string;
30+
} = {},
31+
) {
32+
const fullInfo = {
33+
userUUID: info.userUUID || v4(),
34+
userName: info.userName || v4(),
35+
userPassword: info.userPassword ?? v4(),
36+
avatarURL: info.avatarURL || v4(),
3037
};
3138

32-
await this.full(info);
39+
await this.full(fullInfo);
3340

34-
return info;
41+
return fullInfo;
3542
}
3643

3744
public async fixedName(userName: string) {

0 commit comments

Comments
 (0)