I Have AuthController, with this code
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!');
}
and i Hash with my Own Hash,
and when i dump the $credentials
, the value is correct.
but when i dump Auth::attempt($credentials)
i got false
result.
CodePudding user response:
Using your own hash function for authentication isn't as straight forward as implementing a custom hash function and dropping it in place.
The entire process is more involved.
First, you need to create a Hasher class that implements Illuminate\Contracts\Hashing\Hasher interface.
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.
};
}
Then implement a custom HashManager
that includes a factory for the CustomHash
driver.
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') ?? []);
}
}
Then implement a ServiceProvider
to use your custom HashManager
for driving custom hashing functionality.
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'];
}
}
Then register App\Hashing\CustomHashServiceProvider::class
in place of Illuminate\Hashing\HashServiceProvider::class
in config/app.php
Finally in config/hashing.php
, configure your application to use the custom driver.
'driver' => 'custom',