user2087032
user2087032

Reputation: 98

Using Laravel is it possible to bypass the need for remember_token using an external user database

With a recent update of Laravel they require a remember_token, and also updated_at.

When logging in and out I get:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'remember_token' in 'field list' (SQL: update `account` set `remember_token` = s8XPDCdNdJnjbLj7MlDRO1Cy5owwjxdfW1rYVhd6QpePIltqPkavr9l3KUbd, `updated_at` = 2014-07-30 14:09:37 where `id` = 5)

I am using an external user database which I don't want to create these extra fields on, as it is used also on a game server which I don't want to mess with..

Is there a way to bypass the need for both of these columns?

Upvotes: 2

Views: 2721

Answers (3)

Adam Rodriguez
Adam Rodriguez

Reputation: 1856

Yes it is possible. This code should allow you to avoid having to use the 'remember_token' in your users table

public function getRememberToken()
{
    return null; // will not support
}

/**
 * Set the token value for the "remember me" session.
 *
 * @param  string  $value
 * @return void
 */
public function setRememberToken($value)
{
    // will not support
}

/**
 * Get the column name for the "remember me" token.
 *
 * @return string
 */
public function getRememberTokenName()
{
    return null; // will not support
}

public function setAttribute($key, $value)
{
    $isRememberTokenAttribute = $key == $this->getRememberTokenName();

    if( ! $isRememberTokenAttribute )
    {
        parent::setAttribute($key, $value);
    }
}

Upvotes: 2

alexrussell
alexrussell

Reputation: 14202

I'm pretty sure using Laravel's built in Auth system (Guard) makes you use UserInterface which, in turn, makes you define these. However, you could just fake it by having these methods but making them return useless information.

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface
{
    use UserTrait, RemindableTrait;

    protected $hidden = array('password', 'remember_token');
    public $timestamps = false;

    public function getRememberToken()
    {
        return '';
    }

    public function setRememberToken($value)
    {
    }

    public function getRememberTokenName()
    {
        // just anything that's not actually on the model
        return 'trash_attribute';
    }

    /**
     * Fake attribute setter so that Guard doesn't complain about
     * a property not existing that it tries to set.
     *
     * Does nothing, obviously.
     */
    public function setTrashAttributeAttribute($value)
    {
    }
}

That might just do what you want - basically override these functions to not work correctly, which means that 1) you don't need to mess with your DB and 2) when the functionality doesn't work it's okay because you're not using it anyway.


And as @neeraj says, for the created_at thing, just do $timestamps = false and hope for the best. Updated model above to reflect this.

Upvotes: 2

Neeraj
Neeraj

Reputation: 9165

Try removing below interface from your modal

RemindableInterface

For created_at and updated_at, you can write the below line in modal:

public $timestamps = false;

For example, if your modal name is Admin then code should look like

class Admin extends Eloquent implements UserInterface
{

    public $timestamps = false;
}

Upvotes: 1

Related Questions