[Resolved] Problem with Auto-login after Submitting a User CRED Registration form

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 13 replies, has 3 voices.

Last updated by stephaneL-3 1 month ago.

Assigned support staff: Shane.

Author
Posts
#1637737

Tell us what you are trying to do?

Trying to auto-login a user after they completed the User CRED registration form
I have used the lates Code Snippet available reporte in the following thread:
https://toolset.com/forums/topic/the-auto-login-snippet-doesnt-work-anymoe/
(I also tried many other options...)

Is there any documentation that you are following?

Again tried many options from various support thread for a few days now,
disabled most plugins except Toolset/Elementor,
changed the them (Hello Elementor) to TwentyTwenty
WP Error Log isn't reporting anything

All I have is a Code Snippet creating the username as Firstname/Surname
And then the Following Code for the Auto-login (Tried in Custom Code and function.php)
The User is setting the password via the form (not autogenerated)

/**
* Auto-login new CRED user
*/

add_action( 'cred_save_data', 'func_tssupp_cred_autologin', 10, 2);
function func_tssupp_cred_autologin( $post_id, $form_data ){
  
    if ( $form_data['id'] == 486  ) { // Edit as required
        if ( !empty( $_POST['user_email'] ) && !empty( $_POST['user_pass'] ) ) {
           
          $user = get_user_by( "email", $_POST['user_email'] );
             
            // get the user credentials from the $_POST object
            $user = array(
                'user_login'    =>   $user->user_login,
                'user_password' =>   $_POST['user_pass'],
                'remember'      =>   true
            );
            $login = wp_signon( $user, false );
  
            if ( is_wp_error($login) ) {
                error_log( $login->get_error_message() );
            }
        }
    }
}

Is there a similar example that we can see?
Not that I know of.

What is the link to your site?
hidden link

#1638297

Hello Minesh,

Until you are ready, please find in addition the User Form Code.
I could see you have assisted another user a week agon on this same topic, I have tried the code there, but unfortunately cannot get it to work on my site 🙁 I really appreciate your assistance. The site is to submit projects, however the user needs to create an account first, but having the extra step of login in between the user accoutn creation and then the Post-type form submission is not ideal, we need the user to be auto login. ( I must say thi sis pretty standard procedure for most site, I will make a request to include if possible in future Toolset update as this seems to be also a recurrent request.)
Until then I hope you can assit me, in advance thank you for your time.

[creduserform]
<div class="container-fluid">
	<div class="row">
		<div class="col-md-12">
			[cred_field field='form_messages' class='alert alert-warning']
		</div>
	</div>
	<div class="row">
		<div class="form-group col-md-4">
			<label>Adresse Email</label>
			[cred_field field='user_email' class='form-control' output='bootstrap']
		</div>
		<div class="form-group col-md-4">
			<label>Prénom</label>
			[cred_field field='first_name' class='form-control' output='bootstrap']
		</div>
		<div class="form-group col-md-4">
			<label>Nom</label>
			[cred_field field='last_name' class='form-control' output='bootstrap']
		</div>
	</div>
	<div class="row">
		<div class="form-group col-md-4">
			<label>Mot de passe</label>
          [cred_field field='user_pass' class='form-control' placeholder='Mot de passe' output='bootstrap']
          [cred_field field='user_pass2' class='form-control mdp2' placeholder='Repetez le Mot de passe' output='bootstrap']
		</div>
		<div class="form-group col-md-4">
			<label>Photo de Profil</label>
			[cred_field field='photo-profil' force_type='field' class='form-control' output='bootstrap']
          <small style="font-weight:normal;">(Les dimensions doivent être proportionnelles 1:1, minimum (250px/250px). Formats: jpg, gif, png)</small>
		</div>
		<div class="form-group col-md-4">
			<label>Lien Profil Linkedin</label>
			[cred_field field='profil-linkedin' force_type='field' class='form-control' output='bootstrap']
		</div>
	</div>
  <hr style="margin: 0px 0px 20px 0px;">
	<div class="row" style="margin-top: -10px;">
		<div class="form-group col-md-12">
			<label style="margin-bottom: -30px;">Mini Bio<br><small style="font-weight:normal;">Parlez-nous de vous...<br>(Non obligatoire. Vous pourrez saisir cette information ulterieurement dans votre profil)</small></label>
			[cred_field field='description' class='form-control Bio' output='bootstrap']
		</div>
	</div>
	<div class="row">
	</div>
	<div class="row">
		<div class="col-md-12">
			[cred_field field='form_submit' output='bootstrap' value='Créer mon compte' class='btn btn-primary btn-lg']
		</div>
	</div>
</div>
[/creduserform]
#1638299

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I am not sure why the code is not running on your install. As stated with the reference ticket you shared, I had shared the test site where the code was successfully running.

Can you please share the link of the page where I can see the registration form as well as access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1638387

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Indeed - there seems to be issue on your site but I do not able to locate it yet.

It seems to me that the issue is from custom role for which you created the form. I will try to check with default WordPress user and custom role created using access plugin and will check whats going on.

As the current role "porteur-projet" do not have much capabilities assigned as it set to user_level 0.

I need duplicator copy of your site and the same site I will try to deploy on my localhost or test server to check the issue further.

Can you please send me duplicator copy of your site:
=> https://toolset.com/faq/provide-supporters-copy-site/

I have set the next reply to private which means only you and I have access to it.

#1638445

Hi Minesh,

And thanks again for your time.

I am struggling getting the Duplicator Package to you .. I keep running into problem creating the packakge .. I have use duplicator many times before but for the first time no matter what I do it keeps aborting and showing the following message:

Host Build Interrupt
This server cannot complete the build due to host setup constraints.
To get past this hosts limitation consider the options below by clicking each section.

I am ccontacting the support for the hosting company but I know they will take forever to get back to me 🙁
Is there any way I can pack the db and files manually from the server and get you the package that way... would that be ok for you?
or else do you have another option I can try?

I am happy to change the User level to troubleshoot the site isn't live yet so it isn't a problem.
I have lost so much time already tryin gto troubleshoot this then the duplicatior etc... (nothing for you of course) just frustrated with the server issue now and seems anything I try I run into a new problem .. I know computers for you .... just very frustrated today.

Lmk if nay other option as it stand?
thank you
stephane

#1638525

Hello again,

I am downloading an Updraft Backup (db/File/plug etc..) would that be ok to test on your end?

Cheers
Stephane

#1638667

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I just checked with my test site by creating the custom role "role-auto-login" and it has user_level 0.

Then this is the user form:
=> hidden link

You can create a new user with my test site using the following link: hidden link
- As soon as you create a new user, it will be redirected to the home page and it will autologin as well.

If you want backend access, you can use: hidden link
User: demo
pass: Admin@123

I've used the same code and that code is added to "do-autologin" snippet which is added to "Custom Code" section of Toolset:
=> hidden link

So, as I do not see the issue yet, first of all, Can you please check with minimum test installation where only Toolset plugins are installed with default theme. -
Could you please try to resolve your issue by deactivating all third-party plugins as well as the default theme to check for any possible conflicts with any of the plugins or themes? - Do you see any difference?

if no, I need to check with your duplicator copy or Updraft Backup or other way why its not working.

#1638857

Hi Minesh,

Thank you for your reply.
Indeed checking your demo site it seems to work nicely (I wish!!)
I will try (again) to disabled everyting but Toolset and set to twentytwenty theme now.
Just to be sure tho I see you have commented out the bottom part of the Custom Code .. on purpose?

<?php
/**
 * Auto-login new CRED user
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
add_action( 'cred_save_data', 'func_tssupp_cred_autologin', 10, 3);
function func_tssupp_cred_autologin( $post_id, $form_data ){
  
    if ( $form_data['id'] == 486  ) { // Edit as required
        if ( !empty( $_POST['user_email'] ) && !empty( $_POST['user_pass'] ) ) {
           
          $user = get_user_by( "email", $_POST['user_email'] );
          
          do_action('wp_login', $user->user_login, $user);
         wp_set_current_user( $user->ID );
         wp_set_auth_cookie( $user->ID );
             
       /*     // get the user credentials from the $_POST object
            $user = array(
                'user_login'    =>   $user->user_login,
                'user_password' =>   $_POST['user_pass'],
                'remember'      =>   true
            );
            $login = wp_signon( $user, false );
         
          wp_set_current_user( $login->ID );
          wp_set_auth_cookie( $login->ID );
            get_currentuserinfo();
           
  
            if ( is_wp_error($login) ) {
                error_log( $login->get_error_message() );
            }
            */
        }
    }
}
#1638907

Hello again,
Also I have uploaded the updraft (DB+Files) backup on gg drive, please send me a private message request so I can share the link with you, thx
s

#1639911

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Great - Can you please share your findings with only Toolset plugins installed and with default theme.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1640277

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Stephane,

Minesh had assigned me this ticket to have a look for you.

I tried checking on the form but it seems that you are currently editing it which causes it to become locked.

I want to disable the redirection on your form as this could be why the login isn't working as intended.

Could you set the form to keep displaying so that I can continue checking on this for you ? I also noticed that you have another function that is creating a custom username for the users which could also be interfering with the login process.

Please let me know when you have change the settings on the form so that I can continue checking it.

Thanks,
Shane

#1640285

Hi Shane,

And thank you for your time with this.

I have done what you asked, sorry I was actually trying this exactly, but I have gone out of any toolset area, feel free to jump in, you can also disable plug and theme if needs be, the site isn't live or use so go ahead.
We really need this part in place for the sake of user process, so I appreciate your time on this.
I have disabled the other Custom code (Althouht tried disabling in the past without sucess)

Thanks
s

#1640411

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Stephane,

The autologin should now be working but the problem is that I had to disable your nicename function.

Perhaps you can use a lower priority hook to execute this nicename change.

Try using the hook below to perform the name change.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete
Please let me know if this helps.
Thanks,
shane

#1640767

My issue is resolved now.
Thank you!