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 { Tooltip, Card, Button } from 'antd';
import { Tooltip, Card, Button, message } from 'antd';
import {
SchemaForm,
SchemaMarkupField as Field,
@ -43,20 +43,25 @@ export function Login(props: any) {
<SchemaForm
onSubmit={async values => {
console.log(values);
const { data = {} } = await request('/users:login', {
method: 'post',
data: values,
});
if (data.data && data.data.token) {
localStorage.setItem('NOCOBASE_TOKEN', data.data.token);
// @ts-ignore
setInitialState({
...initialState,
currentUser: data.data,
try {
const { data = {}, error } = await request('/users:login', {
method: 'post',
data: values,
});
await (window as any).routesReload();
history.push(redirect || '/');
if (data.data && data.data.token) {
localStorage.setItem('NOCOBASE_TOKEN', data.data.token);
// @ts-ignore
setInitialState({
...initialState,
currentUser: data.data,
});
await (window as any).routesReload();
history.push(redirect || '/');
}
} catch (error) {
if (typeof error.data === 'string') {
message.error(error.data);
}
}
}}
actions={actions}

View File

@ -72,18 +72,24 @@ export function Register(props: any) {
}}
onSubmit={async values => {
console.log(values);
const { data = {} } = await request('/users:register', {
method: 'post',
data: values,
});
await actions.reset({
validate: false,
forceClear: true,
});
message.success('注册成功,将跳转登录页');
setTimeout(() => {
history.push('/login');
}, 1000);
try {
const { data = {} } = await request('/users:register', {
method: 'post',
data: values,
});
await actions.reset({
validate: false,
forceClear: true,
});
message.success('注册成功,将跳转登录页');
setTimeout(() => {
history.push('/login');
}, 1000);
} catch (error) {
if (typeof error.data === 'string') {
message.error(error.data);
}
}
}}
actions={actions}
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) {
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;
const user = await User.create(values);
ctx.body = {
data: user,
};
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();