0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

fix(console): generate user password (#3615)

This commit is contained in:
Xiao Yijun 2023-03-29 13:29:43 +08:00 committed by GitHub
parent ee51478618
commit bb9cfe55d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View file

@ -1,6 +1,5 @@
import { usernameRegEx } from '@logto/core-kit';
import type { User } from '@logto/schemas';
import { nanoid } from 'nanoid';
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
@ -15,6 +14,8 @@ import UserAccountInformation from '@/components/UserAccountInformation';
import useApi from '@/hooks/use-api';
import * as modalStyles from '@/scss/modal.module.scss';
import { createInitialPassword } from './utils';
type FormData = {
username: string;
name: string;
@ -49,7 +50,7 @@ function CreateForm({ onClose, onCreate }: Props) {
return;
}
const password = nanoid(8);
const password = createInitialPassword();
const createdUser = await api.post('api/users', { json: { ...data, password } }).json<User>();

View file

@ -0,0 +1,13 @@
import { customAlphabet } from 'nanoid';
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
const digits = '0123456789';
// Note: password requires a minimum of 8 characters and contains a mix of letters, numbers, and symbols.
export const createInitialPassword = () => {
const randomAlphabet = customAlphabet(alphabet);
const randomDigit = customAlphabet(digits);
const randomAlphabetOrDigit = customAlphabet(alphabet + digits);
return `${randomAlphabet(1)}${randomDigit(1)}${randomAlphabetOrDigit(6)}`;
};