Skip to content

attempt() never returns null, but returns blank booleans #2242

Open
@casey977

Description

@casey977

Subject of the issue

When I use auth() with attempt(), I'm always getting an HTTP OK (200) as a result with the following code. In spite of invalid credentials, and even a truncated table, attempt() never returns null, but a blank boolean, that is, blank/nothing when I do Log::debug($token), and "boolean" when I do Log::debug(gettype($token)). I use PostgreSQL.

Your environment

Q A
Bug? yes
New Feature? no
Framework Laravel
Framework version 10
Package version 10.44.0
PHP version 8.2.7

Steps to reproduce

I'm just making a basic system, with the given code. I'm still new to Laravel, but I think this is a bug.

Expected behaviour

I'm expecting attempt() to return null when checking credentials fail.

Actual behaviour

I get a blank boolean which in the provided code leads to HTTP 200.

controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Hash;
use Tymon\JWTAuth\Facades\JWTAuth;

use App\Models\Member;

class Login extends Controller {
    public function login() {
        try {
            $creds = request(['email', 'password']);
            $token = auth()->guard('member')->attempt($creds);

            if (is_null($token)) {
                return response()->json(['error' => 'Invalid credentials'], 401);
            } else {
                return response()->json(['token' => $token], 200);
            }
        } catch (Exception $error) {
            Log::error('Error logging in!');
            return response()->json(['error' => 'Error logging in!'], 500);
        }
    }
}

auth.php

<?php

return [

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'members',
    ],

    'guards' => [
        'web' => [
            'driver' => 'jwt',
            'provider' => 'members',
        ],
        'api' => [
            'driver' => 'jwt',
            'provider' => 'members',
        ],
        'member' => [
            'driver' => 'jwt',
            'provider' => 'members',
        ],
    ],

    'providers' => [
        'members' => [
            'driver' => 'eloquent',
            'model' => App\Models\Member::class,
        ],
    ],

    'passwords' => [
        'members' => [
            'provider' => 'members',
            'table' => 'password_reset_tokens',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],

    'password_timeout' => 10800,

];

Activity

eznix86

eznix86 commented on Mar 5, 2024

@eznix86

I think the guard should be 'api'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @casey977@eznix86

        Issue actions

          attempt() never returns null, but returns blank booleans · Issue #2242 · tymondesigns/jwt-auth