<commit_before>(function() {
    var t = klp.login_modal = {};
    var postLoginCallback = null;
    t.open = function(callback) {
        postLoginCallback = callback;
        klp.openModal = t;
        $('.js-login-modal').addClass('show');
        showSignup();
    };

    t.close = function() {
        //showSignup();
        console.log("login modal close called");
        $('.js-login-modal').removeClass('show');
        klp.utils.clearForm('signupForm');
        klp.utils.clearForm('loginForm');
        klp.utils.clearForm('forgotPasswordForm');
        postLoginCallback = null;
    };

    t.init = function() {
        $('#signupForm').submit(submitSignup);
        $('#signupFormSubmit').click(function(e) {
            e.preventDefault();
            $('#signupForm').submit();
        });
        $('#loginForm').submit(submitLogin);
        $('#loginFormSubmit').click(function(e) {
            e.preventDefault();
            $('#loginForm').submit();
        });

        $('.js-showLogin').click(showLogin);
        $('.js-showSignup').click(showSignup);

        $('#forgotPasswordForm').submit(submitForgotPassword);
        $('#forgotPasswordFormSubmit').click(function(e) {
            e.preventDefault();
            $('#forgotPasswordForm').submit();
        });
        $('.js-showForgotPassword').click(showForgotPassword);
    };

    function showSignup(e) {
        if (e) {
            e.preventDefault();
        }
        $('#loginContainer').hide();
        $('#forgotPasswordContainer').hide();
        $('#signupContainer').show();

    }

    function showForgotPassword(e) {
        if (e) {
            e.preventDefault();
        }
        $('#loginContainer').hide();
        $('#signupContainer').hide();
        $('#forgotPasswordContainer').show();
    }

    function showLogin(e) {
        e.preventDefault();
        $('#signupContainer').hide();
        $('#loginContainer').show();
    }

    function submitSignup(e) {
        if (e) {
            e.preventDefault();
        }
        var formID = 'signupForm';
        klp.utils.clearValidationErrors(formID);
        var isValid = klp.utils.validateRequired(formID);
        if (isValid) {
            var fields = {
                'first_name': $('#signupFirstName'),
                'last_name': $('#signupLastName'),
                'mobile_no': $('#signupPhone'),
                'email': $('#signupEmail'),
                'password': <PASSWORD>')                
            };

            var data = klp.utils.getFormData(fields);
            
            klp.utils.startSubmit(formID);
            var signupXHR = klp.api.signup(data);
            
            signupXHR.done(function(userData) {
                klp.utils.stopSubmit(formID);           
                klp.auth.loginUser(userData);
                klp.utils.alertMessage("Thanks for signing up!", "success");
                if (postLoginCallback) {
                    postLoginCallback();
                }
                t.close();
            });

            signupXHR.fail(function(err) {
                //FIXME: deal with errors
                console.log("signup error", err);
                klp.utils.stopSubmit(formID);
                var errors = JSON.parse(err.responseText);
                if ('detail' in errors && errors.detail === 'duplicate email') {
                    var $field = fields.email;
                    klp.utils.invalidateField($field, "This email address already exists.");
                } else {
                    klp.utils.invalidateErrors(fields, errors);
                }
                //alert("error signing up");
            });
        }
    }

    function submitLogin(e) {
        if (e) {
            e.preventDefault();
        }
        var formID = 'loginForm';
        klp.utils.clearValidationErrors(formID);
        var isValid = klp.utils.validateRequired('loginForm');
        if (isValid) {
            var data = {
                'email': $('#loginEmail').val(),
                'password': $('#<PASSWORD>').val()
            };
            var loginXHR = klp.api.login(data);
            klp.utils.startSubmit(formID);
            loginXHR.done(function(userData) {
                klp.utils.stopSubmit(formID);
                userData.email = data.email;
                klp.auth.loginUser(userData);
                klp.utils.alertMessage("Logged in successfully!", "success");
                if (postLoginCallback) {
                    postLoginCallback();
                }
                t.close();
                //console.log("login done", postLoginCallback);

            });

            loginXHR.fail(function(err) {
                console.log("login error", err);
                klp.utils.stopSubmit(formID);
                var errors = JSON.parse(err.responseText);
                var $field = $('#loginPassword');
                if (errors.detail) {
                    klp.utils.invalidateField($field, errors.detail);
                }
            });
        }
    }

    function submitForgotPassword(e) {
        if (e) {
            e.preventDefault();
        }
        var formID = 'forgotPasswordForm';
<commit_msg>display error for unknown error on login, fixes #183<commit_after>(function() {
    var t = klp.login_modal = {};
    var postLoginCallback = null;
    t.open = function(callback) {
        postLoginCallback = callback;
        klp.openModal = t;
        $('.js-login-modal').addClass('show');
        showSignup();
    };

    t.close = function() {
        //showSignup();
        console.log("login modal close called");
        $('.js-login-modal').removeClass('show');
        klp.utils.clearForm('signupForm');
        klp.utils.clearForm('loginForm');
        klp.utils.clearForm('forgotPasswordForm');
        postLoginCallback = null;
    };

    t.init = function() {
        $('#signupForm').submit(submitSignup);
        $('#signupFormSubmit').click(function(e) {
            e.preventDefault();
            $('#signupForm').submit();
        });
        $('#loginForm').submit(submitLogin);
        $('#loginFormSubmit').click(function(e) {
            e.preventDefault();
            $('#loginForm').submit();
        });

        $('.js-showLogin').click(showLogin);
        $('.js-showSignup').click(showSignup);

        $('#forgotPasswordForm').submit(submitForgotPassword);
        $('#forgotPasswordFormSubmit').click(function(e) {
            e.preventDefault();
            $('#forgotPasswordForm').submit();
        });
        $('.js-showForgotPassword').click(showForgotPassword);
    };

    function showSignup(e) {
        if (e) {
            e.preventDefault();
        }
        $('#loginContainer').hide();
        $('#forgotPasswordContainer').hide();
        $('#signupContainer').show();

    }

    function showForgotPassword(e) {
        if (e) {
            e.preventDefault();
        }
        $('#loginContainer').hide();
        $('#signupContainer').hide();
        $('#forgotPasswordContainer').show();
    }

    function showLogin(e) {
        e.preventDefault();
        $('#signupContainer').hide();
        $('#loginContainer').show();
    }

    function submitSignup(e) {
        if (e) {
            e.preventDefault();
        }
        var formID = 'signupForm';
        klp.utils.clearValidationErrors(formID);
        var isValid = klp.utils.validateRequired(formID);
        if (isValid) {
            var fields = {
                'first_name': $('#signupFirstName'),
                'last_name': $('#signupLastName'),
                'mobile_no': $('#signupPhone'),
                'email': $('#signupEmail'),
                'password': $('#<PASSWORD>')                
            };

            var data = klp.utils.getFormData(fields);
            
            klp.utils.startSubmit(formID);
            var signupXHR = klp.api.signup(data);
            
            signupXHR.done(function(userData) {
                klp.utils.stopSubmit(formID);           
                klp.auth.loginUser(userData);
                klp.utils.alertMessage("Thanks for signing up!", "success");
                if (postLoginCallback) {
                    postLoginCallback();
                }
                t.close();
            });

            signupXHR.fail(function(err) {
                //FIXME: deal with errors
                console.log("signup error", err);
                klp.utils.stopSubmit(formID);
                var errors = JSON.parse(err.responseText);
                if ('detail' in errors && errors.detail === 'duplicate email') {
                    var $field = fields.email;
                    klp.utils.invalidateField($field, "This email address already exists.");
                } else {
                    klp.utils.invalidateErrors(fields, errors);
                }
                //alert("error signing up");
            });
        }
    }

    function submitLogin(e) {
        if (e) {
            e.preventDefault();
        }
        var formID = 'loginForm';
        klp.utils.clearValidationErrors(formID);
        var isValid = klp.utils.validateRequired('loginForm');
        if (isValid) {
            var data = {
                'email': $('#loginEmail').val(),
                'password': $('#loginPassword').val()
            };
            var loginXHR = klp.api.login(data);
            klp.utils.startSubmit(formID);
            loginXHR.done(function(userData) {
                klp.utils.stopSubmit(formID);
                userData.email = data.email;
                klp.auth.loginUser(userData);
                klp.utils.alertMessage("Logged in successfully!", "success");
                if (postLoginCallback) {
                    postLoginCallback();
                }
                t.close();
                //console.log("login done", postLoginCallback);

            });

            loginXHR.fail(function(err) {
                console.log("login error", err);
                klp.utils.stopSubmit(formID);
                var errors = JSON.parse(err.responseText);
                var $field = $('#loginPassword');
                if (errors.detail) {
                    klp.utils.invalidateField($field, errors.detail);
                } else {
                    klp.utils.alertMessage("Login failed due to unknown error. Please contact us if this happens again.", "error");
                }
            });
        }
    }

    function submitForgotPassword(e) {
        if (e) {
            e.preventDefault();
        }
        var formID = 'forgotPasswordForm';

{'fraction_non_alphanumeric': 0.14634606317774634, 'fraction_numerical': 0.0002828854314002829, 'mean_word_length': 1.6033382425135003, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '115150', 'n_tokens_mistral': 2858, 'n_tokens_neox': 2788, 'n_words': 439}