Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions lib/guest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

constructor() {
if (!browserStorage.getItem('guestUid')) {
browserStorage.setItem('guestUid', self.crypto.randomUUID())
browserStorage.setItem('guestUid', randomUUID())
}

this._displayName = browserStorage.getItem('guestNickname') || ''
this.uid = browserStorage.getItem('guestUid') || self.crypto.randomUUID()
this.uid = browserStorage.getItem('guestUid') || randomUUID()
this.isAdmin = false

subscribe('user:info:changed', (guest) => {
Expand Down Expand Up @@ -73,3 +73,24 @@

getGuestUser().displayName = nickname
}

/**
* Generate a random UUID (version 4) if the crypto API is not available.
* If the crypto API is available, it uses the less secure `randomUUID` method.
* Crypto API is available in modern browsers on secure contexts (HTTPS).
*
* @return {string} A random UUID.

Check failure on line 82 in lib/guest.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Types are not permitted on @return
*/
function randomUUID(): string {
// Use the crypto API if available
if (globalThis.crypto?.randomUUID) {
return globalThis.crypto.randomUUID()
}

// Generate a random UUID (version 4)
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = Math.random() * 16 | 0
const v = c === 'x' ? r : (r & 0x3 | 0x8)
return v.toString(16)
})
}
Loading