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

fix user create bug #788

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions client/src/http/BaseModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export default class BaseModel {
static async create<T>(options: updateParamsType) {
const { path, data } = options;
const res = await http.post(path, data);
const code = res?.data?.data?.error_code;
if (code !== 'Success') {
throw new Error(res?.data?.data?.detail || res?.data?.data?.reason || 'create failed');
}
return (res.data.data || {}) as T;
}

Expand Down
22 changes: 13 additions & 9 deletions client/src/pages/user/User.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,20 @@ const Users = () => {
} = usePaginationHook(users || []);

const handleCreate = async (data: CreateUserParams) => {
await UserService.createUser(data);
// assign user role if
await UserService.updateUserRole({
username: data.username,
roles: data.roles,
});
try {
await UserService.createUser(data);
// assign user role if
await UserService.updateUserRole({
username: data.username,
roles: data.roles,
});

fetchUsers();
openSnackBar(successTrans('create', { name: userTrans('user') }));
handleCloseDialog();
fetchUsers();
openSnackBar(successTrans('create', { name: userTrans('user') }));
handleCloseDialog();
} catch (error: any) {
openSnackBar(error.message, 'error');
}
};

const onUpdate = async (data: UpdateUserRoleParams) => {
Expand Down
8 changes: 6 additions & 2 deletions server/src/users/dto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { IsString, IsOptional } from 'class-validator';
import { IsString, IsOptional,MinLength,MaxLength,Matches } from 'class-validator';

export class CreateUserDto {
@Matches(/^[a-zA-Z][a-zA-Z0-9_-]*$/, {
message: 'Username must start with a letter and can only contain letters, numbers, underscores, and hyphens'
})
@IsString({ message: 'username is required.' })
readonly username: string;

@MinLength(6, { message: 'Password must be at least 6 characters long' })
@MaxLength(72, { message: 'Password must not exceed 72 characters' })
@IsString({ message: 'password is required.' })
readonly password: string;
}
Expand Down
Loading