aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_sharing/src/public-file-request.ts
blob: 92db2440525d7dc38b0db9cfe23df33eb8acac7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

import { getGuestNickname, setGuestNickname } from '@nextcloud/auth'
import { spawnDialog } from '@nextcloud/dialogs'
import { getUploader } from '@nextcloud/upload'
import { defineAsyncComponent } from 'vue'
import logger from './services/logger'
import { getBuilder } from '@nextcloud/browser-storage'

const storage = getBuilder('files_sharing').build()

/**
 * Setup file-request nickname header for the uploader
 * @param nickname The nickname
 */
function registerFileRequestHeader(nickname: string) {
	const uploader = getUploader()
	uploader.setCustomHeader('X-NC-Nickname', nickname)
	logger.debug('Nickname header registered for uploader', { headers: uploader.customHeaders })
}

/**
 * Callback when a nickname was chosen
 * @param nickname The chosen nickname
 */
function onSetNickname(nickname: string): void {
	// Set the nickname
	setGuestNickname(nickname)
	// Set the dialog as shown
	storage.setItem('public-auth-prompt-shown', 'true')
	// Register header for uploader
	registerFileRequestHeader(nickname)
}

window.addEventListener('DOMContentLoaded', () => {
	const nickname = getGuestNickname() ?? ''
	const dialogShown = storage.getItem('public-auth-prompt-shown') !== null

	// If we don't have a nickname or the public auth prompt hasn't been shown yet, show it
	// We still show the prompt if the user has a nickname to double check
	if (!nickname || !dialogShown) {
		spawnDialog(
			defineAsyncComponent(() => import('./views/PublicAuthPrompt.vue')),
			{
				nickname,
			},
			onSetNickname as (...rest: unknown[]) => void,
		)
	} else {
		logger.debug('Public auth prompt already shown.', { nickname })
		registerFileRequestHeader(nickname)
	}
})