<commit_before>const Buffer = require('buffer/').Buffer;
const request = require('request');
const userController = require('./userController');
const songController = require('./songController');
const nconf = require('../config/nconf');

const loginController = {};

loginController.login = (req, res, next) => {
  const body = (req.body);
  let token = {};
  let spotifyUserProfile;
  if (!body.code || !req.spotifyApi._credentials.clientId) return res.sendStatus(400);
  req.spotifyApi.authorizationCodeGrant(body.code)
  .then(data => {
    if (data.statusCode === 200) {
      token.access_token = data.body['access_token'];
      token.expires_in = data.body['expires_in'];
      token.refresh_token = data.body['refresh_token'];
      // set access token
      req.spotifyApi.setAccessToken(data.body['access_token']);
      req.spotifyApi.setRefreshToken(data.body['refresh_token']);
    }
  }, (err) => console.log(err))
  .then(() => {
    req.spotifyApi.getMe()
    .then(data => {
      return data;
    })
    .then(userInfo => {
      spotifyUserProfile = userInfo;
      // retrieve the user from DB
      // save it in the DB if it doesn't exist
      return userController.getUser({spotifyId:userInfo.body.id})
      .then((user) => {
        if (user.length > 0) {<commit_msg>refactor: send response with status code if authorization fails<commit_after>const Buffer = require('buffer/').Buffer;
const request = require('request');
const userController = require('./userController');
const songController = require('./songController');
const nconf = require('../config/nconf');

const loginController = {};

loginController.login = (req, res, next) => {
  const body = (req.body);
  let token = {};
  let spotifyUserProfile;
  if (!body.code || !req.spotifyApi._credentials.clientId) return res.sendStatus(400);
  req.spotifyApi.authorizationCodeGrant(body.code)
  .then(data => {
    if (data.statusCode === 200) {
      token.access_token = data.body['access_token'];
      token.expires_in = data.body['expires_in'];
      token.refresh_token = data.body['refresh_token'];
      // set access token
      req.spotifyApi.setAccessToken(data.body['access_token']);
      req.spotifyApi.setRefreshToken(data.body['refresh_token']);
    } else {
      res.status = data.statusCode;
      res.send(data);
    }
  }, (err) => console.log(err))
  .then(() => {
    req.spotifyApi.getMe()
    .then(data => {
      return data;
    })
    .then(userInfo => {
      spotifyUserProfile = userInfo;
      // retrieve the user from DB
      // save it in the DB if it doesn't exist
      return userController.getUser({spotifyId:userInfo.body.id})
      .then((user) => {
        if (user.length > 0) {
{'fraction_non_alphanumeric': 0.15529324972334932, 'fraction_numerical': 0.005164146071560309, 'mean_word_length': 3.4097560975609755, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2464729', 'n_tokens_mistral': 850, 'n_tokens_neox': 830, 'n_words': 170}