Search code examples
phplaravelauthenticationlaravel-4illuminate-container

Laravel Login / User Issue with Remember Token


I just started working with a year old Laravel project for my fellowship and a major bug inhibiting progress is in the way. Any help would be greatly appreciated. Story below.

Once Laravel is up and running and I try to log in, I get this error:

Class User contains 3 abstract methods and must therefore be declared abstract or
implement the remaining methods 
(Illuminate\Auth\UserInterface::getRememberToken, 
Illuminate\Auth\UserInterface::setRememberToken,
Illuminate\Auth\UserInterface::getRememberTokenName)

I believed this could be fixed via http://laravel.com/docs/upgrade#upgrade-4.1.26 and so I added the following lines to my User as suggested:

public function getRememberToken(){ return $this->remember_token; }
public function setRememberToken($value){ $this->remember_token = $value; }
public function getRememberTokenName(){ return 'remember_token'; }

This got rid of the error but I still cannot log in. I'll use the correct information but it seems to redirect me back to the login page. Upon further research, I discovered that I may need to have a [b]remember_token[/b] field in my database. I attempted to add this and remigrate by adding:

$table->rememberToken(); 
[edit 2014-09-16, 07:05: added parens. they were in my code but forgotten in the post]

But that produced this stacktrace:

PHP Fatal error:  Call to undefined method Illuminate\Database\Schema\Blueprint::rememberToken() in /vagrant/www/vfamatching.dev/app/database/migrations/2013_11_01_033240_create_users_table.php on line 24
PHP Stack trace:
PHP   1. {main}() /vagrant/www/vfamatching.dev/artisan:0
PHP   2. Symfony\Component\Console\Application->run() /vagrant/www/vfamatching.dev/artisan:59
PHP   3. Symfony\Component\Console\Application->doRun() /vagrant/www/vfamatching.dev/vendor/symfony/console/Symfony/Component/Console/Application.php:121
PHP   4. Symfony\Component\Console\Application->doRunCommand() /vagrant/www/vfamatching.dev/vendor/symfony/console/Symfony/Component/Console/Application.php:191
PHP   5. Illuminate\Console\Command->run() /vagrant/www/vfamatching.dev/vendor/symfony/console/Symfony/Component/Console/Application.php:893
PHP   6. Symfony\Component\Console\Command\Command->run() /vagrant/www/vfamatching.dev/vendor/laravel/framework/src/Illuminate/Console/Command.php:96
PHP   7. Illuminate\Console\Command->execute() /vagrant/www/vfamatching.dev/vendor/symfony/console/Symfony/Component/Console/Command/Command.php:244
PHP   8. Illuminate\Database\Console\Migrations\MigrateCommand->fire() /vagrant/www/vfamatching.dev/vendor/laravel/framework/src/Illuminate/Console/Command.php:108
PHP   9. Illuminate\Database\Migrations\Migrator->run() /vagrant/www/vfamatching.dev/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:67
PHP  10. Illuminate\Database\Migrations\Migrator->runMigrationList() /vagrant/www/vfamatching.dev/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:84
PHP  11. Illuminate\Database\Migrations\Migrator->runUp() /vagrant/www/vfamatching.dev/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:113
PHP  12. CreateUsersTable->up() /vagrant/www/vfamatching.dev/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:137
PHP  13. Illuminate\Support\Facades\Schema::create() /vagrant/www/vfamatching.dev/app/database/migrations/2013_11_01_033240_create_users_table.php:25
PHP  14. Illuminate\Support\Facades\Facade::__callStatic() /vagrant/www/vfamatching.dev/app/database/migrations/2013_11_01_033240_create_users_table.php:25
PHP  15. Illuminate\Database\Schema\Builder->create() /vagrant/www/vfamatching.dev/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:209
PHP  16. CreateUsersTable->{closure:/vagrant/www/vfamatching.dev/app/database/migrations/2013_11_01_033240_create_users_table.php:15-25}() /vagrant/www/vfamatching.dev/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:91
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Call to undefined method Illuminate\\Database\\Schema\\Blueprint::rememberToken()","file":"\/vagrant\/www\/vfamatching.dev\/app\/database\/migrations\/2013_11_01_033240_create_users_table.php","line":24}}vagrant@devbox:/vagrant/www/

[edit 2014-09-16, 07:05: tried watcher's answer] After watcher's answer last night, I have tried using their solution by replacing

$table->rememberToken(); with
$table->string('remember_token', 100);

The migration no longer throws an error but the login still hangs. Also, I should point out that Laravel recommends using the rememberToken syntax

My login route

Route::get('login', array('as' => 'login', function () { return View::make('login'); }))->before('guest');
Route::post('login', 'UsersController@login');

and controller method

public function login() 
{ 
    $user = array(
        'email' => Input::get('email'),
        'password' => Input::get('password')
    );   
    if (Auth::attempt($user)) {
        Auth::user()->login();
        Auth::user()->lastLogin = Carbon::now();
        Auth::user()->save();
        if (Session::has('returnUrl'))
        {
            $intendedDestination = Session::get('returnUrl');
            Session::forget('returnUrl');
            return Redirect::to($intendedDestination)
            ->with('flash_success', 'You are successfully logged in.');
        }
        return Redirect::to('/')
            ->with('flash_success', 'You are successfully logged in.');
    }
    // authentication failure! lets go back to the login page
    return Redirect::route('login')
        ->with('flash_error', 'Your username/password combination was incorrect.')
        ->withInput();
}

Solution

  • 'remember_token' is just a varchar field and should be created like any other in a migration:

    $table->string('remember_token', 100)->nullable();
    

    Update

    In response to your comment, you are correct, the schema builder documentation does allow the rememberToken method, it's interesting that the upgrade guide makes no mention of it. It is only available in Laravel v4.2 and up and, as of this writing, is nothing more than an alias for the code above:

    // File: /vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php
    /**
     * Adds the `remember_token` column to the table.
     *
     * @return \Illuminate\Support\Fluent
     */
    public function rememberToken()
    {
        return $this->string('remember_token', 100)->nullable();
    }
    

    If you are on 4.2 or up, I would use the rememberToken function rather than the string function to define the column, just in case the function definition above changes in any future versions.

    As for your login attempt 'hanging', please update your question with the related controller method and I can maybe help you further.