We have gathered all the essential ingredients in the previous sections, and as soon as we have the MySQL connection pool created, we can refactor and improve our login code from Chapter 10, Adding a Vuex Store, and Chapter 11, Writing Route Middlewares and Server Middlewares, in the following steps:
- Import all the dependencies, such as koa-router, jsonwebtoken, bcryptjs, and the MySQL connection pool for the login route as follows:
// src/modules/public/login/_routes/local.js
import Router from 'koa-router'
import jwt from 'jsonwebtoken'
import bcrypt from 'bcrypt'
import pool from 'core/database/mysql'
import config from 'config'
const router = new Router()
router.post('/login', async (ctx, next) => {
let request = ctx.request.body || {}
//...
})
export default router
We imported the config file here for the configuration options of our API, which contains the MySQL database...