title: I can't figure out why my UserModel gives an error in CodeIgniter 4
body: <p>I'm trying to get Oauth2 to work with CodeIgniter, but I keep running into a 500 error when trying to post request with Postman.</p>
  <p>I've found that if I comment out the instantiation of the new UserModel, the error quits happening.</p>
  <p>User.php(Controller):</p>
  <pre><code>&lt;?php namespace App\Controllers;
  
  use \App\Libraries\Oauth;
  use \OAuth2\Request;
  use CodeIgniter\API\ResponseTrait;
  use App\Models\UserModel;
  
  
  class User extends BaseController
  {
      use ResponseTrait;
  
      public function login(){
          $oauth = new Oauth();
          $request = new Request();
          $respond = $oauth-&gt;server-&gt;handleTokenRequest($request-&gt;createFromGlobals());
          $code = $respond-&gt;getStatusCode();
          $body = $respond-&gt;getResponseBody();
          return $this-&gt;respond(json_decode($body), $code);
         
      }
  
      public function register(){
          helper('form');
          $data = [];
  
          if($this-&gt;request-&gt;getMethod() != 'post')
              return $this-&gt;fail('Only post request is allowed');
  
          
             
          $validation =  \Config\Services::validation();
  
          
          
          $rules = array(
              'firstname' =&gt; 'required|min_length[3]|max_length[20]',
              'lastname' =&gt; 'required|min_length[3]|max_length[20]',
              'email' =&gt; 'required|valid_email',
              //'email' =&gt; 'is_unique[users.email]',
              'password' =&gt; 'required|min_length[8]',
              'password_confirm' =&gt; 'matches[password]',
          );
    
          if(!$this-&gt;validate($rules)){
              return $this-&gt;respond($validation-&gt;getErrors());
              
          }else{ 
              
              $model = new UserModel();
              
              $data = [
              'firstname' =&gt; $this-&gt;request-&gt;getVar('firstname'),
              'lastname' =&gt; $this-&gt;request-&gt;getVar('lastname'),
              'email' =&gt; $this-&gt;request-&gt;getVar('email'),
              'password' =&gt; $this-&gt;request-&gt;getVar('password'),
              ];
   
              
              $user_id = $model-&gt;insert($data);
              $data['id'] = $user_id;
              unset($data['password']);
              
  
              return $this-&gt;respondCreated($data);
             
          }
      }
  }
  </code></pre>
  <p>UserModel.php:</p>
  <pre><code>&lt;?php namespace App\Models;
  
  use CodeIgniter\Model;
  
  class UserModel extends Model{
      
    protected $table = 'users';
    protected $primaryKey = 'id';
    protected $allowedFields = ['firstname','lastname','password','email'];
    
    protected $beforeInsert = ['beforeInsert'];
    protected $beforeUpdate = ['beforeUpdate'];
  
    
    protected function beforeInsert(array $data){
      $data = $this-&gt;passwordHash($data);
      return $data;
    }
  
    protected function beforeUpdate(array $data){
      $data = $this-&gt;passwordHash($data);
      return $data;
    }
  
  
    protected function passwordHash(array $data){
      if(isset($data['data']['password']))
        $data['data']['password'] = password_hash($data['data']['password'], PASSWORD_DEFAULT);
  
      return $data;
    }
  
  }
  </code></pre>
  <p>I don't know if the problem goes into the Oauth stuff, but if it does, please let me know and I can provide other code.</p>
  <p>Such as Oauth.php from the login() function:</p>
  <pre><code>&lt;?php namespace App\Libraries;
  
  //use \OAuth2\Storage\Pdo;
  use \App\Libraries\CustomOauthStorage;
  
  
  class Oauth{
    var $server;
  
    function __construct(){
      $this-&gt;init();
    }
  
    public function init(){
      $dsn = getenv('database.default.DSN');
      $username = getenv('database.default.username');
      $password = getenv('database.default.password');
  
      $storage = new CustomOauthStorage(['dsn' =&gt; $dsn, 'username' =&gt; $username, 'password' =&gt; $password]);
      $this-&gt;server = new \OAuth2\Server($storage);
      $this-&gt;server-&gt;addGrantType(new \OAuth2\GrantType\UserCredentials($storage));
    }
  }
  </code></pre>
  <p>This code is producing a server 500 error when posting in Postman. The request works when I comment out UserModel instantiation. And while I'm on this subject, would anyone happen to know why I have to comment out the password verification line in the User.php rules array in order to get that to work?</p>
  <p>Thanks for any help.</p>