solved Login Event Listener

SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
Have searched Google for JR code sample to handle: "login event listener" - found nothing yet...
Our first custom module is ready in place, but cannot find docs with detailed list of JR listeners.
Any docs or code samples that could clearly explain how to handle "login event listener" ?
Please Advise...
updated by @softdesigns: 04/12/17 05:30:30PM
michael
@michael
7 years ago
7,715 posts
Listeners are all over the place in the code. Often there are several different listeners that COULD be used to get your adjustments in where you want them.

The most up-to-date docs are in the docblocks of the functions themselves in the code, so if you're using an IDE for your development, it should have facilities to bring up the docblocks. ( we like phpstorm, but there are many good IDE's )

For me, when I want to locate a suitable listener for a location I take a look around the area I'm wanting to effect and see if there are any jrCore_trigger_event() functions, then use that listener.

(screenshot search for event triggers in jrUser module)

After running a search for event triggers on the jrUser module, it returns a list of possible triggers.

The one with the most likely name seams to be "login_succcess".

Thats probably the one you're after.

The explanation for that even is "Fired when a user successfully logs in".

Try that one.
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
Yes - you are right - if one is brave enough to dig into the source, we do see there are listeners all over the place. DocBlocks sound great: Our team tries to avoid 'locally installed IDEs', instead we have moved into the Cloud9 IDE, not sure how to get DocBlocks from Cloud9 IDE.
-----
We are Beginners at JR Development - so we will post this code below, to help others like us:
-----
The code below is close, but not quite working, seems not firing the listener code:
Any ideas to fix? Please Advise...
<?php
/**
* 2017 SoftDesigns
*/

//Ensure this module not called directly:
defined('APP_DIR') or exit();

/**
 * meta
 */
function sdContestMWUser_meta(){
	$_tmp = array(
			'name'        => 'ContestMWUser',
			'url'         => 'ContestMWUser',
			'version'     => '1.0.0',
			'developer'   => 'SoftDesigns',
			'description' => 'Verify Contest User',
			'category'    => 'tools'
	);
	return $_tmp;
}

/**
 * init
 */
function sdContestMWUser_init(){
	jrCore_register_event_listener('jrUser','login_success',
			'sdContestMWUser_login_success_listener');
}
function sdContestMWUser_login_success_listener($_data,$_user,$_conf,$_args,$event)
{ $_Log1 = "sdsdContestMWUser"; $_Log2 = "sdContestMWUser_login_success_listener"; $_Log3 = $_args; // fdebug($_Log2, $_args); // jrCore_logger($_Log1, $_Log2, $_Log3); return $_data; }

updated by @softdesigns: 01/10/17 08:18:03PM
michael
@michael
7 years ago
7,715 posts
The first thing I can see that is incorrect is the use of the jrCore_logger() function.

Attached is a screenshot of that function with its docblock exposed.

You can see from the extra info that the correct usage for that function is jrCore_logger('INF', 'this is an info message that gets logged in the activity log.');

The rest of your function looks like it should be working. Make sure that you module is active in the ACP. Only active modules will work.

go to your modules INFO tab and check the ACTIVE checkbox and save. then run the integrity check. It should be working.
screenshot_docblock.png

michael
@michael
7 years ago
7,715 posts
Try this one:
/modules/sdContestMWUser/include.php
<?php
/**
 * 2017 SoftDesigns
 */

//Ensure this module not called directly:
defined('APP_DIR') or exit();

/**
 * meta
 */
function sdContestMWUser_meta(){
    $_tmp = array(
        'name'        => 'ContestMWUser',
        'url'         => 'ContestMWUser',
        'version'     => '1.0.0',
        'developer'   => 'SoftDesigns',
        'description' => 'Verify Contest User',
        'category'    => 'tools'
    );
    return $_tmp;
}

/**
 * init
 */
function sdContestMWUser_init(){
    jrCore_register_event_listener('jrUser', 'login_success','sdContestMWUser_login_success_listener');
}

/**
 * @param $_data
 * @param $_user
 * @param $_conf
 * @param $_args
 * @param $event
 * @return mixed
 */
function sdContestMWUser_login_success_listener($_data, $_user, $_conf, $_args, $event)
{ $_Log1 = "sdsdContestMWUser"; $_Log2 = "sdContestMWUser_login_success_listener"; $_Log3 = $_args; // $_fdebug = array( 'What' => "trying to figure out whats going on in this listener event sdContestMWUser_login_success_listener()", '$_log1' => $_Log1, '$_log2' => $_Log2, '$_log3' => $_Log3 ); fdebug($_fdebug); // jrCore_logger('inf', 'fired the login success listener, it worked'); return $_data; }

updated by @michael: 01/10/17 09:07:25PM
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
Yes - you are correct on both counts: The bug was that our first param in jrCore_logger was wrong, as this first param must be a hard coded constant value. As your screen shot above shows, having the "DocBlocks" in our IDE, we could have quickly found this bug.
-----
Simple fix, to change the first param in: jrCore_logger
-----
Bug:
jrCore_logger($_Log1, $_Log2, $_Log3);
-----
Fix:
jrCore_logger("INF", $_Log2, $_Log3);
-----
Lesson: For JR Development, having DocBlocks show in our IDE is essential
-----
Great Support - thanks...
updated by @softdesigns: 01/11/17 10:48:47AM
brian
@brian
7 years ago
10,148 posts
SoftDesigns:
Lesson: For JR Development, having DocBlocks show in our IDE is essential

Yep - all JR functions are docblocked, which really helps in an IDE :)


--
Brian Johnson
Founder and Lead Developer - Jamroom
https://www.jamroom.net

Tags