User is not authenticated Auth

0

I can not get the user to be authenticated, so he enters my route with the auth middleware ... I can log in but I can not get the user logged in, for example I can not enter any route with middleware auth and nen to use no helper method Auth :: user () ...

Route

Route::group(['middleware' => 'auth'], function () {

Route::get('/usuario',['uses' => 'UsuarioController@index','as' => 'index']);

});

Route::get('/login',['uses' => 'UsuarioController@login','as' => 'login']);
Route::post('/login',['uses'=>'UsuarioController@checkLogin','as' => 'VerificarLogin']);

Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request; use Auth;

class UsuarioController extends Controller{

   public function login () {

       return view('Inicial.login');
   }
   public function index () {

       return view('usuario.index_Usuario');
   }

   public function checkLogin (Request $request){

        //$credentials = ['email'=>$request->get('email'),'password'=>$request->get('senha')];

        $email = $request->get('email');
        $password = $request->get('senha');

        if (Auth::attempt(['email' => $email, 'password' => $password])) {

            return redirect()->route('index');
        }else {
            return 0;
        }

    }
}

Auth.php

    <?php

return [



    'defaults' => [
        'guard' => 'usuario',
        'passwords' => 'users',
    ],


    'guards' => [
        'usuario' => [
            'driver' => 'session',
            'provider' => 'usuario',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],

    'providers' => [
        'usuario' => [
            'driver' => 'eloquent',
            'model' => App\Usuario::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],



    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

];

App \ User

    <?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class Usuario extends Authenticatable
{
    protected $fillable = ['nome','email','password'];

  //  protected $fillable = ['Usuario_Nome','password','email','Usuario_DtNascimento','Usuario_Role']; //fillable para inserção em massa

    protected $table = "usuarios";
}
    
asked by anonymous 27.02.2018 / 16:39

1 answer

1

I went through a similar problem a few times and it took me a long time to understand why.

Laravel creates a variable in the browser that defines the session token to be transacted during browsing.

This data is like a cookie, which is domain-specific and needs to hit the session on the server.

The problem is that if you do not specify the correct URL in .env the application may not work.

In some cases I have never faced a problem, even if I have localhost in APP_URL, but in other cases the Login page only keeps updating without displaying anything, not even Flash messages, because they also use the session to work. p>

So to understand if this is the problem:

  • Verify that your application is creating a cookie / session storage or local storage with a token in the browser

  • Test Flash messages using session;

  • Realizing the problem is this:

  • Specify correctly the .env in the domain that you use to work;

  • Check for write permissions in the storage / sessions directory;

  • Clean Laravel caches: php artisan cache: clear

  • Clear caches in the browser.

  • I hope I have helped!

        
    01.03.2018 / 15:19