<commit_before>import React, {Component} from 'react';
import PropTypes from 'prop-types';
import Login from "../components/Login";

function unixSeconds() {
    return Math.round(Number(new Date()) / 1000);
}

class LoginContainer extends Component {
    componentDidMount() {
        const savedToken = this.getValidAccessToken();
        if (savedToken) {
            this.props.spotifyApi.setAccessToken(savedToken);
            this.props.onAuthorize();
        } else {
            this.addMessageListener();
        }
    }

    addMessageListener = () => {
        window.addEventListener("message", (event) => {
            // console.log(event);
            const message = event.data;
            if (message.type === 'spotify_access_token') {
                // console.log("access token", message);
                this.props.spotifyApi.setAccessToken(message.access_token);
                this.saveAccessToken(message.access_token, Number(message.expires_in));
                this.props.onAuthorize();
            } else if (message.type === 'spotify_login_error') {
                this.props.showNotification('Error logging in - ' + message.error);
            }
        }, false);
    };

    saveAccessToken = (token, expiresIn) => {<commit_msg>Security: Put in a check for the sender origin in the postMessage receive event listener<commit_after>import React, {Component} from 'react';
import PropTypes from 'prop-types';
import Login from "../components/Login";

function unixSeconds() {
    return Math.round(Number(new Date()) / 1000);
}

class LoginContainer extends Component {
    componentDidMount() {
        const savedToken = this.getValidAccessToken();
        if (savedToken) {
            this.props.spotifyApi.setAccessToken(savedToken);
            this.props.onAuthorize();
        } else {
            this.addMessageListener();
        }
    }

    addMessageListener = () => {
        window.addEventListener("message", (event) => {
            // console.log(event);
            const message = event.data;
            if (event.origin === window.location.origin) {
                if (message.type === 'spotify_access_token') {
                    // console.log("access token", message);
                    this.props.spotifyApi.setAccessToken(message.access_token);
                    this.saveAccessToken(message.access_token, Number(message.expires_in));
                    this.props.onAuthorize();
                } else if (message.type === 'spotify_login_error') {
                    this.props.showNotification('Error logging in - ' + message.error);
                }
            }
        }, false);
    };

    saveAccessToken = (token, expiresIn) => {
{'fraction_non_alphanumeric': 0.12361623616236163, 'fraction_numerical': 0.002952029520295203, 'mean_word_length': 2.196933962264151, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '4101280', 'n_tokens_mistral': 718, 'n_tokens_neox': 692, 'n_words': 141}