• Resolved afsangujarati93

    (@afsangujarati93)


    Disclaimer: I am completely new to WP and PHP and I am in need of some guidance.

    My goal is to execute an AJAX request right after a user is successfully logged in. I need help as to where I should place my AJAX (GET) request so that it executes appropriately.

    I read some articles online about making AJAX calls in WP but I couldn’t figure out how I can place it in a way that it executes right after the user logs in.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @afsangujarati93

    Are you using the UM Login form or the standard WP Login form?

    Regards,

    Thread Starter afsangujarati93

    (@afsangujarati93)

    UM Login form I guess. How do I confirm though?

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @afsangujarati93

    UM Login form is generated with the Login page. http://www.yoursite.com/login/

    Regards,

    Thread Starter afsangujarati93

    (@afsangujarati93)

    Yes, that’s the one I am using.

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @afsangujarati93

    Why not use the wp_remote_get function to trigger get request in PHP? It’s impossible to add the javascript to be added after the successful Login.

    Regards,

    Thread Starter afsangujarati93

    (@afsangujarati93)

    I tried that but apparently, it didn’t work for what I am trying to achieve. Maybe you can suggest an alternative.

    To give you a bit of context. When the user successfully logins into the WP website through the UM plugin, I was to make an AJAX (GET) to another website and log the user into that website simultaneously.

    I have the GET request ready for the other website, however, it only works if it’s triggered from the client-side. I tried to make that GET request to the other website through the PHP after the user has successfully logged in, but it didn’t work. My understanding of it not working is that when I make that GET call from the PHP, it’s a backend call and it doesn’t share the same client (Chrome browser) as the WP website.

    Anyways, my goal is that I want to log the user into another website after the user has successfully logged into the WP website. Any suggestions/alternatives?

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @afsangujarati93

    Have you tried attaching it to the wp_login action hook?

    e.g.

    add_action("wp_login", function( $user_login, $user ){
          
       if( isset( $_REQUEST['um_login_success'] ) ){
              echo "<script>// your ajax request here. reload the page without the um_login_success from the URL with the window.location.href after ajax request </script>";
              exit;
       }  
    },10, 2);

    Now, go to the User Role settings, in the Login options >
    Action to be taken after login > Redirect to URL, you need to add the um_login_success to the custom redirect URL e.g. /homepage/?um_login_success=true. In this way, you will be able to trigger the javascript once the user logged in.

    I haven’t tested this but I hope this gives you an idea of how I would implement it.

    Regards,

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @afsangujarati93

    This thread has been inactive for a while so we’re going to go ahead and mark it Resolved. Please feel free to re-open this thread by changing the Topic Status to ‘Not Resolved’ if any other questions come up and we’d be happy to help. 🙂

    Regards,

    Thread Starter afsangujarati93

    (@afsangujarati93)

    Hey Champ Camba,

    Thank you for the nudge in the right direction. Instead of creating a new action and redirecting after login, I ended up adding the following function to class-user.php:

    function login_to_other_website($user_name, $user_password){
    
    	$url = OTHER_WEBSITE_URL;
    	$url = rtrim($url,"/");
    
    	$api_url = $url . "/loginapi";
    	$authorization = LOGIN_API_KEY;
    
    	$final_url = $api_url . "?username=" . $user_name . "&password=" . $user_password . "&Authorization=" . $authorization;
    
    	$data = '{"username": %s, "password": %s, "Authorization": %s}';
    	$data = sprintf($data, $user_name, $user_password, $authorization);
    
    	$logout_url = $url . "/logoutapi";
    	
    	echo "<script type='text/javascript'>
    			const logtoutHttp = new XMLHttpRequest();
    			logtoutHttp.open('GET', '$logout_url', false);
    			logtoutHttp.withCredentials = true;
    			logtoutHttp.send(null);
    
    			const loginHttp = new XMLHttpRequest();
    			loginHttp.open('GET', '$final_url', false);
    			loginHttp.withCredentials = true;
    			loginHttp.send(null);
    
    			console.log(loginHttp.responseText);
    			window.location.href = '/';
    		</script>";
    	exit;
    }

    and called this function from um-actions-login.php > um_user_login (function) after auto_login as follows:

    UM()->user()->login_to_other_website(um_user( 'user_login' ), $args['user_password']);

    I understand that this would interfere with the configured redirection, but this logs in the user into the other website without the user noticing anything explicitly.

    Again, thank you for your help. 🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.