I want to build an API route which can be called by an authenticated user as well as a non-authenticated user and returns the data based on whether or not the user is authenticated.
I am stuck at using Auth::check().
The controller function is something like this:
class SomeController extends Controller
{
public function check(Request $request) {
if(Auth::check()) {
return response()->json([
'message' => 'Logged in'
], 200);
}
else {
return response()->json([
'message' => 'Not logged in'
], 200);
}
}
}
I end up having two scenarios:
Scenario 1
Route::get('scenario1', 'SomeController@check');
In this scenario, in the controller when I check Auth::check() inside an if condition I am always getting the false condition, no matter the user is authenticated or not.
Scenario 2
Route::get('scenario2', 'SomeController@check')->middleware('auth:api');
In this scenario, in controller when I check Auth::check() I get true when user is logged in and error(Route[login] not definded) when user is not logged in.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire