sharkey/src/api/endpoints/username/available.js

42 lines
712 B
JavaScript
Raw Normal View History

2016-12-29 07:49:51 +09:00
'use strict';
/**
* Module dependencies
*/
import User from '../../models/user';
import { validateUsername } from '../../models/user';
/**
* Check available username
*
2017-03-01 17:37:01 +09:00
* @param {any} params
* @return {Promise<any>}
2016-12-29 07:49:51 +09:00
*/
module.exports = async (params) =>
new Promise(async (res, rej) =>
{
// Get 'username' parameter
const username = params.username;
if (username == null || username == '') {
return rej('username-is-required');
}
// Validate username
if (!validateUsername(username)) {
return rej('invalid-username');
}
// Get exist
const exist = await User
.count({
username_lower: username.toLowerCase()
}, {
limit: 1
});
// Reply
res({
available: exist === 0
});
});