Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented additional route for checking usernames #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* `/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.
Expand Down
23 changes: 23 additions & 0 deletions controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,31 @@ function logout() {
});
}

function userExists(username) {
if (!username) {
return Promise.reject({
success: false,
message: 'Username is not provided',
});
}
return User.findOne({ username })
.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,
userExists,
};
18 changes: 18 additions & 0 deletions routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,22 @@ authRouter.get('/logout', (req, res, next) => {
});
});

authRouter.get('/user-exists', (req, res, next) => {
const { username } = req.query;
authConroller
.userExists(username)
.then((result) => {
res.json({
success: result.success,
});
})
.catch((error) => {
res.json({
success: false,
message: error.message,
});
next(error);
});
});

module.exports = authRouter;