Question

I can't seem to get sentry working. I keep getting this error: A hasher has not been provided for the user. Does anyone know what would make this happen?

The error log

I am running MAMP on OS X 10.9. I am using php 5.4.4 MCrypt is installed and enabled. This error occurs when attempting to hash the password when creating a new user. Our project uses the laravel Sentry plugin. Here is the controller:

<?php

use Auth, BaseController, Form, Input, Redirect, Sentry, View;

class AuthController extends BaseController {

        public function register()
        {
            return View::make('Auth.register');
        }

        public function handleRegister()
        {
               $validator = Validator::make(Input::all(), User::$rules);

                if ($validator->passes()) {
                    //The registration has passed, create a user

            $user = new User;
                    $user->first_name = Input::get('first_name');
                    $user->last_name = Input::get('last_name');
                    $user->email = Input::get('email');
                    $user->password = Hash::make(Input::get('password'));
                    $user->activated = 1;
                    $user->save();

            //grabbing the Sentry model of the user so we can save it to the appropriate group
            $sentryUser = Sentry::findUserByLogin($user->email);

            if (Input::get('userType') == 'Promoter')
            {
              $group = 'Promoters';
            }
            else
            {
              $group = 'Agents';
            }

            // Find the group using the group id
            $group = Sentry::findGroupByName($group);

            // Assign the group to the user
           $sentryUser->addGroup($group);

                    return Redirect::action('AuthController@login')->with('message', 'Thanks for registering!');
                } else {
                // validation has failed, display error messages   
                    return Redirect::action('AuthController@register')->with('message', 'The following errors occurred')->withErrors($validator)->withInput();

                }
        }

        /**
         * Display the login page
         * @return View
         */
        public function login()
        {
                return View::make('Auth.login');
        }

        /**
         * Login action
         * @return Redirect
         */
        public function handleLogin()
        {
                $credentials = array(
                        'email' => Input::get('email'),
                        'password' => Input::get('password')
                );

                try
                {
                        $user = Sentry::authenticate($credentials, false);

                        if ($user)
                        {
                                return Redirect::action('OfferController@offer');
                        }
                }
                catch(\Exception $e)
                {
                        return Redirect::action('AuthController@login')->withErrors(array('login' => $e->getMessage()));
                }
        }

        /**
         * Logout action
         * @return Redirect
         */
        public function logout()
        {
                Sentry::logout();

                return Redirect::action('AuthController@login')->with('message','You have been logged out');
        }

}

?>
Was it helpful?

Solution

The problem is when you configured Sentry to use User.php as your model it loses the Sentry hasher. The solution is to set the hasher when a user is registering

$user->setHasher(new Cartalyst\Sentry\Hashing\NativeHasher);

OTHER TIPS

A better alternative to @Dylan Pierce suggestion is to set the hasher directly in the constructor of your user model.

public function __construct()
{
    $this->setHasher(new \Cartalyst\Sentry\Hashing\NativeHasher);
}

note there are other different Hashers provided by Sentry and you can find them at this directory: vendor/cartalyst/sentry/src/Cartalyst/Sentry/Hashing/

Full Example:

use Cartalyst\Sentry\Users\Eloquent\User;
use Cartalyst\Sentry\Hashing\NativeHasher as SentryNativeHasher;

class Subscriber extends User
{

     public function __construct()
     {
          $this->setHasher(new SentryNativeHasher);
     }

}

If you want to use the setHasher method for the entire model; in order to preserve the Eloquent class constructor function, use the static method 'boot' which will be run from the Eloquent constructor class:

public static function boot()
{
    static::setHasher(new \Cartalyst\Sentry\Hashing\NativeHasher);
}

The best method is to use the Eloquent boot function (as suggested by @browno), but also use the hasher from the parent class, so the Sentry config setting for the hasher will still be used. Also, remember to call the parent boot function, otherwise it will likely break things. Example code:

use Cartalyst\Sentry\Users\Eloquent\User as SentryUser;

class User extends SentryUser {
    protected static function boot() {
        parent::boot();

        static::setHasher(parent::getHasher());
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top