简体   繁体   English

Laravel5.6 + Passport Api身份验证和Web身份验证

[英]Laravel5.6+ Passport Api Auth and web auth

I'm rather beginner in laravel, and I need some help. 我是laravel的初学者,我需要一些帮助。 Could somebody help me with a task? 有人可以帮我完成任务吗?

I'm creating some web service, and the task is to connect front-end blade template with back-end via API requests. 我正在创建一些Web服务,任务是通过API请求将前端刀片服务器模板与后端连接。 I created API auth via Laravel passport, got token. 我通过Laravel护照创建了API身份验证,获得了令牌。

And the question is, how would be correctly get this auth token and use it in blade and in requests from blade to back-end? 问题是,如何正确获取此auth令牌并在刀片以及刀片到后端的请求中使用它?

i thought about cookies, or creating some middleware based on the cookies, - is it properly? 我想到过cookie,或基于cookie创建一些中间件,-是否正确? perhaps there exist another right way to implement this? 也许存在另一种正确的方法来实现这一目标?

Thanks!) 谢谢!)

It depends on what you are trying to do. 这取决于您要执行的操作。 Many people store the token in both cookie and browser localstorage. 许多人将令牌存储在cookie和浏览器本地存储中。 This has the benefit to access the token from both backend and frontend withouth any extra libraries. 这样可以从后端和前端访问令牌,而无需任何其他库。 If you are building an SPA with SSR this maybe more difficult when it comes to refresh that token in a request which it happend during the ssr where you have no access to localstorage of the browser so it may be better to store it only on cookies. 如果您要使用SSR构建SPA,则在刷新请求期间刷新令牌时可能会更加困难,因为在ssr期间您无法访问浏览器的本地存储,因此最好将其存储在cookie上。 Personally i use both approaches depending on what i need to build. 就我个人而言,我会根据需要构建两种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM