fix: error message for login and registration
This commit is contained in:
parent
3612689035
commit
214b227a6c
@ -1,5 +1,5 @@
|
||||
import React from 'react';
|
||||
import { Tooltip, Card, Button } from 'antd';
|
||||
import { Tooltip, Card, Button, message } from 'antd';
|
||||
import {
|
||||
SchemaForm,
|
||||
SchemaMarkupField as Field,
|
||||
@ -43,8 +43,8 @@ export function Login(props: any) {
|
||||
|
||||
<SchemaForm
|
||||
onSubmit={async values => {
|
||||
console.log(values);
|
||||
const { data = {} } = await request('/users:login', {
|
||||
try {
|
||||
const { data = {}, error } = await request('/users:login', {
|
||||
method: 'post',
|
||||
data: values,
|
||||
});
|
||||
@ -58,6 +58,11 @@ export function Login(props: any) {
|
||||
await (window as any).routesReload();
|
||||
history.push(redirect || '/');
|
||||
}
|
||||
} catch (error) {
|
||||
if (typeof error.data === 'string') {
|
||||
message.error(error.data);
|
||||
}
|
||||
}
|
||||
}}
|
||||
actions={actions}
|
||||
schema={{
|
||||
|
@ -72,6 +72,7 @@ export function Register(props: any) {
|
||||
}}
|
||||
onSubmit={async values => {
|
||||
console.log(values);
|
||||
try {
|
||||
const { data = {} } = await request('/users:register', {
|
||||
method: 'post',
|
||||
data: values,
|
||||
@ -84,6 +85,11 @@ export function Register(props: any) {
|
||||
setTimeout(() => {
|
||||
history.push('/login');
|
||||
}, 1000);
|
||||
} catch (error) {
|
||||
if (typeof error.data === 'string') {
|
||||
message.error(error.data);
|
||||
}
|
||||
}
|
||||
}}
|
||||
actions={actions}
|
||||
schema={{
|
||||
|
@ -16,6 +16,9 @@ export async function check(ctx: actions.Context, next: actions.Next) {
|
||||
export async function login(ctx: actions.Context, next: actions.Next) {
|
||||
const { uniqueField = 'email', values } = ctx.action.params;
|
||||
// console.log(values);
|
||||
if (!values[uniqueField]) {
|
||||
ctx.throw(401, '请填写邮箱账号');
|
||||
}
|
||||
const User = ctx.db.getModel('users');
|
||||
const user = await User.findOne({
|
||||
where: {
|
||||
@ -23,11 +26,11 @@ export async function login(ctx: actions.Context, next: actions.Next) {
|
||||
},
|
||||
});
|
||||
if (!user) {
|
||||
ctx.throw(401, 'Unauthorized');
|
||||
ctx.throw(401, '邮箱账号未注册');
|
||||
}
|
||||
const isValid = await PASSWORD.verify(values.password, user.password);
|
||||
if (!isValid) {
|
||||
ctx.throw(401, 'Unauthorized');
|
||||
ctx.throw(401, '密码错误,请您重新输入');
|
||||
}
|
||||
if (!user.token) {
|
||||
user.token = cryptoRandomString({ length: 20 });
|
||||
@ -47,15 +50,27 @@ export async function logout(ctx: actions.Context, next: actions.Next) {
|
||||
export async function register(ctx: actions.Context, next: actions.Next) {
|
||||
const User = ctx.db.getModel('users');
|
||||
const { values } = ctx.action.params;
|
||||
try {
|
||||
const user = await User.create(values);
|
||||
ctx.body = {
|
||||
data: user,
|
||||
};
|
||||
} catch (error) {
|
||||
if (error.errors) {
|
||||
console.log(error.errors.map(data => data.message));
|
||||
ctx.throw(401, error.errors.map(data => data.message).join(', '));
|
||||
} else {
|
||||
ctx.throw(401, '注册失败');
|
||||
}
|
||||
}
|
||||
await next();
|
||||
}
|
||||
|
||||
export async function lostpassword(ctx: actions.Context, next: actions.Next) {
|
||||
const { values: { email } } = ctx.action.params;
|
||||
if (!email) {
|
||||
ctx.throw(401, '请填写邮箱账号');
|
||||
}
|
||||
const User = ctx.db.getModel('users');
|
||||
const user = await User.findOne({
|
||||
where: {
|
||||
@ -63,7 +78,7 @@ export async function lostpassword(ctx: actions.Context, next: actions.Next) {
|
||||
},
|
||||
});
|
||||
if (!user) {
|
||||
ctx.throw(401, 'Unauthorized');
|
||||
ctx.throw(401, '邮箱账号未注册');
|
||||
}
|
||||
user.reset_token = cryptoRandomString({ length: 20 });
|
||||
await user.save();
|
||||
|
Loading…
Reference in New Issue
Block a user