DasBoot
DasBoot

Reputation: 479

laravel Auth::check() fails

Auth::check() fails after successful Auth:attempt(). I am just following laracast.com tutorials to make a simple authentication. This specific tutorial https://laracasts.com/series/laravel-from-scratch/episodes/15 . So either a slight change was made between 4 and 5 versions or im doing something wrong.

This is a function that does auth and the second one does the checking. Both of them are in the same class.

public function store() 
{
    $credentials = Input::only('user_displayname');
    $credentials['password'] = Input::get('user_password');

    if (Auth::attempt($credentials))
    {
        return Auth::user();
    }

    return 'not logged';
}

public function status()
{
    return dd(Auth::check());
}

This is User model:

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    protected $table = 'user';

    protected $hidden = array('user_password', 'remember_token');

    protected $fillable = ['user_displayname', 'user_fname', 'user_lname', 'user_email',     'user_password'];

    public $errors;

    public static $rules = array(
        'user_displayname' => 'required|unique:user,user_displayName',
        'user_fname' => 'required',
        'user_lname' => 'required',
        'user_email' => 'required|unique:user,user_email',
        'user_password' => 'required'
    );


    public function isValid($data)
    {
        $validation = Validator::make($data, static::$rules);

        if ($validation->passes())  return true;

        $this->errors = $validation->messages();
    }

    public function getAuthPassword()
    {
        return $this->user_password;
    }

}

Second question. Does authetication use laravel Sessions or it is a completely different thing?

EDIT:

Does Auth have lease times or anything similar that just deletes session after time expires? Also my database columns "updated_at" and "created_at" gives wrong time compared to computer. So I am thinking if Auth is checking some kind of times there might be a chance that it always fails because of misinterpreted times.

P.S already looked over other solutions in stackoverflow.

Thank you

Upvotes: 1

Views: 4282

Answers (3)

Anthony Farr
Anthony Farr

Reputation: 7

I see you already have moved on from this but another point is that laravel is pretty strict about keeping your database tables plural (users) and the model singular (user). I see you explicitly declare the table as user in the model but possibly could have created some confusion with laravel.

Upvotes: 0

Mohammed Saqib Rajput
Mohammed Saqib Rajput

Reputation: 1370

I think Laravel has a bug. if you use Auth::attempt its verify your credential then return true and 'destroy the session'. So we redirect our url and use Auth::check() so its return false. because session is destroy and you lost you data to check.

Upvotes: 1

Gowtham Selvaraj
Gowtham Selvaraj

Reputation: 478

looks like the parameters to Auth::attemp(); is in valid try using this.

public function store() 
{
    $credentials = array('user_displayname'=>Input::get('user_displayname'),
                         'user_password'=> Input::get('user_password'));
    if (Auth::attempt($credentials))
    {
        return Auth::user();
    }

    return 'not logged';
}

Upvotes: 1

Related Questions