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

33 lines
676 B
TypeScript
Raw Normal View History

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