From 5130fba032197c6222608ada3e6a9276c5765ecd Mon Sep 17 00:00:00 2001 From: plane000 Date: Tue, 4 Sep 2018 20:06:22 +0100 Subject: [PATCH] login and signup endpoints complete, some auth framework in place for future --- src/app.js | 3 +- src/controllers/loginController.js | 17 +++++++- src/controllers/routes/router.js | 4 +- src/controllers/userController.js | 8 ++-- src/models/database/sqlite/database.sqlite | Bin 28672 -> 28672 bytes src/models/database/tokens.js | 43 +++++++++++++++++++++ src/models/logger.js | 8 +++- 7 files changed, 73 insertions(+), 10 deletions(-) diff --git a/src/app.js b/src/app.js index f9e035a..cfac678 100644 --- a/src/app.js +++ b/src/app.js @@ -10,7 +10,8 @@ import {User} from './models/user/user'; init(); async function init() { Logger.SetLevel(Logger.VERBOSE_LOGS); - + Logger.SetDialect('SQLITE'); + await Config.load(); await Database.init(); await Database.testConnection(); diff --git a/src/controllers/loginController.js b/src/controllers/loginController.js index 434d448..699a54e 100644 --- a/src/controllers/loginController.js +++ b/src/controllers/loginController.js @@ -27,9 +27,11 @@ export class LoginController extends ControllerHandler { if (!username /*If they're loging in with email*/) { user = await Database.users.getUser('email', email); if (user == -1) errors.addError(422, 'Unprocessable entity', 'There is no user with that email'); + username = user.username; } else { user = await Database.users.getUser('username', username); if (user == -1) errors.addError(422, 'Unprocessable entity', 'There is no user with that username'); + email = user.email; } if (errors.count() > 0) { @@ -48,7 +50,20 @@ export class LoginController extends ControllerHandler { return; } - res.end('Welcome') + let response = new API.user(res, user.id, username, email, new Date(parseInt(user.lastupdated)).toLocaleString()); + + let token = await Database.auth.getTokenByID(user.id); + + if (token == -1) { + let encryptedPass = await User.Password.gen(password); + password = null; // Cleaning password from memory + let status = response.getStatus; + token = User.Token.gen(status, user.id, encryptedPass); + Database.auth.newToken(user.id, token, encryptedPass); + } + response.Token = token.token; + + response.endpoint(); next(); } } diff --git a/src/controllers/routes/router.js b/src/controllers/routes/router.js index 95f1e41..2774f76 100644 --- a/src/controllers/routes/router.js +++ b/src/controllers/routes/router.js @@ -14,8 +14,8 @@ export class Router { app.get('/', [MiddleWare.RateLimits.request, Router.frontPage]); - app.get('/user/:id', [MiddleWare.RateLimits.request]); - app.delete('/user/:id', [MiddleWare.RateLimits.request]); + // app.get('/user/:id', [MiddleWare.RateLimits.request]); + // app.delete('/user/:id', [MiddleWare.RateLimits.request]); app.post('/user', [MiddleWare.RateLimits.request, Controllers.UserController.newUser]); app.post('/login', [MiddleWare.RateLimits.request, Controllers.LoginController.authenticate]) diff --git a/src/controllers/userController.js b/src/controllers/userController.js index b9c90bf..49ac008 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -24,8 +24,8 @@ export class UserController extends ControllerHandler { if (!UserController.isPasswordValid(password)) errors.addError(422, 'Unprocessaable entity', 'Invalid password has spaces'); if (password.length < 7) errors.addError(422, 'Unprocessaable entity', 'Invalid password less than 7 charicters'); - if (await Database.users.getID('username', username) != -1) errors.addError(422, 'Unprocessable entity', 'A user with that username allready exists'); - if (await Database.users.getID('email', email) != -1) errors.addError(422, 'Unprocessable entity', 'A user with that email allready exists'); + if (await Database.users.getUser('username', username) != -1) errors.addError(422, 'Unprocessable entity', 'A user with that username allready exists'); + if (await Database.users.getUser('email', email) != -1) errors.addError(422, 'Unprocessable entity', 'A user with that email allready exists'); if (errors.count() > 0) { errors.endpoint(); @@ -35,16 +35,14 @@ export class UserController extends ControllerHandler { let response = new API.user(res, id, username, email, new Date().toLocaleString()); - let encryptedPass = await User.Password.gen(password); password = null; // Cleaning password from memory - console.log(encryptedPass); - let status = response.getStatus; let id = new Date().getTime(); let token = await User.Token.gen(status, id, encryptedPass); + await Database.auth.newToken(id, token, encryptedPass); response.Token = token; let user = new User(id, username, encryptedPass, email, ip, 1234); diff --git a/src/models/database/sqlite/database.sqlite b/src/models/database/sqlite/database.sqlite index 13659336c3585fc0f566e4dd044fedcd9f2571b4..786de92975ec67db811ba7d0f5bf970282c316b8 100644 GIT binary patch delta 591 zcmb7=v2N2)07Y%J4j{#8I+O~vQanT~5cB-(*iIG{OzS?YaYSRMNtQNs+{W&+w5&*GY1T+?qhXy)yv_JE=I>VjRl zr3%Fxs&uvTmHknASG-|A=c{F0%)b;1^u32mch=X|=!++p-`MB(lj*4&9&*-J-+Ig) zMx!{MjN(b&E)`bw@2&L+k3ts!P=!ZT0aQV2&)tt_FIz~fLoB`s&|o{^g`^v1u&b$Q z-HXH!XxhoKot*_pPqN)azI** z0Q^PGe^!>uEE&a86h)V=$U$=JQ|bKT8GV;|&Aeg|qTP?g;LeQ6k?wdM&p`GZ38sf) z6z4aw*<>i0CG!4ng9LL+rNBoBk_*nxJ80y7=zw%74d*48p_yo0l0mxMx77(f#{ zV%yu*jAYn1=Y#z*M*h^Z&&8fUfW5ghBP{`*j7dw|vgfAXl1$%`%n%S0JxNfrh@l?Gn^9}H|fhZy*F^PlHE&NqqY5bunQjb1#PpYX6T za(Xbci;IdfHWg3)% 0) return; let d = new Date(); console.log('[' + d.toLocaleString() + '] [' - + colours.magenta('SQLITE') + '] ' + message); + + colours.magenta(Dialect) + '] ' + message); } static middleware(message) {