-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add guest user #795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: add guest user #795
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,20 +3,68 @@ | |||||
| * SPDX-License-Identifier: GPL-3.0-or-later | ||||||
| */ | ||||||
| import { getBuilder } from '@nextcloud/browser-storage' | ||||||
| import { NextcloudUser } from './user' | ||||||
| import { emit } from '@nextcloud/event-bus' | ||||||
|
|
||||||
| const browserStorage = getBuilder('public').persist().build() | ||||||
|
|
||||||
| class GuestUser implements NextcloudUser { | ||||||
|
|
||||||
| private _displayName: string | null | ||||||
| readonly uid: string | ||||||
| readonly isAdmin: boolean | ||||||
|
|
||||||
| constructor() { | ||||||
| if (!browserStorage.getItem('guestUid')) { | ||||||
| browserStorage.setItem('guestUid', self.crypto.randomUUID()) | ||||||
| } | ||||||
|
|
||||||
| this._displayName = browserStorage.getItem('guestNickname') || '' | ||||||
| this.uid = browserStorage.getItem('guestUid') || self.crypto.randomUUID() | ||||||
| this.isAdmin = false | ||||||
|
|
||||||
| } | ||||||
|
|
||||||
| get displayName(): string | null { | ||||||
| return this._displayName | ||||||
| } | ||||||
|
|
||||||
| set displayName(displayName: string) { | ||||||
| this._displayName = displayName | ||||||
| browserStorage.setItem('guestNickname', displayName) | ||||||
| emit('user:info:changed', this) | ||||||
| } | ||||||
|
|
||||||
| } | ||||||
|
|
||||||
| let currentUser: GuestUser | undefined | ||||||
|
|
||||||
| /** | ||||||
| * Get the currently Guest user or null if not logged in | ||||||
| */ | ||||||
| export function getGuestUser(): GuestUser { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either
Suggested change
Or we would need to also export the type of GuestUser. |
||||||
| if (!currentUser) { | ||||||
| currentUser = new GuestUser() | ||||||
| } | ||||||
|
|
||||||
| return currentUser | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Get the guest nickname for public pages | ||||||
| */ | ||||||
| export function getGuestNickname(): string | null { | ||||||
| return browserStorage.getItem('guestNickname') | ||||||
| return getGuestUser()?.displayName || null | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Set the guest nickname for public pages | ||||||
| * @param nickname The nickname to set | ||||||
| */ | ||||||
| export function setGuestNickname(nickname: string): void { | ||||||
| browserStorage.setItem('guestNickname', nickname) | ||||||
| if (!nickname || nickname.trim().length === 0) { | ||||||
| throw new Error('Nickname cannot be empty') | ||||||
| } | ||||||
|
|
||||||
| getGuestUser().displayName = nickname | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we would export this class type (skip this depending on the other comment), then the type includes this private field.
Also it is accessible and only marked private for typescript.
You might consider instead using standard ES visibility:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought # was dropped in the end? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No they are standardized in es2019:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_properties
Its more like
protectedbut enforced by runtime, meaning you cannot access the property from outside the class.Whereas the typescript
private,protected, andreadonlyare just types which get stripped away:Meaning you could still access those private fields, while

#somethingwill throw an exception:In the end its not really important, both have good reasons to use or not to use, it depends :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, absolutely!
But I remember using the
#a while back, when it was drafted and about to be accepted.But I also remember that we rolled back and stopped using it 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are some places using it, but it really is not that important.
If you mess with private fields you better know what you are doing, so its your risk.
Meaning its just a matter of taste if you prefer Typescript sugar or Javascript syntax.
E.g. a benefit of Typescript sugar is that you can unit test it (while also there are some guidelines that discurage you from testing private properties, but thats another topic^^).