monkeeShark/src/server/api/endpoints/username/available.ts

33 lines
665 B
TypeScript
Raw Normal View History

2016-12-28 22:49:51 +00:00
/**
* Module dependencies
*/
2017-03-08 18:50:09 +00:00
import $ from 'cafy';
2016-12-28 22:49:51 +00:00
import User from '../../models/user';
import { validateUsername } from '../../models/user';
/**
* Check available username
*
2017-03-01 08:37:01 +00:00
* @param {any} params
* @return {Promise<any>}
2016-12-28 22:49:51 +00:00
*/
2017-03-03 19:28:38 +00:00
module.exports = async (params) => new Promise(async (res, rej) => {
2016-12-28 22:49:51 +00:00
// Get 'username' parameter
2017-03-08 18:50:09 +00:00
const [username, usernameError] = $(params.username).string().pipe(validateUsername).$;
2017-03-02 23:00:10 +00:00
if (usernameError) return rej('invalid username param');
2016-12-28 22:49:51 +00:00
// Get exist
const exist = await User
.count({
2018-03-27 07:51:12 +00:00
host: null,
2016-12-28 22:49:51 +00:00
username_lower: username.toLowerCase()
}, {
limit: 1
});
// Reply
res({
available: exist === 0
});
});