From 214b227a6c1fe92bf54968e369aeaeabb8f73d7a Mon Sep 17 00:00:00 2001 From: chenos Date: Mon, 12 Apr 2021 18:06:10 +0800 Subject: [PATCH] fix: error message for login and registration --- packages/app/src/components/views/Login.tsx | 33 +++++++++++-------- .../app/src/components/views/Register.tsx | 30 ++++++++++------- packages/plugin-users/src/actions/users.ts | 29 ++++++++++++---- 3 files changed, 59 insertions(+), 33 deletions(-) diff --git a/packages/app/src/components/views/Login.tsx b/packages/app/src/components/views/Login.tsx index 576a4e6e3..3599e45ba 100644 --- a/packages/app/src/components/views/Login.tsx +++ b/packages/app/src/components/views/Login.tsx @@ -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) { { - 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} diff --git a/packages/app/src/components/views/Register.tsx b/packages/app/src/components/views/Register.tsx index ad2f0f9fc..f757ddd60 100644 --- a/packages/app/src/components/views/Register.tsx +++ b/packages/app/src/components/views/Register.tsx @@ -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={{ diff --git a/packages/plugin-users/src/actions/users.ts b/packages/plugin-users/src/actions/users.ts index bde5d20f3..9665c76f7 100644 --- a/packages/plugin-users/src/actions/users.ts +++ b/packages/plugin-users/src/actions/users.ts @@ -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();