fix: error message for login and registration

This commit is contained in:
chenos 2021-04-12 18:06:10 +08:00
parent 3612689035
commit 214b227a6c
3 changed files with 59 additions and 33 deletions

View File

@ -1,5 +1,5 @@
import React from 'react'; import React from 'react';
import { Tooltip, Card, Button } from 'antd'; import { Tooltip, Card, Button, message } from 'antd';
import { import {
SchemaForm, SchemaForm,
SchemaMarkupField as Field, SchemaMarkupField as Field,
@ -43,8 +43,8 @@ export function Login(props: any) {
<SchemaForm <SchemaForm
onSubmit={async values => { onSubmit={async values => {
console.log(values); try {
const { data = {} } = await request('/users:login', { const { data = {}, error } = await request('/users:login', {
method: 'post', method: 'post',
data: values, data: values,
}); });
@ -58,6 +58,11 @@ export function Login(props: any) {
await (window as any).routesReload(); await (window as any).routesReload();
history.push(redirect || '/'); history.push(redirect || '/');
} }
} catch (error) {
if (typeof error.data === 'string') {
message.error(error.data);
}
}
}} }}
actions={actions} actions={actions}
schema={{ schema={{

View File

@ -72,6 +72,7 @@ export function Register(props: any) {
}} }}
onSubmit={async values => { onSubmit={async values => {
console.log(values); console.log(values);
try {
const { data = {} } = await request('/users:register', { const { data = {} } = await request('/users:register', {
method: 'post', method: 'post',
data: values, data: values,
@ -84,6 +85,11 @@ export function Register(props: any) {
setTimeout(() => { setTimeout(() => {
history.push('/login'); history.push('/login');
}, 1000); }, 1000);
} catch (error) {
if (typeof error.data === 'string') {
message.error(error.data);
}
}
}} }}
actions={actions} actions={actions}
schema={{ schema={{

View File

@ -16,6 +16,9 @@ export async function check(ctx: actions.Context, next: actions.Next) {
export async function login(ctx: actions.Context, next: actions.Next) { export async function login(ctx: actions.Context, next: actions.Next) {
const { uniqueField = 'email', values } = ctx.action.params; const { uniqueField = 'email', values } = ctx.action.params;
// console.log(values); // console.log(values);
if (!values[uniqueField]) {
ctx.throw(401, '请填写邮箱账号');
}
const User = ctx.db.getModel('users'); const User = ctx.db.getModel('users');
const user = await User.findOne({ const user = await User.findOne({
where: { where: {
@ -23,11 +26,11 @@ export async function login(ctx: actions.Context, next: actions.Next) {
}, },
}); });
if (!user) { if (!user) {
ctx.throw(401, 'Unauthorized'); ctx.throw(401, '邮箱账号未注册');
} }
const isValid = await PASSWORD.verify(values.password, user.password); const isValid = await PASSWORD.verify(values.password, user.password);
if (!isValid) { if (!isValid) {
ctx.throw(401, 'Unauthorized'); ctx.throw(401, '密码错误,请您重新输入');
} }
if (!user.token) { if (!user.token) {
user.token = cryptoRandomString({ length: 20 }); 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) { export async function register(ctx: actions.Context, next: actions.Next) {
const User = ctx.db.getModel('users'); const User = ctx.db.getModel('users');
const { values } = ctx.action.params; const { values } = ctx.action.params;
try {
const user = await User.create(values); const user = await User.create(values);
ctx.body = { ctx.body = {
data: user, 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(); await next();
} }
export async function lostpassword(ctx: actions.Context, next: actions.Next) { export async function lostpassword(ctx: actions.Context, next: actions.Next) {
const { values: { email } } = ctx.action.params; const { values: { email } } = ctx.action.params;
if (!email) {
ctx.throw(401, '请填写邮箱账号');
}
const User = ctx.db.getModel('users'); const User = ctx.db.getModel('users');
const user = await User.findOne({ const user = await User.findOne({
where: { where: {
@ -63,7 +78,7 @@ export async function lostpassword(ctx: actions.Context, next: actions.Next) {
}, },
}); });
if (!user) { if (!user) {
ctx.throw(401, 'Unauthorized'); ctx.throw(401, '邮箱账号未注册');
} }
user.reset_token = cryptoRandomString({ length: 20 }); user.reset_token = cryptoRandomString({ length: 20 });
await user.save(); await user.save();