User does not authenticate Auth

Asked

Viewed 718 times

0

I can’t get the user authenticated, so he enters my route with middleware auth... I can log in but I can’t get the user logged in, for example I can’t enter any route with middleware auth and nen 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 Usuario

    <?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";
}
  • post the class App\Usuario::class please

  • @Virgilionovic I’m in the chat stack burst man, anyone appears there ^^

  • I think there’s something wrong with the setup part... but, I can’t say, because it changed the authentication class?

  • what configuration ? type I created this project from scratch just to test and yet still not catching

  • if you want more information just say that I post, I spent the day hj trying to solve this ...

  • briefcase config\Auth.php but, I don’t know why it doesn’t have to test, but, don’t need to change the authentication class that already comes ready ...!!! I see the staff changing only to add fields, because, does not add in the same understood.

Show 2 more comments

2 answers

1


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

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

This data is like a cookie, which is domain specific and needs to match 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 never faced problem, even keeping localhost in APP_URL, however, in other cases the Login page is only updating without displaying anything, not even Flash messages, because they also use the session to work.

So to understand if this is the problem:

  1. Check that your application is creating in the browser the cookie / Session Storage or Storage location with a token;

  2. Check that with each new access, there is a new session file in the Storage/Sessions or table Sessions (if you have specified database for sessions).

  3. Test Flash messages that use Session;

Realizing that the problem is this:

  1. Correctly specify on . env the domain you use to work with;

  2. Check for write permissions in the Storage/Sessions directory;

  3. Clear Laravel caches: php Artisan cache:clear

  4. Clear the caches in the browser.

I hope I’ve helped!

  • Man the problem is in the database... for some reason when I change database he can authenticate the user, has some notion about it ?

  • When you say exchange database, would it just be the same database or switch to another type of database? Type of Mysql for Mongo? If you are using sessions in the database, confirm that the new database has the Sessions table (if you have not run Migrations).

  • Not bank itself but mysql banks, which I switched banks picked up normal and when I go back to the other bank no funfa, the difference between the two banks is that one has no relationship but the other has some relationships. What I can relationship doesn’t work already what doesn’t work normally

  • I’m not sure, but I think the relationship issue is unrelated to auth. Confirm this set as file the Session in your .env. If it is database, check if there is a Sessions table in the database without relationships.

  • You have file o Session in .env. When I changed the name of my id field, Tava so "Id_usuario" changed to "id" works perfectly, now because I have no idea, know something about it ?

  • If you are using Laravel’s default auth template, it uses what is defined in the users' Migration, where the column is called id. I think to work the other way, you need to specify in the User Model that the data id is Id_user.

  • yes, can fix, default is id, has a command you can change the field $primaryKey = "New id name". Vlw man helped tmj. Vota ae ^^

  • 1

    I’m glad it worked out! I’m new here and I don’t know if this is good practice, but I believe it would be useful for the next ones if you edit your question because it didn’t really explain what problem you were facing, so it helps the next ones who have the same problem. Alias, rebound that I don’t know if this is a good practice here in the community, I’m just thinking about the next.

Show 3 more comments

0

I went through a problem like this using Laravel 8, I decided to put in the model User a protected variable of name $primaryKey with the name value equal to the table’s primary key User.

Ex:

protected $primaryKey = 'id_user';

I hope you help someone!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.