gpt4 book ai didi

php - Auth::user() 在 Laravel 5.8 中返回 null

转载 作者:行者123 更新时间:2023-12-04 15:45:26 24 4
gpt4 key购买 nike

我在 Laravel 5.8.10 项目中遇到身份验证问题。我没有使用 Laravel 创建的用于身份验证的默认表单。当我在浏览器中访问 URL/仪表板时,通常用户会在登录时被重定向。该应用程序无论如何都允许它。此外,当我使用 Auth::user() 时,它返回 null。

当我键入无效的用户名和密码时,它不会从登录屏幕传递。当我键入无效凭据时,它会重定向到仪表板。通过仪表板 View 访问 URL 的问题也仍然存在。就好像您不需要身份验证就可以访问该路由。

注意:我的 .env 文件中有一个变量 PASSWORD_HASH 用于启用或禁用密码加密。

用户模型

namespace App\Entities;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Authenticatable
{
use Notifiable;
use SoftDeletes;

protected $table = "users";
public $timestamps = true;

protected $fillable = [
'cpf', 'name', 'phone', 'birth', 'gender', 'notes', 'email', 'password', 'status', 'permission'
];

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

public function groups()
{
return $this->belongsToMany(Group::Class, 'user_groups');
}

public function setPasswordAttribute($value)
{
$this->attributes['password'] = env('PASSWORD_HASH') ? bcrypt($value) : $value;
}
}

config/auth.php

return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],

'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Entities\User::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
];

仪表板 Controller

public function auth(Request $request)
{
$data = [
'email' => $request->get('username'),
'password' => $request->get('password')
];
try {
if (env('PASSWORD_HASH')) {
Auth::attempt($data, false);
} else {
$user = $this->repository->findWhere(['email' => $request->get('username')])->first();

if (!$user)
throw new Exception("O e-mail informado é inválido. PEEEEN!");
if ($user->password != $request->get('password'))
throw new Exception("A senha informada é inválida. PEEEEN!");
Auth::login($user);
}
return redirect()->route('user.dashboard');
} catch (Exception $e) {
return $e->getMessage();
}
}

路线

Route::get('/login', ['uses' => 'Controller@fazerlogin']);
Route::post('/login', ['as' => 'user.login', 'uses' => 'DashboardController@auth']);
Route::get('/dashboard', ['as' => 'user.dashboard', 'uses' => 'DashboardController@index']);

查看登录

<section id="conteudo-view" class="login">
<h1>Investindo</h1>
<h3>O nosso gerenciador de investimento</h3>
{!! Form::open(['route' => 'user.login', 'method' => 'post']) !!}
<p>Acesse o sistema</p>
<label>
{!! Form::text('username', null, ['class' => 'input', 'placeholder' => "Usuário"]) !!}
</label>
<label>
{!! Form::password('password', ['placeholder' => 'Senha']) !!}
</label>
{!! Form::submit('Entrar') !!}
{!! Form::close() !!}
</section>

.env

PASSWORD_HASH=false

这个想法是,当注册用户时它为 false 时,它​​停止加密密码,而当为 true 时,进行加密。这是有效的。

数据库用户

https://pasteboard.co/IcMC2ds.png

最佳答案

  1. 在没有授权的情况下停止重定向到仪表板使用授权中间件路由

    Route::middleware(['auth'])->group(function () {    
    Route::get('/dashboard', ['as' => 'user.dashboard', 'uses' => 'DashboardController@index']);
    });
  2. env 返回字符串,而不是 bool 值,所以使用 env('PASSWORD_HASH') == 'true' 检查密码哈希是否启用

  3. 使用 loginUsingId() 手动登录。

    if(env('PASSWORD_HASH') == 'true') {
    Auth::attempt($data, false);
    } else {
    $user = User::where('email', $request->username)->where('password', $request->password)->first();

    if(!$user){
    throw new Exception("O e-mail informado é inválido. PEEEEN!");
    } else {
    Auth::loginUsingId($user->id);
    //redirect
    }
    }

关于php - Auth::user() 在 Laravel 5.8 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55945769/

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