gpt4 book ai didi

php - Auth::attempt 返回 true,Auth::check() 返回 false

转载 作者:行者123 更新时间:2023-12-05 05:33:38 25 4
gpt4 key购买 nike

我正在使用 Laravel 9 身份验证。这是一个成功的登录,但在主页上,它没有登录。所以我期待它进行身份验证。

登录 Controller

if (Auth::attempt([
'x_username' => $data['username'],
'x_user_password' => $data['password']
])) {
return true; //login success
} else {
return false;
}

仪表板 Controller (显示 2 个)

if(Auth::check()) {
echo "1"; //not happened, but login before is success?
} else {
echo "2";
}

$user = Auth::user();
var_dump($user); //it shows NULL

模型

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;

protected $table = 'tbl_x_user';

protected $fillable = [
'x_username',
'x_user_email',
'x_user_password',
];

protected $hidden = [
'x_user_password',
'remember_token',
];

protected $casts = [
'email_verified_at' => 'datetime',
];

public function getAuthPassword()
{
return $this->x_user_password;
}
}

auth.php

'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
],
'password_timeout' => 10800,

kernels.php

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
protected $middleware = [
// \App\Http\Middleware\TrustHosts::class,
\App\Http\Middleware\TrustProxies::class,
\Illuminate\Http\Middleware\HandleCors::class,
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];

protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],

'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];

protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];
}

注意事项

  • 使用和不使用禁用增量更改主键和主键名称均无效
  • 在/storage/framework/sessions/中创建 session
  • session.php 未编辑(默认为 laravel)
  • 我的密码字段使用 varchar(255)

最佳答案

你可以试试这个:

在 Blade 中:

@auth
// User is Authenticated
@else
// User is Not Authenticated
@endauth

关于php - Auth::attempt 返回 true,Auth::check() 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73781424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com