   queue = [
 
     ->
-      # checking if token is valid
-      JApiToken.one { code : token }, (err, apiToken_) ->
-        return res.status(500).send 'an error occurred'  if err
-        return res.status(400).send 'invalid token!'     unless apiToken_
-        apiToken = apiToken_
-        queue.next()
-
-    ->
-      # creating a random username with first letters of group slug in front
-      username or= "#{apiToken.group.substring(0, 4)}#{hat(32)}"
-      # checking if username is available
-      JUser.usernameAvailable username, (err, { kodingUser, forbidden }) ->
-        return res.status(500).send 'an error occurred'          if err
-        return res.status(400).send 'username is not available'  if kodingUser or forbidden
+      validateCreateUser { token, username }, (err, data) ->
+        return res.status(err.statusCode).send(err.message)  if err
+        { username, apiToken } = data
         queue.next()
 
     ->

  daisy queue



validateRequest = (req) ->



module.exports = createUser = (req, res, next) ->

  { JUser } = koding.models

  clientId = getClientId req, res
  return handleClientIdNotFound res, req  unless clientId
