title: Moodle Authentication Plugin. What am I missing?
body: <p>I am trying to manually authenticate an user in moodle using authentication plugins, but I don't understand everything that is needed for the whole process to work. So, some suggestions on what I am missing here would be very helpful!</p>
  
  <p>I created the plugin, enabled it, and it worked, but only for SOME cases, and that is where my problem lies. My guess is that at some point I need to actually call a function that saves user information in the moodle database. But again, I'm not sure where it is, or how it works. So... experts, give me a hand here.</p>
  
  <p>Here are the two functions I changed on my authentication plugin. (auth.php)</p>
  
  <pre><code>function loginpage_hook() {
      global $CFG, $DB, $user, $frm, $errormsg;
      $IsAuthenticated = false;
      if(isset($_COOKIE["AUTHENTICATION_KEY"])){
          $json = file_get_contents("WWW.WEBSERVICEURL.COM",true); 
          //getting the file content
          $decode = json_decode($json, true);
          //getting the file content as array  
          if($decode["AuthFlag"]){
              $ucUser = $decode["Username"];
              $user = $DB-&gt;get_record('user', array('username'=&gt;$ucUser, 'mnethostid'=&gt;$CFG-&gt;mnet_localhost_id));
              $frm-&gt;username = $ucUser;
              $IsAuthenticated = true;
          }
      }
      if(!$IsAuthenticated &amp;&amp; empty($frm-&gt;username)){
          $errormsg = ".";
      }
  }
  
  /**
   * Returns true if the username and password work or don't exist and false
   * if the user exists and the password is wrong.
   *
   * @param string $username The username
   * @param string $password The password
   * @return bool Authentication success or failure.
   */
  function user_login ($username, $password) {
      global $CFG, $DB, $user;
      if(!$user){ return false; }
  
      if(isset($_COOKIE["AUTHENTICATION_KEY"])){
          $json = file_get_contents("WWW.WEBSERVICEURL.COM", true); 
          //getting the file content
          $decode = json_decode($json, true);
          //getting the file content as array  
          if($decode["AuthFlag"]){
              $ucUser = $decode["Username"];
              if($user-&gt;username = $ucUser){ return true; }
          }
      }
  
      return false;
  }
  </code></pre>