atlasLogin.js 3.65 KB
Newer Older
1
/**
2 3 4 5 6 7 8
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
9 10 11 12 13 14 15 16
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
17 18
 */

19

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
//Define indexOf for IE
if (!Array.indexOf) {
	Array.prototype.indexOf = function(obj, start) {
		for ( var i = (start || 0); i < this.length; i++) {
			if (this[i] == obj) {
				return i;
			}
		}
		return -1;
	};
}


function doLogin() {

	var userName = $('#username').val().trim();
	var passwd = $('#password').val().trim();

	if (userName === '' || passwd === '') {
		$('#errorBox').show();
		$('#signInLoading').hide();
		$('#signIn').removeAttr('disabled');
		$('#errorBox .errorMsg').text("The username or password you entered is blank..");
		return false;
	}

	var baseUrl = getBaseUrl();
	if (baseUrl.lastIndexOf('/') != (baseUrl.length - 1)) {
		if (baseUrl) {
			baseUrl = baseUrl + '/';
		} else {
			baseUrl = '/';
		}
	}
	var url = baseUrl + 'j_spring_security_check';

	$.ajax({
		data : {
			j_username : userName,
			j_password : passwd
		},
		url : url,
		type : 'POST',
		headers : {
			"cache-control" : "no-cache"
		},
		success : function() {
			if(location.hash.length > 2)
				window.location.replace('index.html'+location.hash);
			else
				window.location.replace('index.html');
		},
		error : function(jqXHR, textStatus, err ) {
			$('#signIn').removeAttr('disabled');
			$('#signInLoading').css("visibility", "hidden");

			if(jqXHR.status && jqXHR.status == 412){
				$('#errorBox').hide();
				$('#errorBoxUnsynced').show();
			} else {
				var resp = JSON.parse(jqXHR.responseText);

                if(resp.msgDesc.startsWith("Username not found") || resp.msgDesc.startsWith("Wrong password")){
                    $('#errorBox .errorMsg').text("Invalid User credentials. Please try again.");
                }else if(resp.msgDesc.startsWith("User role credentials is not set properly")){
                    $('#errorBox .errorMsg').text("User role or credentials is not set properly");
                }else{
                    $('#errorBox .errorMsg').text("Error while authentication");
                }

				$('#errorBox').show();
				$('#errorBoxUnsynced').hide();
			}
		}
	});
}
function getBaseUrl(){
	if(!window.location.origin){
		window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
	}
	return window.location.origin
	+ window.location.pathname.substring(window.location.pathname
			.indexOf('/', 2) + 1, 0);
}
$(function() {
// register handlers
	$('#signIn').on('click', function() {
		$('#signIn').attr('disabled',true);
		$('#signInLoading').css("visibility", "visible");
		doLogin();
		return false;
	});
	$('#loginForm').each(function() {
		$('input').keypress(function(e) {
			// Enter pressed?
			if (e.which == 10 || e.which == 13) {
				doLogin();
			}
		});
	});
	
	$('#loginForm  li[class^=control-group] > input').on('change',function(e){
		if(e.target.value === ''){
			$(e.target).parent().addClass('error');
		}else{
			$(e.target).parent().removeClass('error');
		}
	});
});