我怎样才能在 Auth::attempts Laravel 中得到 False
How Can I Get False in Auth::attempts Laravel
我有 AuthController,使用此代码
public function login(Request $request)
{
$credentials = $request->validate([
'email' => 'required|email:dns',
'password' => 'required'
]);
$credentials['password'] = hashPass($request['email'], $request['password']);
if (Auth::attempt($credentials)) {
$request->session()->regenerate();
return redirect()->intended('/dashboard')->with('loginSuccess', 'Login Berhasil');
}
return back()->with('loginError', 'Login Gagal!');
}
然后我用自己的哈希值进行哈希运算,
当我转储 $credentials
时,值是正确的。
但是当我转储 Auth::attempt($credentials)
时,我得到了 false
结果。
使用您自己的哈希函数进行身份验证并不像实现自定义哈希函数并将其放在适当位置那样简单。
整个过程比较复杂
首先,您需要创建一个实现 Illuminate\Contracts\Hashing\Hasher interface.
的 Hasher class
namespace App\Hashing;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Hashing\AbstractHasher;
use RuntimeException;
class CustomHasher extends AbstractHasher implements HasherContract
{
public function make($value, array $options = []); {
// perform custom routine.
}
/**
* Check the given plain value against a hash.
*
* @param string $value
* @param string $hashedValue
* @param array $options
* @return bool
*/
public function check($value, $hashedValue, array $options = []) {
// perform custom routine.
};
/**
* Check if the given hash has been hashed using the given options.
*
* @param string $hashedValue
* @param array $options
* @return bool
*/
public function needsRehash($hashedValue, array $options = []) {
// perform custom routine.
};
}
然后实施自定义 HashManager
,其中包括 CustomHash
驱动程序的工厂。
namespace App\Hashing;
use Illuminate\Hashing\HashManager;
class CustomHashManager extends HashManager
{
/**
* Create an instance of the Custom hash Driver.
*
* @return \App\Hashing\CustomHasher
*/
public function createCustomDriver()
{
return new CustomHasher($this->config->get('hashing.custom') ?? []);
}
}
然后实施 ServiceProvider
以使用您的自定义 HashManager
来驱动自定义哈希功能。
namespace App\Hashing;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
use Illuminate\Hashing\HashingManager;
class CustomHashServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('hash', function ($app) {
return new CustomHashManager($app);
});
$this->app->singleton('hash.driver', function ($app) {
return $app['hash']->driver();
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['hash', 'hash.driver'];
}
}
然后在config/app.php
中注册App\Hashing\CustomHashServiceProvider::class
代替Illuminate\Hashing\HashServiceProvider::class
最后在 config/hashing.php
中,配置您的应用程序以使用自定义驱动程序。
'driver' => 'custom',
我有 AuthController,使用此代码
public function login(Request $request)
{
$credentials = $request->validate([
'email' => 'required|email:dns',
'password' => 'required'
]);
$credentials['password'] = hashPass($request['email'], $request['password']);
if (Auth::attempt($credentials)) {
$request->session()->regenerate();
return redirect()->intended('/dashboard')->with('loginSuccess', 'Login Berhasil');
}
return back()->with('loginError', 'Login Gagal!');
}
然后我用自己的哈希值进行哈希运算,
当我转储 $credentials
时,值是正确的。
但是当我转储 Auth::attempt($credentials)
时,我得到了 false
结果。
使用您自己的哈希函数进行身份验证并不像实现自定义哈希函数并将其放在适当位置那样简单。
整个过程比较复杂
首先,您需要创建一个实现 Illuminate\Contracts\Hashing\Hasher interface.
的 Hasher classnamespace App\Hashing;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Hashing\AbstractHasher;
use RuntimeException;
class CustomHasher extends AbstractHasher implements HasherContract
{
public function make($value, array $options = []); {
// perform custom routine.
}
/**
* Check the given plain value against a hash.
*
* @param string $value
* @param string $hashedValue
* @param array $options
* @return bool
*/
public function check($value, $hashedValue, array $options = []) {
// perform custom routine.
};
/**
* Check if the given hash has been hashed using the given options.
*
* @param string $hashedValue
* @param array $options
* @return bool
*/
public function needsRehash($hashedValue, array $options = []) {
// perform custom routine.
};
}
然后实施自定义 HashManager
,其中包括 CustomHash
驱动程序的工厂。
namespace App\Hashing;
use Illuminate\Hashing\HashManager;
class CustomHashManager extends HashManager
{
/**
* Create an instance of the Custom hash Driver.
*
* @return \App\Hashing\CustomHasher
*/
public function createCustomDriver()
{
return new CustomHasher($this->config->get('hashing.custom') ?? []);
}
}
然后实施 ServiceProvider
以使用您的自定义 HashManager
来驱动自定义哈希功能。
namespace App\Hashing;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
use Illuminate\Hashing\HashingManager;
class CustomHashServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('hash', function ($app) {
return new CustomHashManager($app);
});
$this->app->singleton('hash.driver', function ($app) {
return $app['hash']->driver();
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['hash', 'hash.driver'];
}
}
然后在config/app.php
App\Hashing\CustomHashServiceProvider::class
代替Illuminate\Hashing\HashServiceProvider::class
最后在 config/hashing.php
中,配置您的应用程序以使用自定义驱动程序。
'driver' => 'custom',