<commit_before>/**
 * React Starter Kit (https://www.reactstarterkit.com/)
 *
 * Copyright © 2014-2016 Kriasoft, LLC. All rights reserved.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE.txt file in the root directory of this source tree.
 */

import React from 'react';
import Music from './Music';
import fetch from '../../core/fetch';

export default {

  path: '/server/:serverId/music',
  auth: true,

  async action(context, params) {
    const options = {
      method: 'get',
      credentials: 'include',
    };
    if (context.headers) {
      options.headers = context.headers;
    }

    let musicResp;
    try {
      musicResp = await fetch(`/api/v1/music/${params.serverId}`, options);
    } catch (error) {
      console.error('prefix Resp caught', error);
    }

    let user;
    if (context.user) {
      console.log('Got cached User');
      user = context.user;
    } else {
      try {
        const resp = await fetch('/api/v1/user/', options);
        console.log('Fetching user');
        user = await resp.json();
      } catch (error) {
        throw new Error(`User Object request failed. Error: ${error}`);
      }
    }

    let music;
    if (musicResp.status === 200) {
      music = (await musicResp.json());
    }

    console.log(music);
    // if (!prefix) throw new Error('Prefix Object missing.');
    if (!user) throw new Error('User Object missing.');

    return <Music user={user} serverId={params.serverId} music={music} />;
  },
};
<commit_msg>redirect to login page if user is not authorised or resources can't be found.<commit_after>/**
 * React Starter Kit (https://www.reactstarterkit.com/)
 *
 * Copyright © 2014-2016 Kriasoft, LLC. All rights reserved.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE.txt file in the root directory of this source tree.
 */

import React from 'react';
import Music from './Music';
import fetch from '../../core/fetch';

export default {

  path: '/server/:serverId/music',
  auth: true,

  async action(context, params) {
    const options = {
      method: 'get',
      credentials: 'include',
    };
    if (context.headers) {
      options.headers = context.headers;
    }

    let musicResp;
    try {
      musicResp = await fetch(`/api/v1/music/${params.serverId}`, options);
    } catch (error) {
      context.context.redirect('/login');
    }

    let user;
    if (context.user) {
      console.log('Got cached User');
      user = context.user;
    } else {
      try {
        const resp = await fetch('/api/v1/user/', options);
        console.log('Fetching user');
        user = await resp.json();
      } catch (error) {
        context.context.redirect('/login');
      }
    }

    let music;
    if (musicResp.status === 200) {
      music = (await musicResp.json());
    }

    console.log(music);
    // if (!prefix) throw new Error('Prefix Object missing.');
    if (!user) throw new Error('User Object missing.');

    return <Music user={user} serverId={params.serverId} music={music} />;
  },
};

{'fraction_non_alphanumeric': 0.1469055374592834, 'fraction_numerical': 0.008469055374592834, 'mean_word_length': 2.558516801853998, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '4007288', 'n_tokens_mistral': 951, 'n_tokens_neox': 909, 'n_words': 279}