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?
– Wallace Maxters
I didn’t change the class, I’m using everything of the standard of Laravel, IE, the same User
– Isaias Lima
Check it out then:
var_dump(Auth::user() instanceof User);. Tell me what comes back.– Wallace Maxters
Returns 'Boolean false'
– Isaias Lima
Come on, second test :
var_dump(Auth::check()). This has to come backtrueif authenticated.– Wallace Maxters
Returned false, but it’s strange because it only redirects to the home if I put the correct login data
– Isaias Lima
Um... I already know. Check the file
session.phpwhich is inside config. Check that the settings are "array". If so, switch to "file".– Wallace Maxters
Nothing, it was already as 'file', and look I tested all possible settings ('file', 'cookie', 'database', 'apc', 'memcached', 'redis', 'array')
– Isaias Lima