Override laravel 4's authentication methods to use custom hasing function

后端 未结 4 677
长发绾君心
长发绾君心 2021-02-08 16:09

I have a table in my database with users. Their password are generated with my own custom hashing function.

How do i override the Authentication methods in laravel 4 to

4条回答
  •  伪装坚强ぢ
    2021-02-08 16:38

    @vFragosop was on the right path with extending Auth.

    There are a couple of ways to skin the cat and here is how I would do that without replacing the default Hasher class:

    Include in your app/routes.php or wherever:

    use Illuminate\Auth\Guard;
    Auth::extend("eloquent", function() {
        return new Guard(
            new \Illuminate\Auth\EloquentUserProvider(new CustomHasher(), "User"),  
            App::make('session.store')
        );
    });
    

    Create and autoload a CustomHasher class (i.e., app/libraries/CustomHasher.php):

    class CustomHasher extends Illuminate\Hashing\BcryptHasher {
        public function make($value, array $options = array())
        {
            ...
        }
        public function check($value, $hashedValue, array $options = array())
        {
            ...
        }
    }
    

    That's it.

提交回复
热议问题