Skip to content

Commit 23eb045

Browse files
committed
fix: make current guest and user global
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
1 parent d14a110 commit 23eb045

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

lib/guest.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { emit } from '@nextcloud/event-bus'
88

99
const browserStorage = getBuilder('public').persist().build()
1010

11-
class GuestUser implements NextcloudUser {
11+
export class GuestUser implements NextcloudUser {
1212

1313
private _displayName: string | null
1414
readonly uid: string
@@ -37,17 +37,15 @@ class GuestUser implements NextcloudUser {
3737

3838
}
3939

40-
let currentUser: NextcloudUser | undefined
41-
4240
/**
4341
* Get the currently Guest user or null if not logged in
4442
*/
4543
export function getGuestUser(): NextcloudUser {
46-
if (!currentUser) {
47-
currentUser = new GuestUser()
44+
if (!window._nc_currentGuestUser) {
45+
window._nc_currentGuestUser = new GuestUser()
4846
}
4947

50-
return currentUser
48+
return window._nc_currentGuestUser
5149
}
5250

5351
/**

lib/user.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,12 @@
22
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
33
* SPDX-License-Identifier: GPL-3.0-or-later
44
*/
5-
declare global {
6-
interface Window {
7-
_oc_isadmin?: boolean
8-
}
9-
}
10-
115
export interface NextcloudUser {
126
uid: string,
137
displayName: string | null,
148
isAdmin: boolean,
159
}
1610

17-
let currentUser: NextcloudUser | null | undefined
18-
1911
const getAttribute = (el: HTMLHeadElement | undefined, attribute: string): string | null => {
2012
if (el) {
2113
return el.getAttribute(attribute)
@@ -28,8 +20,8 @@ const getAttribute = (el: HTMLHeadElement | undefined, attribute: string): strin
2820
* Get the currently logged in Nextcloud user or null if not logged in
2921
*/
3022
export function getCurrentUser(): NextcloudUser | null {
31-
if (currentUser !== undefined) {
32-
return currentUser
23+
if (window._nc_currentUser !== undefined) {
24+
return window._nc_currentUser
3325
}
3426

3527
const head = document?.getElementsByTagName('head')[0]
@@ -40,15 +32,15 @@ export function getCurrentUser(): NextcloudUser | null {
4032
// No user logged in so cache and return null
4133
const uid = getAttribute(head, 'data-user')
4234
if (uid === null) {
43-
currentUser = null
44-
return currentUser
35+
window._nc_currentUser = null
36+
return window._nc_currentUser
4537
}
4638

47-
currentUser = {
39+
window._nc_currentUser = {
4840
uid,
4941
displayName: getAttribute(head, 'data-user-displayname'),
5042
isAdmin: !!window._oc_isadmin,
5143
} as NextcloudUser
5244

53-
return currentUser
45+
return window._nc_currentUser
5446
}

lib/window.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*/
5+
import type { GuestUser } from './guest'
6+
import type { NextcloudUser } from './user';
7+
8+
declare global {
9+
interface Window {
10+
_nc_currentGuestUser: GuestUser | undefined;
11+
_nc_currentUser: NextcloudUser | undefined | null;
12+
_oc_isadmin?: boolean
13+
}
14+
}

0 commit comments

Comments
 (0)