I cannot return the authenticated user in Laravel

Asked

Viewed 1,059 times

3

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

I’ve never worked authentication at Laravel before. Can someone 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 didn’t touch the Authcontroller, except the redirectTo, I put it to my home. The login and Register did almost exactly as in the documentation, I just changed the style. I didn’t touch the Model User, I left it exactly as standard

Principalcontroller.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']),
        ]);
    }
}
  • What class (model) you set to be the authentication entity?

  • I didn’t change the class, I’m using everything of the standard of Laravel, IE, the same User

  • Check it out then: var_dump(Auth::user() instanceof User);. Tell me what comes back.

  • Returns 'Boolean false'

  • Come on, second test : var_dump(Auth::check()). This has to come back true if authenticated.

  • Returned false, but it’s strange because it only redirects to the home if I put the correct login data

  • Um... I already know. Check the file session.php which is inside config. Check that the settings are "array". If so, switch to "file".

  • Nothing, it was already as 'file', and look I tested all possible settings ('file', 'cookie', 'database', 'apc', 'memcached', 'redis', 'array')

Show 3 more comments

2 answers

3

I found out what’s going on!

I am following the documentation of the site, which I found is in version 5.0 and my Laravel is in version 5.2.12, because I had recently updated.

The method to do Laravel’s default authentication is to simply execute the command php artisan make:auth, this way it creates the login files, Register, email, reset, password, app and home, already leaves a well drawn page ready to edit, and the best, already uses what I had searched.

Thanks to those who came to help me in the post comments

  • 1

    Jeez... back in 5.0 ? See the page AuthenticatesAndRegistersUsers.php and look at the function I just told you about...

  • So, from what I saw the function only has the call to the login function, which there is the code that you showed, with some small differences

  • 1

    That’s the one I was talking about postLogin and not getLogin().

  • 1

    https://laravel.com/docs/5.2/

0

I don’t know what happened to your Laravel, or if it’s like that, but the function postLogin() that’s in that file AuthenticatesAndRegistersUsers.php should be 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 it with the one there and see what happens.

The archive session.php has to be file.

Browser other questions tagged

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