I can not return the authenticated user in Laravel

3

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\AuthController@getLogin');
Route::post('login', 'Auth\AuthController@postLogin');
Route::get('logout', 'Auth\AuthController@getLogout');

// Registration routes...
Route::get('register', 'Auth\AuthController@getRegister');
Route::post('register', 'Auth\AuthController@postRegister');

Route::get('/', 'PrincipalController@home');
Route::get('home', 'PrincipalController@home');

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

MainController.php:

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();

        if($user){
            return view('home', compact('user'));
        }else{
            return view('auth/login');
        }
    }
}

AuthController.php

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

3

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
0

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'))
                    ->withErrors([
                        '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