General developer forum

Is there a page-view like event (or trigger) for programmatically login a user?

 
Tim at Lone Pine Koala Sanctuary
Re: Is there a page-view like event (or trigger) for programmatically login a user?
Group Core developersGroup Documentation writersGroup Particularly helpful MoodlersGroup Plugin developers

That is the right hook.

If a user is not logged in, they will be redirected to the login page.

Then that hook can run.

Then they will be redirected back to where they came from.

 
Average of ratings: -
Picture of wz z
Re: Is there a page-view like event (or trigger) for programmatically login a user?
 

I want to programmatically login a certain user according to a username&uniquetoken mapping database table. My step is as below:

  1. When a user access Moodle site in a special browser, I can get the uniquetoken of the user without Moodle's context;
  2. In the pre_login_page hook of my custom authentication plugin, I query the username from the database table where username=uniquetoken, then pass it to the function user_login(), which just simply return true.

Will that work?

 
Average of ratings: Useful (1)
Picture of Farhan Karmali
Re: Is there a page-view like event (or trigger) for programmatically login a user?
Group Plugin developersGroup Testers

That's correct , I had done it in the same way

 
Average of ratings: -
Picture of wz z
Re: Is there a page-view like event (or trigger) for programmatically login a user?
 

Great. But my code does not work. Would you please share your code? Or send it to my mail: wzzhang@shisu.edu.cn Thank you very much!

 
Average of ratings: -
Picture of Farhan Karmali
Re: Is there a page-view like event (or trigger) for programmatically login a user?
Group Plugin developersGroup Testers

Hi


My code is something like this

if(isset($_REQUEST['token']))
        {
             $token = $_REQUEST['token'];
             $userid = $DB->get_field('external_tokens','userid',array('token'=>$token));
            
             $user = $DB->get_record('user',array('id'=>$userid));
           
             complete_user_login($user);
             $urltogo =  $SESSION->wantsurl ?  $SESSION->wantsurl : $CFG->wwwroot.'/';
             redirect($urltogo);
        }


let me know if that works for you

 
Average of ratings: Useful (1)
Picture of wz z
Re: Is there a page-view like event (or trigger) for programmatically login a user?
 

Thank you for your tip very much!  It works now!

 
Average of ratings: -