Skip to content
Advertisement

How Can I Get False in Auth::attempts Laravel

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.

Advertisement

Answer

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 IlluminateContractsHashingHasher interface.

namespace AppHashing;

use IlluminateContractsHashingHasher as HasherContract;
use IlluminateHashingAbstractHasher;
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 AppHashing;

use IlluminateHashingHashManager;

class CustomHashManager extends HashManager
{
    /**
     * Create an instance of the Custom hash Driver.
     *
     * @return AppHashingCustomHasher
     */
    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 AppHashing;

use IlluminateContractsSupportDeferrableProvider;
use IlluminateSupportServiceProvider;
use IlluminateHashingHashingManager;

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 AppHashingCustomHashServiceProvider::class in place of IlluminateHashingHashServiceProvider::class in config/app.php

Finally in config/hashing.php, configure your application to use the custom driver.

    'driver' => 'custom',
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement