1

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

DasBoot
  • 469
  • 6
  • 17
  • Looks more like a session problem. To answer your second question - use, authentication uses Laravel Sessions. – Laurence Sep 25 '14 at 03:35
  • is your id column `id` or `user_id` - from looking at your code I have a guess you've called it `user_id`? it should be `id` – Laurence Sep 25 '14 at 04:27
  • I am starting to think there might be a problem with session configuration. Are there any more settings than sessions lifetime that I could test? I tried changing "config/session.php" lifetime, no impact. Also tried changing user_id to id. Nothing either. – DasBoot Sep 25 '14 at 15:12
  • What session driver do you use? – Jarek Tkaczyk Sep 26 '14 at 10:21
  • Not sure what exactly did I do but it works now. – DasBoot Sep 26 '14 at 20:33
  • Have you found what you changed? I am getting the same problems. – JorenV Jan 06 '15 at 15:53
  • I cant remember but I recall that I was remaking everything as it was in laracast tutorial and I found out that I had tested it the wrong way or something like that. I think this is the tutorial https://laracasts.com/series/laravel-from-scratch/episodes/15 – DasBoot Jan 06 '15 at 17:53

3 Answers3

1

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';
}
Gowtham Selvaraj
  • 478
  • 1
  • 5
  • 12
  • No, that's not the case, and your example is wrong. Read this: http://stackoverflow.com/questions/26002552/laravel-4-custom-named-password-column#26002831 – Jarek Tkaczyk Sep 26 '14 at 10:20
1

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.

Mohammed Saqib Rajput
  • 1,331
  • 1
  • 14
  • 22
0

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.