Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
casey977 opened this issue Feb 28, 2024 · 1 comment
Open

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

casey977 opened this issue Feb 28, 2024 · 1 comment

Comments

@casey977
Copy link

casey977 commented Feb 28, 2024

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,

];
@eznix86
Copy link

eznix86 commented Mar 5, 2024

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants