Web和Api路由的Laravel Auth问题 [英] Laravel Auth Problem for Web and Api Routes
问题描述
不幸的是,我找不到针对用户案例的解决方案,而这种情况实际上必须经常发生.
Unfortunately, I can't find a solution for a user case, which must actually occur frequently.
为了最好地描述它,我将laravelapp的路线(网络和api)结构放在这里:
To describe it best, I put here the structure of my routes (web and api) of my laravelapp:
routes/web.php
get(start|products|product/{id}|contact|inprint|login|register)
get(checkout|checkout/(any))->middleware( auth ) // Checkout is a VUE App
routes/api.php
// only the Vue App use the api
get(user|user_orders|user_address) ->middleware( auth )
post (purchase_order) -> middleware(auth)
您可能会看到我想保护网站的某些部分.此外,我想检查每个查询是否登录用户,以便在导航栏中显示注销按钮.
As you can probably see I want to protect parts of the website. Furthermore, I would like to check every query whether the user is logged in in order to display the logout button in the navbar.
- 使用身份验证系统是否有可能?
- 是否可以使用基于令牌的(JWT)身份验证系统来保护Web路由?我一直以为没有.
感谢您的帮助!
推荐答案
您可以执行相反的操作.这样,您便可以灵活地使用
You can do the reverse. That will give you the flexibility to check your auth in your controller with
Auth::check();
通过这些简单的步骤,您还可以在API路由中检查auth.因此,您还可以将JWT与身份验证Web路由集成在一起.
With these simple steps, you can check auth in API routes also. So you can integrate the JWT this also along with auth web routes.
您只需要添加
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
在"API"中间件组中
In your 'API' middleware group
'api' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
\\ existing code...
],
谢谢
这篇关于Web和Api路由的Laravel Auth问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!