Login to Moodle after registration

I'm trying to make my first Moodle autorun extension where I want to confirm and log in right after registration.

I changed the user_signup function in the "email" auth plugin as follows:

\core\event\user_created::create_from_userid($user->id)->trigger();

$DB->set_field("user", "confirmed", 1, array("id"=>$user->id));

$user = get_complete_user_data('username', $username);
$DB->set_field("user", "firstaccess", time(), array("id"=>$user->id));
$DB->set_field("user", "lastlogin", 0, array("id"=>$user->id));
update_user_login_times($user);

complete_user_login($user);
redirect("$CFG->wwwroot/enrol/index.php?id=2");

      

It works as long as the user becomes registered and verified. But as far as login goes, I get the following error:

core \ session \ manager :: login_user () must be an instance of stdClass, boolean given

I might be acting silly here, but I don't know how I can log in as a new user here. Any help would be very sorry. Thank!

+3


source to share


1 answer


After creating a user account, enter the username and password that you entered and then follow these steps.



if ($user = authenticate_user_login($username, $password)) {
/// Let get them all set up.
        complete_user_login($user);
        redirect($CFG->wwwroot . 'URL you want');
}

      

+1


source







All Articles