1
Fork 0

Gate certain features by requiring a logged in user.

This commit is contained in:
Bauke 2023-07-15 15:20:05 +02:00
parent 8a7132c276
commit 809943edcc
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
3 changed files with 29 additions and 6 deletions

View File

@ -1,5 +1,10 @@
import {type JSX, render} from "preact"; import {type JSX, render} from "preact";
import {extractGroups, initializeGlobals, log} from "../utilities/exports.js"; import {
extractGroups,
initializeGlobals,
log,
userIsLoggedIn,
} from "../utilities/exports.js";
import { import {
Data, Data,
Feature, Feature,
@ -36,6 +41,11 @@ async function initialize() {
const knownGroups = await fromStorage(Data.KnownGroups); const knownGroups = await fromStorage(Data.KnownGroups);
const userLabels = await fromStorage(Feature.UserLabels); const userLabels = await fromStorage(Feature.UserLabels);
const isLoggedIn = userIsLoggedIn();
if (!isLoggedIn) {
log("User is not logged in, running with limited features enabled.", true);
}
// Only when any of the features that uses this data are enabled, try to save // Only when any of the features that uses this data are enabled, try to save
// the groups. // the groups.
if ( if (
@ -89,7 +99,7 @@ async function initialize() {
}); });
} }
if (enabledFeatures.value.has(Feature.MarkdownToolbar)) { if (enabledFeatures.value.has(Feature.MarkdownToolbar) && isLoggedIn) {
observerFeatures.push(() => { observerFeatures.push(() => {
runMarkdownToolbarFeature(); runMarkdownToolbarFeature();
}); });
@ -121,7 +131,7 @@ async function initialize() {
// Object to hold the active components we are going to render. // Object to hold the active components we are going to render.
const components: Record<string, JSX.Element | undefined> = {}; const components: Record<string, JSX.Element | undefined> = {};
if (enabledFeatures.value.has(Feature.Autocomplete)) { if (enabledFeatures.value.has(Feature.Autocomplete) && isLoggedIn) {
components.autocomplete = ( components.autocomplete = (
<AutocompleteFeature <AutocompleteFeature
anonymizeUsernamesEnabled={anonymizeUsernamesEnabled} anonymizeUsernamesEnabled={anonymizeUsernamesEnabled}
@ -135,7 +145,7 @@ async function initialize() {
components.backToTop = <BackToTopFeature />; components.backToTop = <BackToTopFeature />;
} }
if (enabledFeatures.value.has(Feature.JumpToNewComment)) { if (enabledFeatures.value.has(Feature.JumpToNewComment) && isLoggedIn) {
components.jumpToNewComment = <JumpToNewCommentFeature />; components.jumpToNewComment = <JumpToNewCommentFeature />;
} }
@ -152,11 +162,17 @@ async function initialize() {
runCommentAnchorFixFeature(); runCommentAnchorFixFeature();
} }
if (miscEnabled.value.has(MiscellaneousFeature.GroupListSubscribeButtons)) { if (
miscEnabled.value.has(MiscellaneousFeature.GroupListSubscribeButtons) &&
isLoggedIn
) {
runGroupListSubscribeButtonFeature(); runGroupListSubscribeButtonFeature();
} }
if (miscEnabled.value.has(MiscellaneousFeature.TopicInfoIgnore)) { if (
miscEnabled.value.has(MiscellaneousFeature.TopicInfoIgnore) &&
isLoggedIn
) {
runTopicInfoIgnore(); runTopicInfoIgnore();
} }

View File

@ -7,4 +7,5 @@ export * from "./logging.js";
export * from "./query-selectors.js"; export * from "./query-selectors.js";
export * from "./report-a-bug.js"; export * from "./report-a-bug.js";
export * from "./text.js"; export * from "./text.js";
export * from "./user.js";
export * from "./validators.js"; export * from "./validators.js";

6
source/utilities/user.ts Normal file
View File

@ -0,0 +1,6 @@
/**
* Checks whether a user is logged in on or not.
*/
export function userIsLoggedIn(): boolean {
return document.querySelector(".logged-in-user-username") !== null;
}