       port       = if publicPort in ['80', '443'] then '' else ":#{publicPort}"
 
       loginUrl = "#{protocol}://#{host}#{port}/#{URL}"
-      return res.status(200).send { token, loginUrl }
+      return sendApiResponse res, { token, loginUrl }
 
   ]
 

  token        = null
  { username } = req.body

  errors =
    invalidRequest      :
      statusCode        : 400
      message           : 'invalid request'
    unauthorizedRequest :
      statusCode        : 401
      message           : 'unauthorized request'

  unless username
    return { error : errors.invalidRequest }

  unless token = checkAuthorizationBearerHeader req
    return { error : errors.unauthorizedRequest }

  return { error : null, token, username }


    ->
      # checking if token is valid
      JApiToken.one { code : token }, (err, apiToken_) ->
        return sendApiError res, apiErrors.internalError    if err
        return sendApiError res, apiErrors.invalidApiToken  unless apiToken_
        apiToken = apiToken_
        queue.next()

    ->
      checkApiTokenAvailability { apiToken }, (err) ->
        return sendApiError res, err  if err
        queue.next()

    ->
      # checking if user exists
      JAccount.one { 'profile.nickname' : username }, (err, account_) ->
        return sendApiError res, apiErrors.internalError    if err
        return sendApiError res, apiErrors.invalidUsername  unless account_
        account = account_
        queue.next()

