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,20 +43,25 @@ 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,
});
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(); if (data.data && data.data.token) {
history.push(redirect || '/'); 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} actions={actions}

View File

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