英文:
How to retrieve limited records if the user is not logged in and all records if the user is logged in in Laravel?
问题
这是您要求的代码部分的中文翻译:
我有一个 Laravel 路由,我想根据用户是否已登录来进行不同处理。如果用户未登录,我想在控制器中仅检索 5 条记录(限制为 5)。如果用户已登录,我想检索所有记录。
这是我的当前路由:
Route::get('/test', 'TestApiController@index');
当我为我的路由设置 middleware('auth:sanctum') 时,响应将是 401 未经授权,但我希望在这种情况下获取有限的记录。我应该如何修改路由和控制器以实现这一点?另外,如何检查用户是否已登录?
这是我的当前控制器代码:
class TestApiController extends Controller
{
public function index()
{
if (auth()->check()) {
$data = Test::all();
} else {
$data = Test::limit(5)->get();
}
return response()->json($data);
}
}
我认为我需要从请求标头中获取认证参数以检查用户是否已登录:
$request->header('Authorization')
我该如何修改我的代码以实现所需的功能?谢谢。
请注意,上述翻译只包含您的代码和请求,没有额外的内容。
英文:
I have a Laravel route that I want to handle differently depending on whether the user is logged in or not. If the user is not logged in, I want to retrieve only 5 records (limit 5) in the controller. If the user is logged in, I want to retrieve all the records.
Here is my current route:
Route::get('/test', 'TestApiController@index');
When I set middleware('auth:sanctum') for my Route, the response will be 401 Unauthorized, but I want to get limited records for this situation instead. How can I modify the route and the controller to achieve this? Also, how do I check if the user is logged in?
Here is my current controller code:
class TestApiController extends Controller
{
public function index()
{
if (auth()->check()) {
$data = Test::all();
} else {
$data = Test::limit(5)->get();
}
return response()->json($data);
}
}
I think I need to get the authentication parameter from the request header to check if the user is logged in:
$request->header('Authorization')
How can I modify my code to achieve the desired functionality? Thank you.
I try when or middlewareIf and middleware(function ($request, $next),
but they are not working..
答案1
得分: 0
你的路由看起来像是一个API路由,使用的是'api'守卫而不是'web'。当你调用auth()->check()
时,它正在使用'web'守卫。
基于这一点,你的控制器应该像这样:
class TestApiController extends Controller
{
public function index()
{
if (auth('api')->check()) {
$data = Test::all();
} else {
$data = Test::limit(5)->get();
}
return response()->json($data);
}
}
你可以在Laravel认证文档中了解更多信息。
英文:
Looks like you are mixing some concepts there.
Your route looks like an API route, which uses 'api' guard instead of web. When you call for auth()->check()
its getting the 'web' guard.
Knowing this, your controller should look like this:
class TestApiController extends Controller
{
public function index()
{
if (auth('api')->check()) {
$data = Test::all();
} else {
$data = Test::limit(5)->get();
}
return response()->json($data);
}
}
You can read more in the Laravel authentication documentation.
答案2
得分: 0
我注意到你的代码是由Sanctum授权的。
路由:
//注意:没有使用中间件('auth:sanctum')
Route::get('/test', 'TestApiController@index');
控制器:
解决方案 #1:
class TestApiController extends Controller
{
public function index()
{
$token = $request->bearerToken();
$personalAccessToken = \Laravel\Sanctum\PersonalAccessToken::findToken($token);
if ($personalAccessToken) {
// '已登录'
// 访问用户信息
// $user = $personalAccessToken->tokenable;
$data = Test::all();
} else {
// '未登录'
$data = Test::limit(5)->get();
}
return response()->json($data);
}
}
解决方案 #2:
class TestApiController extends Controller
{
public function index()
{
if (\Auth::guard('sanctum')->check()) {
// '已登录'
$data = Test::all();
} else {
// '未登录'
$data = Test::limit(5)->get();
}
return response()->json($data);
}
}
英文:
I noticed that your code is authorized by Sanctum
Route:
//Note: Without middleware('auth:sanctum')
Route::get('/test', 'TestApiController@index');
Controller:
Solution #1:
class TestApiController extends Controller
{
public function index()
{
$token = $request->bearerToken();
$personalAccessToken = \Laravel\Sanctum\PersonalAccessToken::findToken($token);
if( $personalAccessToken ) {
// 'logged in'
// Access to user information
// $user = $personalAccessToken->tokenable;
$data = Test::all();
} else {
// 'Not logged in'
$data = Test::limit(5)->get();
}
return response()->json($data);
}
}
Solution #2:
class TestApiController extends Controller
{
public function index()
{
if (\Auth::guard('sanctum')->check()) {
// 'logged in'
$data = Test::all();
} else {
// 'Not logged in'
$data = Test::limit(5)->get();
}
return response()->json($data);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论