Skip to content

Auth login() uses hardcoded id in ORDER BY instead of configured primary key #44

@atifmustaffa

Description

@atifmustaffa

Hi 👋

Summary

When using Leaf\Auth with a custom primary key (like p_id), the login fails because the internal DB helper uses a hardcoded ORDER BY id ASC inside first() causes:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'order clause'

Configuration

auth()->config([
  'db.table' => 'pengguna',
  'id.key' => 'p_id',
  'password.key'  => 'p_password'
]);

Login code

$data = request()->postData();

$success = auth()->login([
  'p_username' => $data['username'],
  'p_password' => $data['password']
]);

Where it breaks

Inside Auth::login():

$user = $this->db->select($table)->where($credentials)->first();

This calls Db::first():

public function first()
{
    $this->query .= ' ORDER BY id ASC LIMIT 1'; // <- hardcoded "id"
    return $this->fetchAssoc();
}

Expected Behavior

first() should use the configured primary key:

  • From Auth config (id.key)
  • Or simply use ->orderBy(Config::get('id.key'), 'asc')->limit(1)->fetchAssoc();

This should also apply to every other first() call inside Leaf\Auth

Environment

  • LeafPHP version: 4.0
  • Leaf\Db version: 4.0
  • Leaf\Auth version: 4.2
  • PHP version: 8.2
  • Database: MySQL

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions