Skip to content

Auth::guard('jwt')->id() returns string instead of integer for sub claim. #294

@Kim-Munbeom

Description

@Kim-Munbeom

네, 요청하신 틀에 맞춰 php-open-source-saver/jwt-auth 패키지에서 $\text{ID}$$\text{string}$으로 반환되는 문제를 보고하는 $\text{GitHub}$ 이슈를 작성해 드립니다.


Subject of the issue

Auth::guard('jwt')->id() returns $\text{string}$ instead of $\text{integer}$ for $\text{sub}$ claim.


Your environment:

Q A
Bug? yes
New Feature? no
Framework Laravel
Framework version 11.46.0
Package version 2.8.2
PHP version 8.3.26

Steps to reproduce

  1. Ensure your database primary key for the users table is an $\text{INTEGER}$ (standard Laravel setup).
  2. Log in a user to generate a $\text{JWT}$.
  3. Access the authenticated user's $\text{ID}$ using the $\text{jwt}$ guard.
$user_id = Auth::guard('jwt')->id();

// Or using the helper function
$user_id_helper = auth('jwt')->id();
  1. Check the data type of the returned $\text{ID}$.
// Check the type of the returned ID
dd([
    'ID from auth()->id()' => auth()->id(),
    'ID from auth(\'jwt\')->id()' => auth('jwt')->id(),
    'Type of JWT ID' => gettype(auth('jwt')->id()),
    'ID from Auth::id()' => Auth::id(),
    'ID from Auth::guard(\'jwt\')->id()' => Auth::guard('jwt')->id(),
    'Type of JWT Guard ID' => gettype(Auth::guard('jwt')->id()),
]);

Expected behaviour

The $\text{ID}$ returned by $\text{Auth::guard('jwt')->id()}$ should match the data type of the user's primary key in the database, which is typically $\text{integer}$.

Expected output for type check:

...
'Type of JWT ID' => 'integer',
'Type of JWT Guard ID' => 'integer',

(assuming the default guard returns $\text{integer}$)


Actual behaviour

The $\text{ID}$ is returned as a $\text{string}$ (likely due to the $\text{sub}$ claim being read as a string from the $\text{JWT}$ payload), even when the database primary key is an $\text{integer}$.

Actual output from the $\text{dd()}$ in steps to reproduce:

array:6 [
  "ID from auth()->id()" => 1779851
  "ID from auth('jwt')->id()" => "1779851"
  "Type of JWT ID" => "string" // <-- ISSUE
  "ID from Auth::id()" => 1779851
  "ID from Auth::guard('jwt')->id()" => "1779851"
  "Type of JWT Guard ID" => "string" // <-- ISSUE
]

This requires explicit type casting ((int)) in application code when comparing the $\text{JWT}$ $\text{ID}$ to other $\text{integer}$ $\text{IDs}$, which can lead to subtle bugs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions