I can not return the authenticated user in Laravel


Good morning In my project, I do the default authentication provided by Laravel 5, hence it directs to my home normally when I log in. I know I logged in normally because when I put wrong data it returns error. At this time I try to get the user logged in $user = Auth::user(); and when I try to see if it returned the value I want it says it did not build the object: "Trying to get property of non-object".

To date, I've never worked with Laravel authentication. Can anyone help me?

Below the codes routes.php:

// Authentication routes...
Route::get('login', 'Auth\[email protected]');
Route::post('login', 'Auth\[email protected]');
Route::get('logout', 'Auth\[email protected]');

// Registration routes...
Route::get('register', 'Auth\[email protected]');
Route::post('register', 'Auth\[email protected]');

Route::get('/', '[email protected]');
Route::get('home', '[email protected]');

I did not switch to AuthController, except in redirectTo , which I put to my home. The login and register did almost exactly as in the documentation, I just changed the style. I did not move in the Model User, I left exactly as the default


use Auth;
use Redirect;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class PrincipalController extends Controller{
    public function home(){
        //Pode fazer um array de atributos/objetos e mandar pelo compact (pode separar os parâmetros no compact por vírgula)
        $user = Auth::user();

            return view('home', compact('user'));
            return view('auth/login');


namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
    use AuthenticatesAndRegistersUsers;
    protected $redirectTo = '/';
    protected function validator(array $data)
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
    protected function create(array $data)
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
asked by anonymous 02.02.2016 / 14:46

2 answers


I found out what's happening!

I'm following the documentation of the site, which I found is in version 5.0 and my Laravel is in version 5.2.12, as it had updated recently.

The default Laravel authentication method is to simply execute the php artisan make:auth , so it creates the login, register, email, reset, password, app and home files, leaving a well-designed page ready to edit, and the best, already uses what I had sought.

Thank you to anyone who came to assist me in the post comments

02.02.2016 / 20:56

I do not know what happened to your Laravel, or if it is anyway, but the postLogin() function that has this AuthenticatesAndRegistersUsers.php file should look like this:

public function postLogin(Request $request)
        $this->validate($request, [
            'email' => 'required|email', 'password' => 'required',

        $credentials = $request->only('email', 'password');

        if ($this->auth->attempt($credentials, $request->has('remember')))
            return redirect()->intended($this->redirectPath());

        return redirect($this->loginPath())
                    ->withInput($request->only('email', 'remember'))
                        'email' => $this->getFailedLoginMessage(),

Try to replace what is there and see what it gives.

The file session.php must be file .

02.02.2016 / 18:40