0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

chore(core,locale): fix error messages of signing in with non-existing username (#1035)

* chore(core,locale): fix error messages

* chore(core,locale): add input parameter guard and fix comment

* chore(core,locale): add nonempty string guard for username
This commit is contained in:
Darcy Ye 2022-06-02 16:29:17 +08:00 committed by GitHub
parent 90ca76eeb5
commit 2e08fa3816
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 11 additions and 9 deletions

View file

@ -44,6 +44,7 @@ export const findUserByUsernameAndPassword = async (
password: string password: string
): Promise<User> => { ): Promise<User> => {
const user = await findUserByUsername(username); const user = await findUserByUsername(username);
assertThat(user, 'user.username_not_exists');
const { passwordEncrypted, passwordEncryptionMethod } = user; const { passwordEncrypted, passwordEncryptionMethod } = user;
assertThat(passwordEncrypted && passwordEncryptionMethod, 'session.invalid_sign_in_method'); assertThat(passwordEncrypted && passwordEncryptionMethod, 'session.invalid_sign_in_method');

View file

@ -10,7 +10,7 @@ import { DeletionError, UpdateError } from '@/errors/SlonikError';
const { table, fields } = convertToIdentifiers(Users); const { table, fields } = convertToIdentifiers(Users);
export const findUserByUsername = async (username: string) => export const findUserByUsername = async (username: string) =>
envSet.pool.one<User>(sql` envSet.pool.maybeOne<User>(sql`
select ${sql.join(Object.values(fields), sql`,`)} select ${sql.join(Object.values(fields), sql`,`)}
from ${table} from ${table}
where ${fields.username}=${username} where ${fields.username}=${username}

View file

@ -67,15 +67,14 @@ export default function sessionRoutes<T extends AnonymousRouter>(router: T, prov
'/session/sign-in/username-password', '/session/sign-in/username-password',
koaGuard({ koaGuard({
body: object({ body: object({
username: string(), username: string().nonempty(),
password: string(), password: string().nonempty(),
}), }),
}), }),
async (ctx, next) => { async (ctx, next) => {
const { username, password } = ctx.guard.body; const { username, password } = ctx.guard.body;
const type = 'SignInUsernamePassword'; const type = 'SignInUsernamePassword';
ctx.log(type, { username }); ctx.log(type, { username });
assertThat(password, 'session.insufficient_info');
const { id } = await findUserByUsernameAndPassword(username, password); const { id } = await findUserByUsernameAndPassword(username, password);
ctx.log(type, { userId: id }); ctx.log(type, { userId: id });

View file

@ -89,7 +89,7 @@ const translation = {
invalid_passcode: 'The passcode is invalid.', invalid_passcode: 'The passcode is invalid.',
invalid_connector_auth: 'The authorization is invalid.', invalid_connector_auth: 'The authorization is invalid.',
invalid_connector_request: 'The connector data is invalid.', invalid_connector_request: 'The connector data is invalid.',
request: 'Request error {{message}}', request: 'Request error: {{message}}',
unknown: 'Unknown error, please try again later.', unknown: 'Unknown error, please try again later.',
invalid_session: 'Session not found. Please go back and sign in again.', invalid_session: 'Session not found. Please go back and sign in again.',
}, },
@ -585,6 +585,7 @@ const errors = {
phone_exists_register: 'The phone number has been registered.', phone_exists_register: 'The phone number has been registered.',
invalid_email: 'Invalid email address.', invalid_email: 'Invalid email address.',
invalid_phone: 'Invalid phone number.', invalid_phone: 'Invalid phone number.',
username_not_exists: 'The username has not been registered yet.',
email_not_exists: 'The email address has not been registered yet.', email_not_exists: 'The email address has not been registered yet.',
phone_not_exists: 'The phone number has not been registered yet.', phone_not_exists: 'The phone number has not been registered yet.',
identity_not_exists: 'The social account has not been registered yet.', identity_not_exists: 'The social account has not been registered yet.',
@ -603,7 +604,7 @@ const errors = {
connector_id_mismatch: 'The connectorId is mismatched with session record.', connector_id_mismatch: 'The connectorId is mismatched with session record.',
connector_session_not_found: 'Connector session not found. Please go back and sign in again.', connector_session_not_found: 'Connector session not found. Please go back and sign in again.',
unauthorized: 'Please sign in first.', unauthorized: 'Please sign in first.',
unsupported_prompt_name: 'Unsupported prompt name', unsupported_prompt_name: 'Unsupported prompt name.',
}, },
connector: { connector: {
general: 'An unexpected error occurred in connector.', general: 'An unexpected error occurred in connector.',
@ -646,7 +647,7 @@ const errors = {
create_failed: 'Failed to create {{name}}.', create_failed: 'Failed to create {{name}}.',
not_exists: 'The {{name}} does not exist.', not_exists: 'The {{name}} does not exist.',
not_exists_with_id: 'The {{name}} with ID `{{id}}` does not exist.', not_exists_with_id: 'The {{name}} with ID `{{id}}` does not exist.',
not_found: 'The resource does not exist', not_found: 'The resource does not exist.',
}, },
}; };

View file

@ -89,7 +89,7 @@ const translation = {
invalid_passcode: '无效的验证码。', invalid_passcode: '无效的验证码。',
invalid_connector_auth: '登录失败。', invalid_connector_auth: '登录失败。',
invalid_connector_request: '无效的登录请求。', invalid_connector_request: '无效的登录请求。',
request: '请求错误:{{ message }}', request: '请求错误:{{ message }}', // All error messages end with '。'.
unknown: '未知错误,请稍后重试。', unknown: '未知错误,请稍后重试。',
invalid_session: '未找到有效的会话,请重新登录。', invalid_session: '未找到有效的会话,请重新登录。',
}, },
@ -579,6 +579,7 @@ const errors = {
phone_exists_register: '手机号码已被注册。', phone_exists_register: '手机号码已被注册。',
invalid_email: '邮箱地址不正确。', invalid_email: '邮箱地址不正确。',
invalid_phone: '手机号码不正确。', invalid_phone: '手机号码不正确。',
username_not_exists: '用户名尚未注册。',
email_not_exists: '邮箱地址尚未注册。', email_not_exists: '邮箱地址尚未注册。',
phone_not_exists: '手机号码尚未注册。', phone_not_exists: '手机号码尚未注册。',
identity_not_exists: '该社交账号尚未注册。', identity_not_exists: '该社交账号尚未注册。',
@ -638,7 +639,7 @@ const errors = {
create_failed: '创建 {{name}} 失败。', create_failed: '创建 {{name}} 失败。',
not_exists: '该 {{name}} 不存在。', not_exists: '该 {{name}} 不存在。',
not_exists_with_id: 'ID 为 `{{id}}` 的 {{name}} 不存在。', not_exists_with_id: 'ID 为 `{{id}}` 的 {{name}} 不存在。',
not_found: '该资源不存在', not_found: '该资源不存在',
}, },
}; };