Skip to content

authenticate

Kechushou edited this page May 19, 2015 · 2 revisions

dom/authenticate

Description: Ensures the user is logged in before accessing content pages. Otherwise, the browser is redirected back to the login page.

This module also does the following:

  • Appends the logout button to the header
  • Appends the "Report a Problem" button to the header, which mails the account specified in config.js
  • Adds the current username to the header bar
  • If the current page has a form, an "addedBy" hidden input is added to automatically populate the addedBy fact column

Functions

quickforms.logout()

Removes the userid cookie, and redirects the user back to the login page.

define(['helper/helper','dom/form/text'],
function (){
	
	var loginPage = "/"+quickforms.app+"/index.html"
	var username = getCookie('username'),
		userId = getCookie('userid');
	if(isNull(username) 
		&& window.location.pathname != loginPage
		&& !quickforms.offline)
	{
		window.location = loginPage;
	}
	else
	{
		var header = $('div[data-role="header"]'),
			logoutDiv = $('<div class="ui-btn-right" data-role="controlgroup" data-type="horizontal" ></div>'),
			logoutButton = $('<a href="#"  data-role="button" data-theme="c" onclick="quickforms.logout()" style="position:relative;top:-5px">Logout</a>'),
			reportButton = $('<a href="mailto:'+quickforms.qfEmail+'" data-role="button" data-rel="dialog" data-theme="c" style="position:relative;top:-5px">Report a Problem</a>'),
			topRightContainer = $('<div data-role="controlgroup" data-type="horizontal" ></div>');
		topRightContainer.append(reportButton);
		topRightContainer.append(logoutButton);
		header.children().first().append(' - '+username);
		header.append (logoutDiv);
		logoutDiv.append(topRightContainer);
		header.trigger('create');
		quickforms.form.domParsers.push(function(formObj){
				var addedBy = $('<input type="hidden" name="addedBy" id = "addedBy" value="'+userId+'" />');
				var texObj = new quickforms.TextElement(addedBy,formObj);
				texObj.summary = function(){return '';};
				texObj.parseDom(formObj);
				formObj.addChild(texObj);
				window.setTimeout(function(){formObj.finishedParsing();},1);	
			});
		
		
	}
	quickforms.logout = function()
	{
		setCookie('username','',1);
		window.location = loginPage;
	}
});
Clone this wiki locally