Session (refresh)

Session

Session( Redirect )

The Session function retrieves session information from an API service. In order for session information to be retrieved the 'sessionid' cookie needs to have been previously set using Auth.Login.

function Session( Redirect )
{
	Session.Redirect = Redirect;

	Call( "/api/auth/session/", {}, Session.Switch );
}

Helper functions

Session.Switch
=
function( responseText )
{
	Session.Handler( responseText );

	if ( 'function' === typeof Session.CustomHandler )
	{
		Session.CustomHandler( responseText );
	}

	if ( Session.Redirect )
	{
		Session.Redirect( Session.idtype );
	}
}

Session.Handler
=
function ( responseText )
{
	var idtype = "";

	Session.idtype = "";

	if ( -1 != responseText.indexOf( "UNAUTHENTICATED" ) )
	{
		Session.status = "UNAUTHENTICATED";

        Auth.UnsetCookie( "accessid" );
	}
	else
	if ( -1 != responseText.indexOf( "INVALID_SESSION" ) )
	{
		Session.status = "INVALID_SESSION";

        Auth.UnsetCookie( "accessid" );
	}
	else
	if ( "" != responseText )
	{
		var obj = JSON.parse( responseText );
		if ( obj && obj.idtype )
		{
			Session.email       = obj.email;
			Session.idtype      = obj.idtype;
			Session.given_name  = obj.given_name;
			Session.family_name = obj.family_name;
			Session.user_hash   = obj.user_hash;
			Session.read_only   = obj.read_only;
			Session.status      = "AUTHENTICATED";

			idtype = Session.idtype;
		}
		else
		if ( obj && obj.results && (1 == obj.results.length) && obj.results[0].idtype )
		{
			var obj = obj.results[0];

			Session.email       = obj.email;
			Session.idtype      = obj.idtype;
			Session.given_name  = obj.given_name;
			Session.family_name = obj.family_name;
			Session.user_hash   = obj.user_hash;
			Session.read_only   = obj.read_only;
			Session.status      = "AUTHENTICATED";

			idtype = Session.idtype;
		}
		else
		if ( obj && obj.error )
		{
			Session.status = obj["error"];
		}
	}

	//Redirect( idtype );
}