简体   繁体   English

在每个请求上传递参数api

[英]rest api passing parameter on every request

I have a rest api which has organisations as a top level object and all other data belongs to an organisation eg orders, invoices, projects etc.. 我有一个rest api,它以组织为顶层对象,而所有其他数据都属于组织,例如订单,发票,项目等。

Users have the ability to belong to multiple organisations and on my frond end SPA I want them to be logged into one application at a time with the ability to switch. 用户具有属于多个组织的能力,并且在我的第一代SPA上,我希望他们能够同时登录到一个应用程序中。

Should I be storing the current organisation id on the client side and passing it with every request or should i have a rest endpoint that sets the current org id on the user table etc, which will also mean when they come back and login it is already set to the last organisation they accessed. 我应该在客户端存储当前组织ID并随每个请求传递它,还是应该有一个休息端点在用户表等上设置当前组织ID,这也意味着当他们再次登录并登录时设置为他们访问的最后一个组织。

In general, stateless APIs are fairly common. 通常,无状态API相当普遍。 For instance, you might pass an Auth Token (JWT) with every single request, which identifies the User and could potentially provide other info about the User as well. 例如,您可能会在每个请求中传递一个Auth Token(JWT),以标识用户并可能还提供有关该用户的其他信息。

If you're using Laravel, you might want to look into this package to handle JSON Web Tokens: https://github.com/tymondesigns/jwt-auth . 如果您使用的是Laravel,则可能需要研究此程序包以处理JSON Web令牌: https : //github.com/tymondesigns/jwt-auth You could then serialize your User object on Login, which could also include their current Organization. 然后,您可以在Login上序列化User对象,其中还可以包括其当前的Organization。 The frontend can then pass this token with every request (as an Authorization Header) 然后,前端可以随每个请求传递此令牌(作为授权标头)

Should I be storing the current organization id on the client side and passing it with every request? 我应该在客户端存储当前组织ID并随每个请求传递它吗?

Yes , a general practice is sending the information in each API because API are stateless. 是的 ,由于API是无状态的,因此通常的做法是在每个API中发送信息。

Should I have a rest endpoint that sets the current org id on the user table? 我是否应该有一个休息端点来设置用户表上的当前组织ID?

NO you should not, one of the reason is let say user logs in browser1 you will store that organizationId in table, same user login from browser2 this will override your previous Id 否,您不应该这样做,原因之一是,假设用户登录浏览器1,您会将该组织ID存储在表中,同一用户从浏览器2登录,这将覆盖您之前的ID

Now if you go back to browser1 it will have inconsistent data unless you had designed a mechanism not to override the previous value. 现在,如果您返回浏览器1,它将具有不一致的数据,除非您设计了不覆盖先前值的机制。

Hope that make sense 希望有道理

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

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