From 1147215674bd6a81b62184aad34c337cdbd48696 Mon Sep 17 00:00:00 2001 From: Dmitry Tselinko Date: Fri, 25 May 2018 10:19:45 +0300 Subject: [PATCH 1/4] implemented additional route for checking usernames --- controllers/auth.js | 23 +++++++++++++++++++++++ routes/auth.js | 19 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/controllers/auth.js b/controllers/auth.js index 96d7524..0237754 100644 --- a/controllers/auth.js +++ b/controllers/auth.js @@ -102,8 +102,31 @@ function logout() { }); } +function checkUser(username) { + if (!username) { + return Promise.reject({ + success: false, + message: 'Username is not provided', + }); + } + return User.findOne({ username: username.toLowerCase() }) + .exec() + .then((user) => { + if (user) { + return Promise.reject({ + success: false, + message: 'Username is already taken', + }); + } + return Promise.resolve({ + success: true, + }); + }); +} + module.exports = { signUp, login, logout, + checkUser, }; diff --git a/routes/auth.js b/routes/auth.js index 87564d4..7377a54 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -1,3 +1,4 @@ +const url = require('url'); const { Router } = require('express'); const authConroller = require('../controllers/auth'); @@ -67,4 +68,22 @@ authRouter.get('/logout', (req, res, next) => { }); }); +authRouter.get('/check-user', (req, res, next) => { + const { query } = url.parse(req.url, true).query; + authConroller + .checkUser(query) + .then((result) => { + res.json({ + success: result.success, + }); + }) + .catch((error) => { + res.json({ + success: false, + message: error.message, + }); + next(error); + }); +}); + module.exports = authRouter; From 2cdcc0551ff36a88746b7abc6029d0d549ac020f Mon Sep 17 00:00:00 2001 From: Dmitry Tselinko Date: Fri, 25 May 2018 21:51:15 +0300 Subject: [PATCH 2/4] corrected query parsing, updated README --- README.md | 3 ++- routes/auth.js | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1926627..e874900 100644 --- a/README.md +++ b/README.md @@ -41,10 +41,11 @@ http://localhost:8000/v1/chats Here's the map of API's HTTP routes: -* `/` — routes related to authentication. +* `/` — routes related to authentication. * `/signup` **POST** — create new user with `username` and `password`. * `/login` **POST** — log user in with `username` and `password`. * `/logout` **GET** — log out active user. + * `/check-user?query=john` **POST** — check, whether username is already taken. * `/users` — routes related to users. * `/users` **GET** — retrieve data about all users. * `/users/me` **GET** — retrieve my user's data. diff --git a/routes/auth.js b/routes/auth.js index 7377a54..5d5a6b8 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -1,4 +1,3 @@ -const url = require('url'); const { Router } = require('express'); const authConroller = require('../controllers/auth'); @@ -69,7 +68,7 @@ authRouter.get('/logout', (req, res, next) => { }); authRouter.get('/check-user', (req, res, next) => { - const { query } = url.parse(req.url, true).query; + const { query } = req.query; authConroller .checkUser(query) .then((result) => { From 6d9fdc8b72c9348fe33ccb0436b3d20c61d4f0c3 Mon Sep 17 00:00:00 2001 From: Dmitry Tselinko Date: Fri, 25 May 2018 21:52:59 +0300 Subject: [PATCH 3/4] corrected README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e874900..4e3236c 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ Here's the map of API's HTTP routes: * `/signup` **POST** — create new user with `username` and `password`. * `/login` **POST** — log user in with `username` and `password`. * `/logout` **GET** — log out active user. - * `/check-user?query=john` **POST** — check, whether username is already taken. + * `/check-user?query=john` **GET** — check, whether username is already taken. * `/users` — routes related to users. * `/users` **GET** — retrieve data about all users. * `/users/me` **GET** — retrieve my user's data. From 7fe134e57a6aedb01595aa2a638c4e69dfb71027 Mon Sep 17 00:00:00 2001 From: Dmitry Tselinko Date: Fri, 1 Jun 2018 19:49:17 +0200 Subject: [PATCH 4/4] corrected API according to code review comments --- README.md | 2 +- controllers/auth.js | 6 +++--- routes/auth.js | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 4e3236c..05a4af5 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ Here's the map of API's HTTP routes: * `/signup` **POST** — create new user with `username` and `password`. * `/login` **POST** — log user in with `username` and `password`. * `/logout` **GET** — log out active user. - * `/check-user?query=john` **GET** — check, whether username is already taken. + * `/user-exists?username=john` **GET** — check, whether username is already taken. * `/users` — routes related to users. * `/users` **GET** — retrieve data about all users. * `/users/me` **GET** — retrieve my user's data. diff --git a/controllers/auth.js b/controllers/auth.js index 0237754..17aa854 100644 --- a/controllers/auth.js +++ b/controllers/auth.js @@ -102,14 +102,14 @@ function logout() { }); } -function checkUser(username) { +function userExists(username) { if (!username) { return Promise.reject({ success: false, message: 'Username is not provided', }); } - return User.findOne({ username: username.toLowerCase() }) + return User.findOne({ username }) .exec() .then((user) => { if (user) { @@ -128,5 +128,5 @@ module.exports = { signUp, login, logout, - checkUser, + userExists, }; diff --git a/routes/auth.js b/routes/auth.js index 5d5a6b8..d4da0ea 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -67,10 +67,10 @@ authRouter.get('/logout', (req, res, next) => { }); }); -authRouter.get('/check-user', (req, res, next) => { - const { query } = req.query; +authRouter.get('/user-exists', (req, res, next) => { + const { username } = req.query; authConroller - .checkUser(query) + .userExists(username) .then((result) => { res.json({ success: result.success,