feat: implement admin authentication overlay and API integration for secure access to voice and media controls
This commit is contained in:
@@ -5,6 +5,7 @@ import { MessagesPanel } from "./components/messages/MessagesPanel";
|
|||||||
import { ReviewPanel } from "./components/review/ReviewPanel";
|
import { ReviewPanel } from "./components/review/ReviewPanel";
|
||||||
import { Tabs, TabsContent } from "./components/ui/tabs";
|
import { Tabs, TabsContent } from "./components/ui/tabs";
|
||||||
import { VoicePanel } from "./components/voice/VoicePanel";
|
import { VoicePanel } from "./components/voice/VoicePanel";
|
||||||
|
import { AuthOverlay } from "./components/layout/AuthOverlay";
|
||||||
import { useDashboardSocket } from "./hooks/useDashboardSocket";
|
import { useDashboardSocket } from "./hooks/useDashboardSocket";
|
||||||
import { mergeMessages, useMessages } from "./hooks/useMessages";
|
import { mergeMessages, useMessages } from "./hooks/useMessages";
|
||||||
import { useMediaControl } from "./hooks/useMediaControl";
|
import { useMediaControl } from "./hooks/useMediaControl";
|
||||||
@@ -26,6 +27,7 @@ export default function App() {
|
|||||||
const [levels, setLevels] = useState<number[]>(Array.from({ length: 32 }, () => 0.04));
|
const [levels, setLevels] = useState<number[]>(Array.from({ length: 32 }, () => 0.04));
|
||||||
const [isListening, setIsListening] = useState(false);
|
const [isListening, setIsListening] = useState(false);
|
||||||
const [isStreaming, setIsStreaming] = useState(false);
|
const [isStreaming, setIsStreaming] = useState(false);
|
||||||
|
const [isAuthenticated, setIsAuthenticated] = useState(!!localStorage.getItem("admin-password"));
|
||||||
const audioContextListenRef = useRef<AudioContext | null>(null);
|
const audioContextListenRef = useRef<AudioContext | null>(null);
|
||||||
const audioContextTransmitRef = useRef<AudioContext | null>(null);
|
const audioContextTransmitRef = useRef<AudioContext | null>(null);
|
||||||
const streamRef = useRef<MediaStream | null>(null);
|
const streamRef = useRef<MediaStream | null>(null);
|
||||||
@@ -198,34 +200,42 @@ export default function App() {
|
|||||||
</div>
|
</div>
|
||||||
<Tabs value={activeTab} onValueChange={(value) => patchUIState({ activeTab: value as DashboardTab })}>
|
<Tabs value={activeTab} onValueChange={(value) => patchUIState({ activeTab: value as DashboardTab })}>
|
||||||
<TabsContent value="voice">
|
<TabsContent value="voice">
|
||||||
<VoicePanel
|
{!isAuthenticated ? (
|
||||||
guilds={voice.guilds}
|
<AuthOverlay onAuthenticated={() => setIsAuthenticated(true)} />
|
||||||
channels={voice.voiceChannels}
|
) : (
|
||||||
selectedGuild={selectedVoiceGuild}
|
<VoicePanel
|
||||||
selectedChannel={selectedVoiceChannel}
|
guilds={voice.guilds}
|
||||||
status={voice.voiceStatus}
|
channels={voice.voiceChannels}
|
||||||
loading={voice.loading}
|
selectedGuild={selectedVoiceGuild}
|
||||||
activeSpeakers={activeSpeakers}
|
selectedChannel={selectedVoiceChannel}
|
||||||
levels={levels}
|
status={voice.voiceStatus}
|
||||||
isListening={isListening}
|
loading={voice.loading}
|
||||||
isStreaming={isStreaming}
|
activeSpeakers={activeSpeakers}
|
||||||
onGuildChange={(guildId) => patchUIState({ selectedVoiceGuild: guildId, selectedVoiceChannel: "" })}
|
levels={levels}
|
||||||
onChannelChange={(channelId) => patchUIState({ selectedVoiceChannel: channelId })}
|
isListening={isListening}
|
||||||
onJoin={() => voice.joinVoice(selectedVoiceGuild, selectedVoiceChannel)}
|
isStreaming={isStreaming}
|
||||||
onDisconnect={() => voice.leaveVoice()}
|
onGuildChange={(guildId) => patchUIState({ selectedVoiceGuild: guildId, selectedVoiceChannel: "" })}
|
||||||
onListenToggle={toggleListening}
|
onChannelChange={(channelId) => patchUIState({ selectedVoiceChannel: channelId })}
|
||||||
onStreamingToggle={toggleStreaming}
|
onJoin={() => voice.joinVoice(selectedVoiceGuild, selectedVoiceChannel)}
|
||||||
/>
|
onDisconnect={() => voice.leaveVoice()}
|
||||||
|
onListenToggle={toggleListening}
|
||||||
|
onStreamingToggle={toggleStreaming}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</TabsContent>
|
</TabsContent>
|
||||||
<TabsContent value="media">
|
<TabsContent value="media">
|
||||||
<MediaPanel
|
{!isAuthenticated ? (
|
||||||
state={media.mediaState}
|
<AuthOverlay onAuthenticated={() => setIsAuthenticated(true)} />
|
||||||
loading={media.loading}
|
) : (
|
||||||
onQueueMusic={(source) => media.enqueue(source, "music")}
|
<MediaPanel
|
||||||
onStartScreen={(source) => media.enqueue(source, "screen")}
|
state={media.mediaState}
|
||||||
onSkip={media.skip}
|
loading={media.loading}
|
||||||
onStop={media.stop}
|
onQueueMusic={(source) => media.enqueue(source, "music")}
|
||||||
/>
|
onStartScreen={(source) => media.enqueue(source, "screen")}
|
||||||
|
onSkip={media.skip}
|
||||||
|
onStop={media.stop}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</TabsContent>
|
</TabsContent>
|
||||||
<TabsContent value="messages">
|
<TabsContent value="messages">
|
||||||
<MessagesPanel
|
<MessagesPanel
|
||||||
|
|||||||
8
frontend/src/api/auth.ts
Normal file
8
frontend/src/api/auth.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import { request } from "./client";
|
||||||
|
|
||||||
|
export async function login(password: string): Promise<{ ok: boolean }> {
|
||||||
|
return request<{ ok: boolean }>('/api/auth/login', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ password }),
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -50,8 +50,12 @@ class ApiError extends Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
export async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
||||||
|
const password = localStorage.getItem("admin-password");
|
||||||
const res = await fetch(path, {
|
const res = await fetch(path, {
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
...(password ? { "X-Admin-Password": password } : {}),
|
||||||
|
},
|
||||||
...init,
|
...init,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
62
frontend/src/components/layout/AuthOverlay.tsx
Normal file
62
frontend/src/components/layout/AuthOverlay.tsx
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
import { useState } from "react";
|
||||||
|
import { login } from "../../api/auth";
|
||||||
|
import { Button } from "../ui/button";
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "../ui/card";
|
||||||
|
import { Input } from "../ui/input";
|
||||||
|
import { Lock } from "lucide-react";
|
||||||
|
|
||||||
|
interface AuthOverlayProps {
|
||||||
|
onAuthenticated: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function AuthOverlay({ onAuthenticated }: AuthOverlayProps) {
|
||||||
|
const [password, setPassword] = useState("");
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
setLoading(true);
|
||||||
|
setError(null);
|
||||||
|
try {
|
||||||
|
await login(password);
|
||||||
|
localStorage.setItem("admin-password", password);
|
||||||
|
onAuthenticated();
|
||||||
|
} catch (err) {
|
||||||
|
setError("Invalid password");
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex items-center justify-center p-4">
|
||||||
|
<Card className="w-full max-w-md">
|
||||||
|
<CardHeader className="text-center">
|
||||||
|
<div className="mx-auto mb-4 flex h-12 w-12 items-center justify-center rounded-full bg-primary/10 text-primary">
|
||||||
|
<Lock className="h-6 w-6" />
|
||||||
|
</div>
|
||||||
|
<CardTitle>Admin Access Required</CardTitle>
|
||||||
|
<CardDescription>Enter the admin password to access Voice and Media controls.</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<form onSubmit={handleSubmit} className="space-y-4">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Input
|
||||||
|
type="password"
|
||||||
|
placeholder="Enter password"
|
||||||
|
value={password}
|
||||||
|
onChange={(e) => setPassword(e.target.value)}
|
||||||
|
autoFocus
|
||||||
|
/>
|
||||||
|
{error && <p className="text-xs text-destructive">{error}</p>}
|
||||||
|
</div>
|
||||||
|
<Button type="submit" className="w-full" disabled={loading || !password}>
|
||||||
|
{loading ? "Authenticating..." : "Unlock Controls"}
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -76,6 +76,7 @@ const configSchema = z
|
|||||||
POSTGRES_DB: z.string().optional(),
|
POSTGRES_DB: z.string().optional(),
|
||||||
POSTGRES_POOL_MIN: z.coerce.number().int().positive().default(2),
|
POSTGRES_POOL_MIN: z.coerce.number().int().positive().default(2),
|
||||||
POSTGRES_POOL_MAX: z.coerce.number().int().positive().default(10),
|
POSTGRES_POOL_MAX: z.coerce.number().int().positive().default(10),
|
||||||
|
ADMIN_PASSWORD: z.string().default("admin123"),
|
||||||
})
|
})
|
||||||
.superRefine((value, ctx) => {
|
.superRefine((value, ctx) => {
|
||||||
if (!value.AI_ANALYSIS_ENABLED) {
|
if (!value.AI_ANALYSIS_ENABLED) {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { fileURLToPath } from "node:url";
|
|||||||
import { Streamer } from "@dank074/discord-video-stream";
|
import { Streamer } from "@dank074/discord-video-stream";
|
||||||
import { AudioPlayerStatus } from "@discordjs/voice";
|
import { AudioPlayerStatus } from "@discordjs/voice";
|
||||||
import type { Client } from "discord.js-selfbot-v13";
|
import type { Client } from "discord.js-selfbot-v13";
|
||||||
|
import { config } from "./config";
|
||||||
import express, {
|
import express, {
|
||||||
type NextFunction,
|
type NextFunction,
|
||||||
type Request,
|
type Request,
|
||||||
@@ -257,6 +258,25 @@ export async function startWebserver(
|
|||||||
res.send(await getMetrics());
|
res.send(await getMetrics());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Simple password-based auth
|
||||||
|
app.post("/api/auth/login", (req: Request, res: Response) => {
|
||||||
|
const { password } = req.body;
|
||||||
|
if (password === config.ADMIN_PASSWORD) {
|
||||||
|
res.json({ ok: true });
|
||||||
|
} else {
|
||||||
|
res.status(401).json({ error: "Invalid password" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const adminAuth = (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
const authHeader = req.headers["x-admin-password"];
|
||||||
|
if (authHeader === config.ADMIN_PASSWORD) {
|
||||||
|
next();
|
||||||
|
} else {
|
||||||
|
res.status(401).json({ error: "Unauthorized access to admin features" });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Register route modules
|
// Register route modules
|
||||||
app.use(
|
app.use(
|
||||||
"/api",
|
"/api",
|
||||||
@@ -264,6 +284,7 @@ export async function startWebserver(
|
|||||||
);
|
);
|
||||||
app.use(
|
app.use(
|
||||||
"/api",
|
"/api",
|
||||||
|
adminAuth,
|
||||||
createVoiceRoutes({
|
createVoiceRoutes({
|
||||||
voiceController,
|
voiceController,
|
||||||
patchSharedUIState,
|
patchSharedUIState,
|
||||||
@@ -273,7 +294,7 @@ export async function startWebserver(
|
|||||||
app.use("/api", createMessageRoutes());
|
app.use("/api", createMessageRoutes());
|
||||||
app.use("/api", createAnalysisRoutes());
|
app.use("/api", createAnalysisRoutes());
|
||||||
app.use("/api", createSyncRoutes(_client));
|
app.use("/api", createSyncRoutes(_client));
|
||||||
app.use("/api", createMediaRoutes(mediaController));
|
app.use("/api", adminAuth, createMediaRoutes(mediaController));
|
||||||
|
|
||||||
// Inbound: Discord PCM → tagged chunks → browser
|
// Inbound: Discord PCM → tagged chunks → browser
|
||||||
(globalThis as VoiceGlobals).broadcastPcmToWeb = (
|
(globalThis as VoiceGlobals).broadcastPcmToWeb = (
|
||||||
|
|||||||
Reference in New Issue
Block a user