 
   # validating req params
   { error, token, username } = validateRequest req
-  return res.status(error.statusCode).send(error.message)  if error
+  return sendApiError res, error  if error
 
   account  = null
   apiToken = null

    ->
      # 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()

    ->
      # checking if user exists
      JAccount.one { 'profile.nickname' : username }, (err, account_) ->
        return res.status(500).send 'an error occurred'  if err
        return res.status(400).send 'invalid username!'  unless account_
        account = account_
        queue.next()


{ argv }  = require 'optimist'
KONFIG    = require('koding-config-manager').load("main.#{argv.c}")

{ daisy }                          = require 'bongo'
{ checkAuthorizationBearerHeader } = require '../../helpers'
{ sendApiError
  sendApiResponse
  checkApiTokenAvailability }      = require './helpers'

apiErrors = require './errors'


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

