From bcde0c6dc793db5ce67ebb444b6392fcd7c4c7f3 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Thu, 17 Oct 2019 20:37:16 -0600 Subject: [PATCH] Fix login errors and redirect to home after success --- app/views/login/controller.js | 6 ++++-- app/views/login/index.js | 22 ++++++++++++++++++++++ server/routes/account.js | 8 ++++---- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/app/views/login/controller.js b/app/views/login/controller.js index eba090f..8f84c4f 100644 --- a/app/views/login/controller.js +++ b/app/views/login/controller.js @@ -16,6 +16,7 @@ export class LoginController extends ViewController { }, loginError: '', createError: '', + loginMessage: '', createMessage: '', pageMessage: '', isChecking: false, @@ -47,7 +48,7 @@ export class LoginController extends ViewController { validateLogin () { const { __ } = this.i18n; - this.state.createError = ''; + this.state.loginError = ''; this.state.isChecking = true; this.emit('render', () => { @@ -60,7 +61,7 @@ export class LoginController extends ViewController { loginEmail, loginPassword, ].includes('')) { - this.state.createError = __('login.create_required_field_blank'); + this.state.loginError = __('login.login_required_field_blank'); this.state.isChecking = false; this.emit('render'); return; @@ -132,6 +133,7 @@ export class LoginController extends ViewController { return; } + this.appState.isLoggedIn = true; this.state.loginMessage = __(response.message); this.state.isChecking = false; this.clearLoginForm(); diff --git a/app/views/login/index.js b/app/views/login/index.js index a3136ba..4a91c3e 100644 --- a/app/views/login/index.js +++ b/app/views/login/index.js @@ -6,6 +6,28 @@ export const loginView = (state, emit, i18n) => { const controller = new LoginController(state, emit, i18n); const { __ } = controller.i18n; + if (controller.appState.isLoggedIn === true) { + setTimeout(() => { + controller.state.loginMessage = ''; + emit('pushState', '/') + }, 3000); + + return html``; + } + return html`
${ diff --git a/server/routes/account.js b/server/routes/account.js index 2a7e71e..751ddae 100644 --- a/server/routes/account.js +++ b/server/routes/account.js @@ -147,16 +147,16 @@ async function routes(fastify, options) { } }); - fastify.get('/api/account/login', async (request, reply) => { + fastify.post('/api/account/login', async (request, reply) => { const formDataIsValid = Account.loginDataIsValid(request.body); if (formDataIsValid !== true) { return reply.code(400).send(formDataIsValid); } const account = new Account(fastify.models.User); - const user = account.validateLogin(request.body.email, request.body.password); + const user = await account.validateLogin(request.body.email, request.body.password); - if (user.error !== true) { + if (user.error === true) { return reply.code(400).send(user); } @@ -173,7 +173,7 @@ async function routes(fastify, options) { }) .send({ error: false, - message: 'api.account_create_success', + message: 'api.account_login_success', }); });