Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

selim's avatar

Auth::attempt fails

I'm having problems with my project, it won't log any users in with the Auth::attempt() - it just does nothing - no error message or anything is displayed.

I really don't know what the problem is, been trying to figure it out for a while now.

This is my form

{{Form::open(array('action' => 'SessionsController@store', 'class'  => 'login', 'method' => 'POST'))}}
{{Form::text('email', null, ['class' => 'form', 'required' => 'required', 'placeholder' => 'Email'])}}
{{Form::password('password', ['class' => 'form', 'required' => 'required', 'placeholder' => 'Šifra'])}}
{{Form::submit('Prijava', array('class' => 'form', 'style' => 'text-align:center;'))}}
{{Form::close()}}


This is my User model

<?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;



 /**
  * The database table used by the model.
  *
  * @var string
  */
 protected $table = 'users';

    protected $fillable = ['fname', 'lname', 'code', 'dob', 'email', 'gender', 'username', 'password', 'confirmpassword', 'city'];

 /**
  * The attributes excluded from the model's JSON form.
  *
  * @var array
  */
 protected $hidden = array('password', 'remember_token');

    public function getRememberToken()
    {
        return $this->remember_token;
    }

    public function setRememberToken($value)
    {
        $this->remember_token = $value;
    }

    public function getRememberTokenName()
    {
        return 'remember_token';
    }

    public function getAuthIdentifier() {
        return $this->getKey();
    }
    public function getAuthPassword() {
        return $this->password;
    }


}



And this is my SessionsController

class SessionsController extends \BaseController {

 public function store()
 {
    $input = Input::only('email', 'password');

        if (Auth::attempt($input))
        {

           return 'Success';

        }
 }
}

0 likes
18 replies
webtower.org@gmail.com's avatar

Check your "users" table - it should contain "remember_token" (varchar type) column, if it doesn't you have to create it manually.

selim's avatar

It's there and its value for the user I'm trying to log in is NULL. I just added

    public function store()
    {
        $input = Input::only('email', 'password');

        if (Auth::attempt($input))
        {

           return 'Success';

        }

        else {
            return 'No';
        }
    }

this else statement and noticed that it too, doesn't trigger so I think my form is not being submitted at all.

This is my routes file: (the important line is Route::post('/', 'SessionsController@store');)

   <?php

#csrf Protection
Route::when('*', 'csrf', ['post', 'put', 'patch']);

#Home
Route::get('/', ['as' => 'home', 'uses' => 'PagesController@index']);

#MAIN

#Registration
Route::get('/register', 'RegistrationController@create')->before('guest');
Route::post('/register', 'RegistrationController@store');

#Account
Route::get('/account/activate/{code}', array('as' => 'account-activate', 'uses' => 'RegistrationController@getActivate'));


#Login

Route::post('/', 'SessionsController@store');
#END MAIN


#ADMIN

#Banners
Route::get('/admin/clients/banners', 'BannersController@index');
Route::post('/admin/clients/banners/create', 'BannersController@create');
Route::resource('/admin/clients/banners', 'BannersController', ['only' => ['create', 'store', 'destroy']]);
Route::post('/admin/cgalleries/{id}/edit', 'CgalleriesController@addPictures');


#Place Images
Route::post('/admin/places/{place}/edit', 'GalleriesController@store');


#Resources
Route::group(array('prefix' => 'admin'), function()
{
    Route::resource('clients', 'ClientsController');
    Route::resource('streams', 'StreamsController');
    Route::resource('places', 'PlacesController');
    Route::resource('articles', 'ArticlesController');
    Route::resource('cgalleries', 'CgalleriesController');
    Route::resource('announcements', 'AnnouncementsController');
});

#END ADMIN
foxted's avatar

Can you post your users table migration file as well ?

selim's avatar

Here is the migration file

note: before you ask, the passwords are all hashed with Hash:make() e.g the user I'm trying to log in: $2y$10$AT9x0f10omwjpRQoetUfme8H7S0vlkOgWh660l.Rsz8ihHIWAHdo2

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateUsersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('username')->unique();
            $table->string('email')->unique();
            $table->boolean('admin')->default(0);
            $table->string('code');
            $table->boolean('active')->default(0);
            $table->string('fname');
            $table->string('lname');
            $table->string('gender');
            $table->date('dob');
            $table->string('city');
            $table->string('password', 64);
            $table->string('remember_token')->nullable();
            $table->timestamps();
        });
    }


    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }

}

foxted's avatar

I don't think it comes from the database, perhaps your sessions configuration?

selim's avatar

I don't have anything special in the sessions.php file if that's what you're asking, just the default stuff in there. I really don't know what the problem could be :/. Please note that I also am still a beginner, just trying to pick up on laravel, with this being my first bigger project with laravel.

All I have is the filter to stop unathorized users to access the admin panel

filters.php

Route::filter('admin', function()
{
    if (!Auth::user() || Auth::user()->admin != 1) return Redirect::to('/');
});

alenabdula's avatar

My routes file.

Route::get('/login',[ 'as' => 'login', 'uses' => 'SessionsController@create']);
Route::get('/logout',[ 'as' => 'logout', 'uses' => 'SessionsController@destroy']);
Route::resource('sessions', 'SessionsController', ['only' => ['create', 'store', 'destroy'] ]);

My SessionsController.

public function create()
{
    if ( Auth::check() ) return Redirect::to('/admin');
    return View::make('sessions.create');
}

public function store()
{
    if ( Auth::attempt(Input::only('email', 'password') ) ) {
        return Redirect::to('/admin');
    }
    else {
        return Redirect::to('/login')->withInput();
    }
}

public function destroy()
{
    Auth::logout();
    return Redirect::to('/');
}

My sessions / create view.

{{ Form::open([ 'route' => 'sessions.store' ]) }}
        {{ ... }}
{{ Form::close() }}

Hope that helps. -Alen

selim's avatar

I just noticed that it doesn't reach the controller method @store at all. Added return 'no'; at the first line of the store method but nothing was returned..

foxted's avatar

Must be related to your routes then, try to change the action url or place it at the top of your routes file and check if you see any difference.

selim's avatar

Tried that, no difference :/. Could it be that my header.blade.php file contains that form, and the header file is included into the home.blade.php file with @include ?

The home.blade.php file is called by the PagesController@index while the login is processed by SessionsController@store. I don't think that could be an issue but I honestly don't know right now.

foxted's avatar

Do you hash your password before storing it? Could you post your RegistrationController code?

The idea that comes to my mind is that something goes wrong when you create the user in database, like this guy's problem on StackOverflow

selim's avatar

When the form is submitted it doesn't even go to the SessionsController@store, it just refreshes the website instead of redirecting to SessionsController@store.

I tried to return a random string at the beginning of SessionsController@store and it never gets output.

Could it be the jquery behind my form? The fields for the login (email and pw) are shown once the user clicks one button in the header.

And once again - the password is hashed.

UPDATE: So i tried extracting it to a totally separate view and route, and when the form is submitted it always brings me back to the homepage, that is '/'.

This is my RegistrationController:

<?php

use Acme\Forms\CreateUser;

class RegistrationController extends \BaseController {

    public function __construct(CreateUser $createUser)
    {
        $this->createUser =$createUser;
    }

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

    public function store()
    {
        $input = Input::only('fname', 'lname', 'dob', 'email', 'gender', 'username', 'password', 'confirmpassword', 'city');
        $fname = Input::get('fname');
        $lname = Input::get('lname');
        $dob = Input::get('dob');
        $email = Input::get('email');
        $gender = Input::get('gender');



        $username = Input::get('username');
        $password = Input::get('password');
        $city = Input::get('city');
        $code = str_random(16);

        $this->createUser->validate($input);

        $user = User::create(array(

                'fname' => $fname,
                'lname' => $lname,
                'dob' => $dob,
                'email' => $email,
                'gender' => $gender,
                'username' => $username,
                'password' => Hash::make($password),
                'city' => $city,
                'code' => $code
            ));

       if ($user)
       {
           Mail::send('emails.auth.activate', array('link' => URL::route('account-activate', $code),
               'username' => $username, 'fname' => $fname, 'lname' => $lname, 'dob' => $dob, 'gender' => $gender, 'city' => $city, 'password' => $password),function($message) use ($user) {

               $message->to($user->email, $user->username)->subject('Aktivirajte svoj profil');
           });
       }

    }

    public function getActivate($code)
    {
        $user = User::whereCode($code)->whereActive(0);

        if($user->count()){

            $user = $user->first();

        $user->active = 1;
        $user->code = '';

            if($user->save())
            {
                return Redirect::route('home')->with('global', 'Account activated');
            }
        }

            return Redirect::route('home')->with('global', 'We could not activate your account. Try again later');
    }





}
bashy's avatar

Make sure you test your form with Postman or Development tools on your browser.

You can easily check if it's posting anything and what's sent/received.

alenabdula's avatar

Is debug turned on?

I have a feeling it's a routing issue.

Route::post('/', 'SessionsController@store');

I would use named route here and reference that in the form.

So i tried extracting it to a totally separate view and route, and when the form is submitted it always brings me back to the homepage, that is '/'.

Well you're posting to the '/' route, so that's expected.

Can you die-and-dump in the SessionsController@store method.

public function store()
{
    dd(Input::all());
}
1 like
hafizabi's avatar

I had this issue in https://laracasts.com/series/laravel-from-scratch-2017 course. And fixed it by adding

public function setPasswordAttribute($value)
    {
        if( \Hash::needsRehash($value) ) {
            $value = \Hash::make($value);
        }
        $this->attributes['password'] = $value;
    }

in User.php (User Model)

The issue is password was not begin hashed. May be there is a nice way.

hafizabi's avatar

I had this issue in https://laracasts.com/series/laravel-from-scratch-2017 course. And fixed it by adding

public function setPasswordAttribute($value)
    {
        if( \Hash::needsRehash($value) ) {
            $value = \Hash::make($value);
        }
        $this->attributes['password'] = $value;
    }

in User.php (User Model)

The issue is password was not begin hashed so it should be hashed before saving it DB. May be there is a nice way.

Please or to participate in this conversation.