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-
115export interface NextcloudUser {
126 uid : string ,
137 displayName : string | null ,
148 isAdmin : boolean ,
159}
1610
17- let currentUser : NextcloudUser | null | undefined
18-
1911const 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 */
3022export 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}
0 commit comments