Skip to content

Commit

Permalink
feat: Initialize cached values later
Browse files Browse the repository at this point in the history
Currently the current user and the csrf token are initialized when the file
is loaded, but this will fail when imported in tests without DOM or for SSR.
So instead the cached values are loaded on the first usage.

Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
  • Loading branch information
susnux committed Mar 8, 2023
1 parent 38366cb commit 03cdc5c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
19 changes: 16 additions & 3 deletions lib/requesttoken.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import { subscribe } from '@nextcloud/event-bus'

const tokenElement = document.getElementsByTagName('head')[0]
let token = tokenElement ? tokenElement.getAttribute('data-requesttoken') : null

export interface CsrfTokenObserver {
(token: string): void;
}

let token: string | null | undefined = undefined
const observers: CsrfTokenObserver[] = []

/**
* Get current request token
*
* @return {string|null} Current request token or null if not set
*/
export function getRequestToken(): string | null {
if (token === undefined) {
// Only on first load, try to get token from document
const tokenElement = document?.getElementsByTagName('head')[0]
token = tokenElement ? tokenElement.getAttribute('data-requesttoken') : null
}
return token
}

/**
* Add an observer which is called when the CSRF token changes
*
* @param observer The observer
*/
export function onRequestTokenUpdate(observer: CsrfTokenObserver): void {
observers.push(observer)
}
Expand Down
42 changes: 22 additions & 20 deletions lib/user.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
/// <reference types="@nextcloud/typings" />

declare var OC: Nextcloud.v16.OC
| Nextcloud.v17.OC
| Nextcloud.v18.OC
| Nextcloud.v19.OC
| Nextcloud.v20.OC
| Nextcloud.v21.OC
| Nextcloud.v22.OC
| Nextcloud.v20.OC
| Nextcloud.v24.OC;
declare var OC: Nextcloud.v23.OC
| Nextcloud.v24.OC
| Nextcloud.v25.OC;

const getAttribute = (el: HTMLHeadElement | undefined, attribute: string): string | null => {
if (el) {
Expand All @@ -18,13 +12,7 @@ const getAttribute = (el: HTMLHeadElement | undefined, attribute: string): strin
return null
}

const head = document.getElementsByTagName('head')[0]
const uid = getAttribute(head, 'data-user')
const displayName = getAttribute(head, 'data-user-displayname')

const isAdmin = (typeof OC === 'undefined')
? false
: OC.isUserAdmin()
let currentUser: NextcloudUser | null | undefined = undefined

export interface NextcloudUser {
uid: string,
Expand All @@ -33,13 +21,27 @@ export interface NextcloudUser {
}

export function getCurrentUser(): NextcloudUser | null {
if (uid === null) {
if (currentUser !== undefined) {
return currentUser
}

const head = document?.getElementsByTagName('head')[0]
if (!head) {
return null
}

return {
// No user logged in so cache and return null
const uid = getAttribute(head, 'data-user')
if (uid === null) {
currentUser = null
return currentUser
}

currentUser = {
uid,
displayName,
isAdmin,
displayName: getAttribute(head, 'data-user-displayname'),
isAdmin: (typeof OC === 'undefined') ? false : OC.isUserAdmin(),
} as NextcloudUser

return currentUser
}

0 comments on commit 03cdc5c

Please sign in to comment.