I use laravel 5.4 as the backend for my application, and for the front-end I use angular. I use laravel authfor authentication.
The problem is Auth::attempt()working fine, and if I print it immediately Auth::user(), then it prints the data, but returns falseif I try to extract it in the next method. But this functionality works fine on a hosted server.
Tested
- Change session from file to database.
- Changes to kernel.php (middleware content).
- Did php artisan make: auth again.
- Changes to the user table column.
- Adding private $ primarykey = 'id' to the model.
- Adding web middleware to all routes.
This is my controller
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\User;
public function login()
{
if (Auth::attempt(['email' => $email, 'password' => $password ]))
{
$response = array('response' =>'Succssfully Login!' , 'success' => true);
return $response;
}
}
Here I use Auth::check()in the same controller
public function check()
{
if(Auth::check())
$response = array('response' =>'Authenticated' , 'success'=>true);
else
$response = array('response' =>'UnAuthenticated' , 'success'=>false);
return $response;
}
I am confused because the same code works fine on the server but doesn't work on localhost. Do I need to make any http related changes to laravel for this?
source
share