solved Execute JS after User Login

SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
GOAL: Execute JS after user SignIn, to prompt users with various dynamic messages.
--
We have the SignIn event firing, and logging, but cannot get JS alert to work.
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 = "sdContestMWUser"; $_Log2 = $_Log1.":Param-User:".var_dump($_user); // jrCore_logger("INF", $_Log2, ""); // //2017.02.17: TEST: How to open JS Message Window after user login? : $_sJS = "<script>" ."alert('TEST: Alert On SignIn');" ."</script>"; echo $_sJS; jrCore_logger("INF", "DEBUG:JS:13:14", ""); // return $_data; }
--
Any advice or samples, on how to execute custom JS after SignIn - to open custom JS window?
updated by @softdesigns: 05/25/17 04:59:27PM
SteveX
SteveX
@ultrajam
7 years ago
2,584 posts
Untested, but you could try this:
function sdContestMWUser_login_success_listener($_data,$_user,$_conf,$_args,$event)
{ $tmp = array("alert('TEST: Alert On SignIn');"); jrCore_create_page_element('javascript_footer_function', $tmp); }



--
¯\_(ツ)_/¯ Education, learning resources, TEL, AR/VR/MR, CC licensed content, panoramas, interactive narrative, sectional modules (like jrDocs), lunch at Uni of Bristol. Get in touch if you share my current interests or can suggest better :)
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
Thanks for the reply: We have updated the code, but still not working:
function sdContestMWUser_login_success_listener($_data,$_user,$_conf,$_args,$event)
{ $_sModule = "sdContestMWUser"; // //2017.02.17: TEST: How to open JS Message Window after user login? : $_js = array("alert('TEST: Alert On SignIn');"); jrCore_create_page_element('javascript_footer_function', $_js ); // jrCore_logger("INF", $_sModule.":DEBUG:JS:18:25:JS=".var_dump($_js), ""); // return $_data; }
No alert window shows after SignIn.
Also the value of $_js not showing in logs.
We must be missing something - any ideas?
updated by @softdesigns: 02/17/17 06:26:03PM
michael
@michael
7 years ago
7,715 posts
instead of the javascript_footer_function, try 'javascript_ready_function'

As that code is in the header, but wrapped to fire in an
$(document).ready()

The footer function might work too if you wrap it in an on-ready function code to fire when the page is ready to go.

Give each a go. Thing to test is to see if your alert('TEST: Alert On SignIn'); is being placed in the footer after sign-in. If it is, then all that is needed is to get it firing.
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
Thanks for the reply:
We have updated the code to use “javascript_ready_function” but still not working :(
jrCore_logger creates new activity log, so code executed after each SignIn, but no JS alert box firing.
function sdContestMWUser_login_success_listener($_data,$_user,$_conf,$_args,$event)
{ $_sModule = "sdContestMWUser"; $_sUser = $_sModule.":Param-User:".var_dump($_user); // jrCore_logger("INF", $_sUser, ""); // //2017.02.17: TEST: How to open JS Message Window after user login? : $_aJS = array("alert('TEST: Alert On SignIn');"); jrCore_create_page_element('javascript_ready_function', $_aJS ); // javascript_ready_function // javascript_footer_function jrCore_logger("INF", $_sModule.":DEBUG:JS:05:23:JS=".var_dump($_aJS), ""); // return $_data; }
We have tested both "javascript_footer_function" and "javascript_ready_function", everything seems correct, but still no JS alert box firing.
--
Seems like we are missing something to make the JS fire consistently after user SignIn?
updated by @softdesigns: 02/18/17 03:04:57AM
SteveX
SteveX
@ultrajam
7 years ago
2,584 posts
Does it appear in your html source?


--
¯\_(ツ)_/¯ Education, learning resources, TEL, AR/VR/MR, CC licensed content, panoramas, interactive narrative, sectional modules (like jrDocs), lunch at Uni of Bristol. Get in touch if you share my current interests or can suggest better :)
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
Thanks for the reply:
We searched “index” and “user profile" pages for "TEST:" did not find the JS code :(
--
From the last code above, onto which page should the JS be written?
Also jrCore_logger does not show the value of $_aJS
Could the JS code be lost because of redirect after SignIn?
updated by @softdesigns: 02/18/17 03:15:51AM
SteveX
SteveX
@ultrajam
7 years ago
2,584 posts
Try logging in and look at the source of the page you end up on. It will only be written to the page after login, not on any other.


--
¯\_(ツ)_/¯ Education, learning resources, TEL, AR/VR/MR, CC licensed content, panoramas, interactive narrative, sectional modules (like jrDocs), lunch at Uni of Bristol. Get in touch if you share my current interests or can suggest better :)
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
The page after SignIn goes to logged in users profile.
Searched page source, and that page does not contain our JS code below:
	//2017.02.17: TEST:  How to open JS Message Window after user login? :
	$_aJS = array("alert('TEST: Alert On SignIn');");
	jrCore_create_page_element('javascript_ready_function', $_aJS );
Quite strange, as all the code seems correct, so it should simply display a JS alert box?
--
Seems a bit difficult to execute JS after SignIn
--
We did clear cache, but could this have anything to do with code cached, or not up to date?
We must be missing something?
updated by @softdesigns: 02/18/17 04:57:40AM
SteveX
SteveX
@ultrajam
7 years ago
2,584 posts
In that case it might be difficult to do that way.

Could you direct the user to a different page on their profile on login (one that only they will see), and there provide a button to continue to their profile?

If so, take a look at the TOS module which does something similar.


--
¯\_(ツ)_/¯ Education, learning resources, TEL, AR/VR/MR, CC licensed content, panoramas, interactive narrative, sectional modules (like jrDocs), lunch at Uni of Bristol. Get in touch if you share my current interests or can suggest better :)
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
Thanks for the reply:
Yes, your idea is good :)
We will work on your advice and try update code method to "redirect" User to new page.
It may take some time, but we will update this post with new results...
Great Support...
updated by @softdesigns: 02/18/17 06:32:58AM
michael
@michael
7 years ago
7,715 posts
Trying to figure this out now.

--edit--
The listener is happening BEFORE the page is redirected, so its the wrong listener to use.

The flow we have is:
* user enters their login details.
* details are verified for authenticity
* 'login_success' is fired
* page is refreshed and redirected to the landing page location.

so trying to add JS at this location isnt going to work because the page is not being created.

Going to look into adding a flag during 'login_success' that we can check for on page load. If the flag is found, then add the javascript to the page about to be created, then delete the flag.

Expect that to work. Will report back with a working example.
updated by @michael: 02/18/17 05:02:50PM
michael
@michael
7 years ago
7,715 posts
This works:
in the include.php file for your module (I called this one xxAutojs you could call yours anything)

/**
 * meta
 */
function xxAutojs_meta(){
    $_tmp = array(
        'name'        => 'Auto Js',
        'url'         => 'autojs',
        'version'     => '1.0.0',
        'developer'   => 'The Jamroom Network, &copy;' . strftime('%Y'),
        'description' => 'Trigger javascript after a user logs in',
        'category'    => 'custom',
        'license'     => 'mpl'
    );
    return $_tmp;
}

/**
 * init
 */
function xxAutojs_init(){
    jrCore_register_event_listener('jrUser', 'login_success', 'xxAutojs_login_success_listener');
    jrCore_register_event_listener('jrCore', 'profile_template', 'xxAutojs_profile_template_listener');
    return true;
}

function xxAutojs_profile_template_listener($_data, $_user, $_conf, $_args, $event)
{ $onLoginJs = jrCore_get_temp_value('xxAutojs', 'login_success_' . $_user['_user_id']); if ($onLoginJs) { jrCore_create_page_element('javascript_ready_function', $onLoginJs); jrCore_delete_temp_value('xxAutojs', 'login_success_' . $_user['_user_id']); } } function xxAutojs_login_success_listener($_data, $_user, $_conf, $_args, $event)
{ // $_sModule = "sdContestMWUser"; // $_sUser = $_sModule . ":Param-User:" . var_dump($_user); // // // jrCore_logger("INF", $_sUser, ""); // //2017.02.17: TEST: How to open JS Message Window after user login? : $_aJS = array("alert('TEST: Alert On SignIn');"); // jrCore_create_page_element('javascript_ready_function', $_aJS); // // javascript_ready_function // // javascript_footer_function // jrCore_logger("INF", $_sModule . ":DEBUG:JS:05:23:JS=" . var_dump($_aJS), ""); // jrCore_set_temp_value('xxAutojs', 'login_success_' . $_user['_user_id'], $_aJS); return $_data; }

The concept:
* when the user logs in a temp value is set for them using their _user_id
* they are redirected to their profile
* when a profile view is viewed, the 'profile_template' listener fires and checks if there is a temp value set for this user, if there is fire the javascript.
* after the javascript has been fired, delete the temp value so the javascript isnt fired again.

If you're redirecting anywhere after the login, then you'd need to choose a listener that exists at that location to listen for the temp value.

You could either set the javascript to be fired at the 'login_success' stage, or just put a TRUE flag there and put all the javascript into the 'profile_template' function.

Your choice.
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
Thanks for the reply: Your detailed explanation makes sense...
Will study and test your latest code above...
updated by @softdesigns: 02/20/17 02:31:03AM
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
Great Support - Last code above tested successfully.
--
More forum code samples, help create more JR pro level coders...
Great to promote more samples, and deeper study of JR platform coding :)
--
Resolved...
michael
@michael
7 years ago
7,715 posts
Jamroom could use more developer level users in the community. Happy to help get you up to speed @softdesigns

keep the questions coming. :)
michael
@michael
7 years ago
7,715 posts
I've setup a video streaming server channel on https://www.liveedu.tv/jamroomdev

It tweets to https://twitter.com/jamroomdev. whenever it goes live.

I'm going to try out solving problems while streaming it to see if its useful at all to anyone. See how it goes.
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
@michael : Your new idea is cool : https://www.liveedu.tv/jamroomdev
--
We hope to be involved when time allows - please let us know next steps.
We do not have twitter, so please keep us posted via email or JR Forum.
--
After many years of research:
We feel JR Platform is the most Flexible and Powerful Social Media CMS currently available :)
--
We are excited about the opportunity to Study and Promote JR Platform Coding.
--
thanks,
Soft-Designs, Inc.
People First, software second - Since 1985
updated by @softdesigns: 02/22/17 02:50:07AM
michael
@michael
7 years ago
7,715 posts
Cool. We're working on getting our video education series cleaned up first, then can figure out if times for streaming if its an idea the community has interest in. :)

Tags