使用中间件在路由 API 中进行身份验证(已解决)

huangapple go评论55阅读模式
英文:

Authentication using Middleware in route api (solved)

问题

在 Laravel 的 api.php 中,我想在 api.php 文件中进行身份验证,我使用自定义中间件来识别 Auth()->role。

我已经完成以下操作:

使用中间件在路由 API 中进行身份验证(已解决)
使用中间件在路由 API 中进行身份验证(已解决)

我的问题是,当我以作者身份登录时,它显示未经授权(尽管我已在 route api.php 中声明了该路由)。但是,当我以管理员身份登录时,它正常工作。

使用中间件在路由 API 中进行身份验证(已解决)

编辑:
唯一的问题是,我在声明角色时使用了空格,像这样
使用中间件在路由 API 中进行身份验证(已解决)

答案是删除空格,如下所示 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:

使用中间件在路由 API 中进行身份验证(已解决)
使用中间件在路由 API 中进行身份验证(已解决)

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

使用中间件在路由 API 中进行身份验证(已解决)

EDIT:
the only problem is i use whitespaces when i declared the role, like this
使用中间件在路由 API 中进行身份验证(已解决)

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

huangapple
  • 本文由 发表于 2023年5月28日 14:52:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定