Skip to content

Commit 5590535

Browse files
fix(plugin-multi-tenant): not setting cookie properly on login (#14107)
When using something like oauth, the cookie needs to be set. This is because the login happens elsewhere and the user is redirected. When the admin panel loads, there is already a user so the `useEffect` does not run since the user does not change like it does when logging in without oauth. The fix was to check if there is an intialValue and no matching cookie, then sync the tenants which will also set the cookie. Also useful if a user logs in and has a stale tenant cookie set.
1 parent ee78eb2 commit 5590535

File tree

1 file changed

+24
-10
lines changed
  • packages/plugin-multi-tenant/src/providers/TenantSelectionProvider

1 file changed

+24
-10
lines changed

packages/plugin-multi-tenant/src/providers/TenantSelectionProvider/index.client.tsx

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,40 @@ const Context = createContext<ContextType>({
6565
updateTenants: () => null,
6666
})
6767

68-
const setCookie = (value?: string) => {
68+
const DEFAULT_COOKIE_NAME = 'payload-tenant'
69+
70+
const setTenantCookie = (args: { cookieName?: string; value: string }) => {
71+
const { cookieName = DEFAULT_COOKIE_NAME, value } = args
6972
document.cookie = generateCookie<string>({
70-
name: 'payload-tenant',
73+
name: cookieName,
7174
maxAge: 60 * 60 * 24 * 365, // 1 year in seconds
7275
path: '/',
7376
returnCookieAsObject: false,
7477
value: value || '',
7578
})
7679
}
7780

78-
const deleteCookie = () => {
81+
const deleteTenantCookie = (args: { cookieName?: string } = {}) => {
82+
const { cookieName = DEFAULT_COOKIE_NAME } = args
7983
document.cookie = generateCookie<string>({
80-
name: 'payload-tenant',
84+
name: cookieName,
8185
maxAge: -1,
8286
path: '/',
8387
returnCookieAsObject: false,
8488
value: '',
8589
})
8690
}
8791

92+
const getTenantCookie = (args: { cookieName?: string } = {}): string | undefined => {
93+
const { cookieName = DEFAULT_COOKIE_NAME } = args
94+
const value = `; ${document.cookie}`
95+
const parts = value.split(`; ${cookieName}=`)
96+
if (parts.length === 2) {
97+
return parts.pop()?.split(';').shift()
98+
}
99+
return undefined
100+
}
101+
88102
export const TenantSelectionProviderClient = ({
89103
children,
90104
initialTenantOptions,
@@ -119,9 +133,9 @@ export const TenantSelectionProviderClient = ({
119133
({ id, refresh }: { id: number | string | undefined; refresh?: boolean }) => {
120134
setSelectedTenantID(id)
121135
if (id !== undefined) {
122-
setCookie(String(id))
136+
setTenantCookie({ value: String(id) })
123137
} else {
124-
deleteCookie()
138+
deleteTenantCookie()
125139
}
126140
if (refresh) {
127141
router.refresh()
@@ -171,7 +185,7 @@ export const TenantSelectionProviderClient = ({
171185

172186
if (result.tenantOptions.length === 1) {
173187
setSelectedTenantID(result.tenantOptions[0].value)
174-
setCookie(String(result.tenantOptions[0].value))
188+
setTenantCookie({ value: String(result.tenantOptions[0].value) })
175189
}
176190
}
177191
} catch (e) {
@@ -199,21 +213,21 @@ export const TenantSelectionProviderClient = ({
199213
)
200214

201215
React.useEffect(() => {
202-
if (userChanged) {
216+
if (userChanged || (initialValue && String(initialValue) !== getTenantCookie())) {
203217
if (userID) {
204218
// user logging in
205219
void syncTenants()
206220
} else {
207221
// user logging out
208222
setSelectedTenantID(undefined)
209-
deleteCookie()
223+
deleteTenantCookie()
210224
if (tenantOptions.length > 0) {
211225
setTenantOptions([])
212226
}
213227
}
214228
prevUserID.current = userID
215229
}
216-
}, [userID, userChanged, syncTenants, tenantOptions])
230+
}, [userID, userChanged, syncTenants, tenantOptions, initialValue])
217231

218232
/**
219233
* If there is no initial value, clear the tenant and refresh the router.

0 commit comments

Comments
 (0)