auth立面无法在没有圣所API的情况下用作Laravel 8的中间件

发布于 2025-02-13 23:25:39 字数 773 浏览 0 评论 0原文

我正在创建一个API,任何人都可以查看页面,但是只有管理员才能看到所有帖子,而用户仅限于批准。这是通过iS_verified布尔值变量实现的,在其中给admin的值为1,用户用户值为0。我想创建一个这样的函数,


public function show(){
    if(Auth::check()){
        $showAllDetails = Events::all();
        echo $showAllDetails;   
    }else {
        $showUserDetails = Events:all()->where('is_verified',1);
        echo $showUserDetails;
    }
}

但是auth:check仅在 如果我在HoppsCotch上

Route::middleware('auth:sanctum')->group(function () {
    Route::get('view', [ViewController::class, 'show']);
});

运行此代码,则只能显示何时登录(用户不需要登录)。因此,用户看不到任何帖子。如果我删除auth:Sanctum中间件,则只能运行代码的其他部分,并且没有验证检查或任何东西可以运行。

我需要一种将两者都合并到单个功能中的方法,以便我可以创建一个单个路线,而不是为不同人创建两个路线。有什么办法做这样的事情吗?

I'm creating an api through which anybody can view a page, however only admin can see all posts, while users are restricted to approved only. This is implemented via is_verified boolean variable where admin is given value of 1 and user the value of 0. I want to create a function like this


public function show(){
    if(Auth::check()){
        $showAllDetails = Events::all();
        echo $showAllDetails;   
    }else {
        $showUserDetails = Events:all()->where('is_verified',1);
        echo $showUserDetails;
    }
}

However Auth:check only works if I have sanctum api in my route

Route::middleware('auth:sanctum')->group(function () {
    Route::get('view', [ViewController::class, 'show']);
});

If I run this code on Hoppscotch, it only shows if the admin is logged in (User don't require login). So a user can't see any post. If I remove the auth:sanctum middleware, only the else part of the code runs and no auth check or any stuff can run .

I need a way to incorporate both in a single function so that I can create a single route instead of creating two routes for different persons. Any way of doing such things?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雄赳赳气昂昂 2025-02-20 23:25:39
public function show(){
  if(Auth::check()){
    $showAllDetails = Events::all();
    echo $showAllDetails;   
  }else {
    $showUserDetails = Events::where('is_verified',1)->get();
    echo $showUserDetails;
 }
}

我想您的其他部分是错误的查询,更改您的其他部分

public function show(){
  if(Auth::check()){
    $showAllDetails = Events::all();
    echo $showAllDetails;   
  }else {
    $showUserDetails = Events::where('is_verified',1)->get();
    echo $showUserDetails;
 }
}

I guess your else part is incorrect query, change your else part like above

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文