mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-02 19:42:51 +08:00
feat: remember server and channel passwords locally
This commit is contained in:
1 parent
9c8647df50
commit
2f5cdcba90
6 files changed
+289
-43
No files matched your search
@@ -2,6 +2,14 @@ import { computed, nextTick, watch, type Ref } from "vue";
|
||||
import type { WebClientChatTab } from "./useWebClientChat.js";
|
||||
import type { TreeChannel } from "./useWebClientChannels.js";
|
||||
import { combineTeamSpeakTarget, isValidTeamSpeakPort, suggestedTeamSpeakPort } from "../services/teamspeak-target.js";
|
||||
import {
|
||||
loadStoredChannelPassword,
|
||||
loadStoredServerPassword,
|
||||
removeStoredChannelPassword,
|
||||
removeStoredServerPassword,
|
||||
saveStoredChannelPassword,
|
||||
saveStoredServerPassword,
|
||||
} from "../services/local-persistence.js";
|
||||
|
||||
interface ChannelPasswordDialogState {
|
||||
open: boolean;
|
||||
@@ -21,6 +29,7 @@ interface UseWebClientConnectionOptions {
|
||||
initialized: Readonly<Ref<boolean>>;
|
||||
accessMode: Readonly<Ref<string>>;
|
||||
isConnecting: Readonly<Ref<boolean>>;
|
||||
isConnected: Readonly<Ref<boolean>>;
|
||||
errorCode: Readonly<Ref<string>>;
|
||||
errorMessage: Readonly<Ref<string>>;
|
||||
channelSwitchedChannelId: Readonly<Ref<string>>;
|
||||
@@ -53,6 +62,7 @@ export function useWebClientConnection({
|
||||
initialized,
|
||||
accessMode,
|
||||
isConnecting,
|
||||
isConnected,
|
||||
errorCode,
|
||||
errorMessage,
|
||||
channelSwitchedChannelId,
|
||||
@@ -90,12 +100,28 @@ export function useWebClientConnection({
|
||||
serverPort.value = suggestedTeamSpeakPort(previousAddress, address, serverPort.value);
|
||||
}, { flush: "sync" });
|
||||
|
||||
let serverPasswordLookupAttempt = 0;
|
||||
let resolvingServerPassword = false;
|
||||
let attemptedServerCredential: { target: string; password: string } | null = null;
|
||||
let channelSelectionAttempt = 0;
|
||||
let attemptedChannelCredential: { target: string; channelId: string; password: string } | null = null;
|
||||
|
||||
function currentServerTarget(): string {
|
||||
return combineTeamSpeakTarget(serverHost.value, serverPort.value);
|
||||
}
|
||||
|
||||
watch(() => currentServerTarget(), (target, previousTarget) => {
|
||||
if (!previousTarget || target === previousTarget) return;
|
||||
serverPassword.value = "";
|
||||
resolvingServerPassword = false;
|
||||
serverPasswordLookupAttempt += 1;
|
||||
attemptedServerCredential = null;
|
||||
attemptedChannelCredential = null;
|
||||
channelSelectionAttempt += 1;
|
||||
}, { flush: "sync" });
|
||||
|
||||
function doConnect(): void {
|
||||
if (!canJoin.value || isConnecting.value) return;
|
||||
if (!canJoin.value || isConnecting.value || resolvingServerPassword) return;
|
||||
beforeConnect?.();
|
||||
clearError();
|
||||
nickname.value = nickname.value.trim();
|
||||
@@ -105,21 +131,41 @@ export function useWebClientConnection({
|
||||
serverPort.value = serverPort.value.trim();
|
||||
}
|
||||
selectedChannelId.value = "";
|
||||
// Keep the password available for retry even if the target is administrator-managed.
|
||||
connect(
|
||||
currentServerTarget(),
|
||||
channelName.value.trim(),
|
||||
nickname.value,
|
||||
serverPassword.value,
|
||||
rememberIdentity.value ? identityMaterial.value : "",
|
||||
rememberIdentity.value,
|
||||
inviteToken.value,
|
||||
Boolean(accelerationRelayId.value),
|
||||
accelerationRelayId.value,
|
||||
);
|
||||
const target = currentServerTarget();
|
||||
const attempt = ++serverPasswordLookupAttempt;
|
||||
resolvingServerPassword = true;
|
||||
void (async () => {
|
||||
try {
|
||||
const savedPassword = serverPassword.value ? "" : await loadStoredServerPassword(target);
|
||||
if (attempt !== serverPasswordLookupAttempt || target !== currentServerTarget()) return;
|
||||
// A password entered while IndexedDB was being read takes precedence.
|
||||
const password = serverPassword.value || savedPassword;
|
||||
attemptedServerCredential = { target, password };
|
||||
// Keep the in-memory value available for the existing reconnect flow.
|
||||
serverPassword.value = password;
|
||||
connect(
|
||||
target,
|
||||
channelName.value.trim(),
|
||||
nickname.value,
|
||||
password,
|
||||
rememberIdentity.value ? identityMaterial.value : "",
|
||||
rememberIdentity.value,
|
||||
inviteToken.value,
|
||||
Boolean(accelerationRelayId.value),
|
||||
accelerationRelayId.value,
|
||||
);
|
||||
} finally {
|
||||
if (attempt === serverPasswordLookupAttempt) resolvingServerPassword = false;
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
function doDisconnect(): void {
|
||||
serverPasswordLookupAttempt += 1;
|
||||
resolvingServerPassword = false;
|
||||
attemptedServerCredential = null;
|
||||
channelSelectionAttempt += 1;
|
||||
attemptedChannelCredential = null;
|
||||
disconnect();
|
||||
selectedChannelId.value = "";
|
||||
showToast(t("leftToast"));
|
||||
@@ -145,17 +191,30 @@ export function useWebClientConnection({
|
||||
selectedChannelId.value = channel.id;
|
||||
channelName.value = channel.name;
|
||||
chatTab.value = "channel";
|
||||
switchChannel(channel.id);
|
||||
const target = currentServerTarget();
|
||||
const attempt = ++channelSelectionAttempt;
|
||||
attemptedChannelCredential = null;
|
||||
void loadStoredChannelPassword(target, channel.id).then((password) => {
|
||||
if (attempt !== channelSelectionAttempt || selectedChannelId.value !== channel.id || target !== currentServerTarget()) return;
|
||||
attemptedChannelCredential = password ? { target, channelId: channel.id, password } : null;
|
||||
switchChannel(channel.id, password);
|
||||
});
|
||||
}
|
||||
|
||||
function submitChannelPassword(): void {
|
||||
if (!channelPasswordDialog.open || channelPasswordDialog.submitting || !channelPasswordDialog.channelId || !channelPasswordDialog.password) return;
|
||||
channelPasswordDialog.error = "";
|
||||
channelPasswordDialog.submitting = true;
|
||||
switchChannel(channelPasswordDialog.channelId, channelPasswordDialog.password);
|
||||
const target = currentServerTarget();
|
||||
const channelId = channelPasswordDialog.channelId;
|
||||
attemptedChannelCredential = { target, channelId, password: channelPasswordDialog.password };
|
||||
channelSelectionAttempt += 1;
|
||||
switchChannel(channelId, channelPasswordDialog.password);
|
||||
}
|
||||
|
||||
function cancelChannelPassword(): void {
|
||||
channelSelectionAttempt += 1;
|
||||
attemptedChannelCredential = null;
|
||||
const ownChannel = channels.value.find((item) => item.members.some((member) => member.id === clientId.value));
|
||||
if (ownChannel) selectedChannelId.value = ownChannel.id;
|
||||
channelPasswordDialog.open = false;
|
||||
@@ -172,36 +231,61 @@ export function useWebClientConnection({
|
||||
}
|
||||
|
||||
watch(errorCode, (code) => {
|
||||
if (code && code !== "CHANNEL_PASSWORD_REQUIRED" && channelPasswordDialog.open && channelPasswordDialog.submitting) {
|
||||
channelPasswordDialog.error = errorMessage.value;
|
||||
if (code === "CHANNEL_PASSWORD_REQUIRED" || code === "INVALID_CHANNEL_PASSWORD") {
|
||||
const channelId = selectedChannelId.value;
|
||||
if (!channelId) return;
|
||||
const target = currentServerTarget();
|
||||
if (attemptedChannelCredential?.channelId === channelId && attemptedChannelCredential.target === target) {
|
||||
void removeStoredChannelPassword(target, channelId);
|
||||
attemptedChannelCredential = null;
|
||||
}
|
||||
channelPasswordDialog.open = true;
|
||||
channelPasswordDialog.channelId = channelId;
|
||||
channelPasswordDialog.password = "";
|
||||
channelPasswordDialog.error = t("channelPasswordRetry");
|
||||
channelPasswordDialog.submitting = false;
|
||||
clearError();
|
||||
void nextTick(() => document.getElementById("channel-password-input")?.focus());
|
||||
return;
|
||||
}
|
||||
if (code !== "CHANNEL_PASSWORD_REQUIRED" || !selectedChannelId.value) return;
|
||||
channelPasswordDialog.open = true;
|
||||
channelPasswordDialog.channelId = selectedChannelId.value;
|
||||
channelPasswordDialog.password = "";
|
||||
channelPasswordDialog.error = t("channelPasswordRetry");
|
||||
channelPasswordDialog.submitting = false;
|
||||
clearError();
|
||||
void nextTick(() => document.getElementById("channel-password-input")?.focus());
|
||||
if (code && channelPasswordDialog.open && channelPasswordDialog.submitting) {
|
||||
channelPasswordDialog.error = errorMessage.value;
|
||||
channelPasswordDialog.submitting = false;
|
||||
attemptedChannelCredential = null;
|
||||
clearError();
|
||||
}
|
||||
});
|
||||
watch(errorCode, (code) => {
|
||||
if (code !== "SERVER_PASSWORD_REQUIRED" && code !== "INVALID_SERVER_PASSWORD") return;
|
||||
if (attemptedServerCredential) void removeStoredServerPassword(attemptedServerCredential.target);
|
||||
attemptedServerCredential = null;
|
||||
serverPassword.value = "";
|
||||
serverPasswordDialog.open = true;
|
||||
serverPasswordDialog.password = "";
|
||||
serverPasswordDialog.errorCode = code;
|
||||
clearError();
|
||||
void nextTick(() => document.getElementById("retry-server-password-input")?.focus());
|
||||
});
|
||||
watch(isConnected, (connected) => {
|
||||
if (!connected || !attemptedServerCredential) return;
|
||||
const credential = attemptedServerCredential;
|
||||
attemptedServerCredential = null;
|
||||
if (credential.password) void saveStoredServerPassword(credential.target, credential.password);
|
||||
});
|
||||
watch(channelSwitchedChannelId, (channelId) => {
|
||||
if (!channelPasswordDialog.open || !channelId || channelId !== channelPasswordDialog.channelId) return;
|
||||
channelPasswordDialog.open = false;
|
||||
channelPasswordDialog.channelId = "";
|
||||
channelPasswordDialog.password = "";
|
||||
channelPasswordDialog.error = "";
|
||||
channelPasswordDialog.submitting = false;
|
||||
if (!channelId) return;
|
||||
const credential = attemptedChannelCredential;
|
||||
if (credential?.channelId === channelId && credential.target === currentServerTarget()) {
|
||||
attemptedChannelCredential = null;
|
||||
void saveStoredChannelPassword(credential.target, channelId, credential.password);
|
||||
}
|
||||
if (channelPasswordDialog.open && channelId === channelPasswordDialog.channelId) {
|
||||
channelPasswordDialog.open = false;
|
||||
channelPasswordDialog.channelId = "";
|
||||
channelPasswordDialog.password = "";
|
||||
channelPasswordDialog.error = "";
|
||||
channelPasswordDialog.submitting = false;
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user