General developer forum
Is there a page-view like event (or trigger) for programmatically login a user?
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.
I want to programmatically login a certain user according to a username&uniquetoken mapping database table. My step is as below:
- When a user access Moodle site in a special browser, I can get the uniquetoken of the user without Moodle's context;
- 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?
Great. But my code does not work. Would you please share your code? Or send it to my mail: firstname.lastname@example.org Thank you very much!
My code is something like this
$token = $_REQUEST['token'];
$userid = $DB->get_field('external_tokens','userid',array('token'=>$token));
$user = $DB->get_record('user',array('id'=>$userid));
$urltogo = $SESSION->wantsurl ? $SESSION->wantsurl : $CFG->wwwroot.'/';
let me know if that works for you