Skip to content

Commit

Permalink
feat: add register with role selection
Browse files Browse the repository at this point in the history
  • Loading branch information
zzdhybthu committed Dec 1, 2024
1 parent 4d94238 commit 3056868
Show file tree
Hide file tree
Showing 3 changed files with 360 additions and 5 deletions.
120 changes: 120 additions & 0 deletions src/helpers/validate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import isEmail from "isemail";
import { client } from "..";
import { gql } from "graphql-request";

/**
* Email with `tsinghua` suffix
Expand All @@ -24,3 +26,121 @@ export const validatePassword = (password: string) => {
password
);
};





interface IValidate {
(value: string): boolean;
}

export const __ValidateEmail: IValidate = (value: string) => {
if (!value) {
return false;
}
return /^([0-9a-zA-Z_.-\u4e00-\u9fa5])+@([0-9a-zA-Z_.-])+\.([a-zA-Z]{2,8})$/.test(
value,
);
};

export const __ValidateStudentEmail: IValidate = (value: string) => {
if (!value) {
return false;
}
if (value.endsWith("@mails.tsinghua.edu.cn")) {
return __ValidateEmail(value);
} else {
return false;
}
};

export const __ValidateTeacherEmail: IValidate = (value: string) => {
if (!value) {
return false;
}
if (value.endsWith("@tsinghua.edu.cn")) {
return __ValidateEmail(value);
} else {
return false;
}
}

export const __ValidatePhone: IValidate = (value: string) => {
if (!value) {
return false;
}
return /^[0-9]+$/.test(value) && value.length === 11;
};

export const __ValidateStudentID: IValidate = (value: string) => {
if (!value) {
return false;
}
return /^[0-9]+$/.test(value) && value.length === 10;
};

export const __ValidateClass: IValidate = (value: string) => {
if (!value) {
return false;
}
return /^[\u4e00-\u9fa5]+[0-9]+$/.test(value);
};

export const __ValidateName: IValidate = (value: string) => {
if (!value) {
return false;
}
return /^[\u4e00-\u9fa5]+$/.test(value) || /^[a-zA-Z\s]+$/.test(value);
};

export const __ValidateUsername: IValidate = (value: string) => {
if (!value) {
return false;
}
return /^[a-zA-Z][a-zA-Z0-9]*$/.test(value);
};


interface IValidateAsync {
(value: string): Promise<boolean>;
}
export const __ValidateEmailRegistered: IValidateAsync = async (value: string) => {
const item: any = await client.request(
gql`
query GetUserByEmail($email: String!) {
users(where: { email: { _eq: $email } }) {
email
}
}
`,
{ email: value }
)
return item?.users?.length !== 0;
};
export const __ValidateStudentIDRegistered: IValidateAsync = async (value: string) => {
const item: any = await client.request(
gql`
query GetUserByStudentId($student_no: String!) {
users(where: { student_no: { _eq: $student_no } }) {
student_no
}
}
`,
{ student_no: value }
)
return item?.users?.length !== 0;
}
export const __ValidatePhoneRegistered: IValidateAsync = async (value: string) => {
const item: any = await client.request(
gql`
query GetUserByPhone($phone: String!) {
users(where: { phone: { _eq: $phone } }) {
phone
}
}
`,
{ phone: value }
)
return item?.users?.length !== 0;
}
13 changes: 13 additions & 0 deletions src/middlewares/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ export interface JwtVerifyPayload {
phone: string;
code: string; // hash加密后的验证码
}
export interface IRegister {
role: string;
verificationEmailCode?: string;
verificationEmailToken?: string;
verificationPhoneCode?: string;
verificationPhoneToken?: string;
studentID?: string;
name: string;
class_?: string;
depart?: string;
password: string;
username?: string;
}
export interface UserInfo {
uuid: string;
role: string;
Expand Down
Loading

0 comments on commit 3056868

Please sign in to comment.