diff --git a/readme.md b/readme.md index f2a596f..230d983 100644 --- a/readme.md +++ b/readme.md @@ -28,7 +28,7 @@ This is a simple SPA built using [Koa](http://koajs.com/) (2.3) as the backend a - Prettier - Babel - PM2 -- MySQL +- MySQL with Knex - log4js - And more... @@ -41,11 +41,14 @@ npm install # serve using nodemon with hot reload npm run watch -# build for production with prettier +# build for production with prettier and babel npm run build # serve in production using the pm2 ecosystem.json file npm run start-production + +# run prettier on the project +npm run pretty ``` ## General Information @@ -67,7 +70,7 @@ As mentioned in the frontend code, the user authentication process is this: - User create an account - User logs in - The server sends and `accessToken` and a `refreshToken` back -- We take the `accessToken` and decoded it using `jwt-decode`. This gets us the logged in user's information. We stick this in the Vuex variable `user`. Then we store the `refreshToken` amd `accessToken`. +- We take the `accessToken` and decode it using `jwt-decode`. This gets us the logged in user's information. We stick this in the Vuex variable `user`. Then we store the `refreshToken` amd `accessToken`. - Each protected endpoint will be expecting you to attach the `accessToken` you have to the call (using Authentication: Bearer). After a short amount of time, the server will respond with `401 TOKEN EXPIRED`. When you see this - that means you need to send your `refreshToken` and `user.email` to the endpoint that deals with `accessToken` refreshing. Once you do that, you'll received a brand new `accessToken` and `refreshToken`. - Repeat the process as needed. @@ -79,11 +82,11 @@ The `src` folder is the heart of the program. I'll go over its subfolders now. ### controllers -We use controllers to keep our router thin. The controller's responsibility is to manage the request body and make sure it's nice and clean when it eventually gets sent to a `model` to make database calls. There are two controller files present - one for user signup/login/forgot... and one for notes. +We use controllers to keep our router thin. The controller's responsibility is to manage the request body and make sure it's nice and clean when it eventually gets sent to a `model` to make database calls. There are two controller files present - one for user signup/login/forgot... and one for notes. Note: the `UserActionController.js` is a little different then normal controllers, as I believe the actions of a user signup/login/forgot/reset are seperate from the normal actions for a user - so that's why `UserActionController.js` in written in a more *functional* way. ### db -Here is our database setup. This project uses Knex to manage migarations and execute queries. I initially wrote raw SQL queries for the program, but the need for a migrations maanager pushed me towards an ORM for the MySQL database. Knex is awesome - very powerful, easy to use and make queries, and the migarations are nice to have for sure - especially for testing. +Here is our database setup. This project uses Knex to manage migrations and execute queries. I initially wrote wrote all the MySQL calls using raw SQL, but the need for a migrations maanager pushed me towards an ORM for the MySQL database. Knex is awesome - very powerful, easy to use and make queries, and the migrations are nice to have for sure - especially for testing. ### middleware @@ -97,10 +100,6 @@ Our models folder contains two model files - one for users and one for notes. Th Very simple - here are our routes. I've broken it down into two files - this keeps things in control. Each route is nice and thin - all it's doing is calling a controller. Some routes are using that jwt middleware I mentioned earlier. Koa make it really nice and easy to add middleware to a route. Very cool. -### sql - -I provided the sql code here you'll need when creating your database. I was hoping to find a nice database-migration library for node - but unfortunately I came up empty-handed. I tried TJ's - but I think it's not really supported anymore - didn't work for me using the latest Node and ES6/7 syntax. I'm keeping my eye out for one - let me know if you use one you like. - ### static Static files - just used for the favicon. diff --git a/src/sql/koa_vue_notes_2017-08-04.sql b/src/sql/koa_vue_notes_2017-08-04.sql deleted file mode 100644 index ef6d2b1..0000000 --- a/src/sql/koa_vue_notes_2017-08-04.sql +++ /dev/null @@ -1,97 +0,0 @@ -# ************************************************************ -# Sequel Pro SQL dump -# Version 4541 -# -# http://www.sequelpro.com/ -# https://github.com/sequelpro/sequelpro -# -# Host: 127.0.0.1 (MySQL 5.7.17-0ubuntu0.16.04.2) -# Database: koa_vue_notes -# Generation Time: 2017-08-05 02:14:52 +0000 -# ************************************************************ - - -/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; -/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; -/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; -/*!40101 SET NAMES utf8 */; -/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; -/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; -/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; - - -# Dump of table koa_vue_notes_notes -# ------------------------------------------------------------ - -DROP TABLE IF EXISTS `koa_vue_notes_notes`; - -CREATE TABLE `koa_vue_notes_notes` ( - `id` int(11) unsigned NOT NULL AUTO_INCREMENT, - `userId` int(11) DEFAULT NULL, - `title` varchar(255) DEFAULT NULL, - `content` mediumtext, - `ipAddress` varchar(255) DEFAULT NULL, - `updatedAt` timestamp NULL DEFAULT NULL, - `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; - - - -# Dump of table koa_vue_notes_refresh_tokens -# ------------------------------------------------------------ - -DROP TABLE IF EXISTS `koa_vue_notes_refresh_tokens`; - -CREATE TABLE `koa_vue_notes_refresh_tokens` ( - `id` int(11) unsigned NOT NULL AUTO_INCREMENT, - `username` varchar(255) DEFAULT NULL, - `refreshToken` varchar(255) DEFAULT NULL, - `info` varchar(255) DEFAULT NULL, - `isValid` tinyint(1) NOT NULL DEFAULT '1', - `expiration` timestamp NULL DEFAULT NULL, - `ipAddress` varchar(255) DEFAULT NULL, - `updatedAt` timestamp NULL DEFAULT NULL, - `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; - - - -# Dump of table koa_vue_notes_users -# ------------------------------------------------------------ - -DROP TABLE IF EXISTS `koa_vue_notes_users`; - -CREATE TABLE `koa_vue_notes_users` ( - `id` int(11) unsigned NOT NULL AUTO_INCREMENT, - `token` varchar(255) DEFAULT NULL, - `firstName` varchar(255) DEFAULT NULL, - `lastName` varchar(255) DEFAULT NULL, - `username` varchar(255) NOT NULL, - `email` varchar(255) DEFAULT '', - `password` varchar(100) DEFAULT '', - `passwordResetToken` varchar(100) DEFAULT NULL, - `passwordResetExpiration` timestamp NULL DEFAULT NULL, - `sendPromotionalEmails` tinyint(1) NOT NULL DEFAULT '0', - `isAdmin` tinyint(1) NOT NULL DEFAULT '0', - `isDeleted` tinyint(11) NOT NULL DEFAULT '0', - `loginCount` int(11) NOT NULL DEFAULT '0', - `ipAddress` varchar(255) DEFAULT NULL, - `updatedAt` timestamp NULL DEFAULT NULL, - `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY (`id`), - UNIQUE KEY `username` (`username`), - UNIQUE KEY `email` (`email`), - UNIQUE KEY `token` (`token`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; - - - - -/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; -/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; -/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; -/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; -/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; -/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;