I'm creating a messaging application. User logs in and then his conversations are displayed to him.
i used Auth::attempt to check the user(after he submits the login form) . After successful login, he is redirected to a route(conversations) which uses respective controller method(HomeController@conversations).
In the method, i need to know the user's id so that i could fetch his conversation list. SO i need to use Auth::user()->id, wherever needed.
Now when i'm using Auth::user()->id, it is showing an error(trying to get property from non-object).
i tried print_r(Auth::user) and the result was blank. But when tried it before redirection, it's showing the details
On localhost, everthing is working fine, but i'm facing this issue on the live server. Auth::attempt is working fine, but Auth::check() and Auth::user() are not. Session directory is writable. Session files are creating there.
Here is the route file:
//Route for form display and processing
Route::get('login', array('uses' => 'HomeController@showLogin'));
Route::post('login', array('uses' => 'HomeController@doLogin'));
Route::get('conversations', array('as'=>'conversations', 'uses'=>'HomeController@conversations'));
Here is the doLogin method that processes the form and redirect user to the route conversations
public function doLogin()
{
$rules = array(
'email' => 'required|email', // make sure the email is an actual email
'password' => 'required|alphaNum|min:3'
);
$validator = Validator::make(Input::all(), $rules);
// if the validator fails, redirect back to the form
if ($validator->fails()) {
return Redirect::to('login')
->withErrors($validator) // send back all errors to the login form
->withInput(Input::except('password'));
} else {
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
// attempt to do the login
if (Auth::attempt($userdata)) {
// validation successful!
return Redirect::to('conversations')
}else {
// validation not successful, send back to form
return Redirect::to('login');
}
}
}
here is the conversation method
public function conversations(){
if (Auth::check()){
$self_id = Auth::user()->id
//use $self_id to show conversations
}
}
I think your problem is that your session is not persisting. What is your session configuration? Are you using memcached or database ?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community