I've scoured the documentation and I've spent hours trying to figure this out, and this is really my last resort. If this doesn't work I may have to get a subscription to Laracasts. I'm following this guide and it doesn't seem to be working for me. https://laravel.com/docs/5.4/session
I'm hoping that someone can help me and tell me what I'm doing wrong. When I do my API request I get a success response and it returns the user from the database, but it doesn't create the session in the session table. Also, I don't get any errors in laravel.log.
I can create a user with no problems. I can match hashed passwords when retrieving the user.
api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('/users', 'UserApiController@login');
UserApiController.php
public function login()
{
$input = Request::all();
$user = new User();
$response = ['success' => false];
if (!$user->validate($input))
{
return response()->json($response, 412);
}
$user = User::where('Email', $input['Email'])->where('Password', User::makePassword($input['Password']))->first();
Log::info($user); //returns the user object successfully
if (!isset($user))
{
return response()->json($response, 401);
}
session('user', $input]); //<--this is not working
$response['success'] = true;
return response()->json($response, 200);
}
I'm not sure if you wrote what you really want to achieve, but this is what I think - you are using api middleware group and you want to use sessions. But if you look at app/Http/Kernel.php you will see sessions are used by default only by web middleware group:
\Illuminate\Session\Middleware\StartSession::class,
so you can either add this middleware to api group or rethink what you really want to achieve because APIs are usually stateless so you normally don't use sessions for them because in each request you send authorization data (api_token in this case by default) so each time you can find out which user is making the request