英文:
Authentication using Middleware in route api (solved)
问题
在 Laravel 的 api.php 中,我想在 api.php 文件中进行身份验证,我使用自定义中间件来识别 Auth()->role。
我已经完成以下操作:
我的问题是,当我以作者身份登录时,它显示未经授权(尽管我已在 route api.php 中声明了该路由)。但是,当我以管理员身份登录时,它正常工作。
答案是删除空格,如下所示 Waad Mawlood 的回答所示。
英文:
i wanted to do authentication inside api.php in laravel, i use custom middleware to identify Auth()->role.
this is what i have done:
my problem is, when i logged on as author it said not authorized(which i have declare the route in route api.php). but, when i use admin, it works well
EDIT:
the only problem is i use whitespaces when i declared the role, like this
the answer is to delete the whitespaces as referred to Waad Mawlood answer below
答案1
得分: 1
简单地使用
auth()->user()->role
而不是
$request->user()->role
⚠️ 在角色之间不要留下空格 => `admin,author`
英文:
simply used
auth()->user()->role
instead of
$request->user()->role
⚠️ do not leave white spaces between roles => admin,author
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论