mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
Add single-user authentication system with Better Auth integration (#76)
* Add Better Auth integration with session management - Implement user authentication using Better Auth library - Add login, register, and sign out functionality - Support passkey/WebAuthn authentication - Create database schema for users, sessions, accounts, passkeys - Add AuthProvider context and ProtectedRoute components - Add account settings page in settings dialog - Protect all API routes with authentication middleware - Update environment configuration for auth providers * Add single-user authentication system with admin setup - Add first-run Setup page for initial admin account creation - Add environment-based admin configuration (ADMIN_EMAIL, ADMIN_PASSWORD) - Add password reset capability via ADMIN_PASSWORD_RESET environment flag - Migrate to data routers with auth loaders, remove ProtectedRoute - Add change password functionality to account settings - Enforce AUTH_SECRET requirement in production - Auto-disable registration when admin is pre-configured via environment * Track busy/idle session status across hooks * Bump version to 0.7.5 * Update pnpm to 10.28.1
This commit is contained in:
1 parent
af292758c3
commit
81a66d2ddb
29 files changed
+2324
-149
No files matched your search
@@ -0,0 +1,10 @@
|
||||
import { useContext } from 'react'
|
||||
import { AuthContext } from '@/contexts/AuthContext'
|
||||
|
||||
export function useAuth() {
|
||||
const context = useContext(AuthContext)
|
||||
if (!context) {
|
||||
throw new Error('useAuth must be used within an AuthProvider')
|
||||
}
|
||||
return context
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import type {
|
||||
import type { paths, components } from "../api/opencode-types";
|
||||
import { parseNetworkError } from "../lib/opencode-errors";
|
||||
import { showToast } from "../lib/toast";
|
||||
import { useSessionStatus } from "../stores/sessionStatusStore";
|
||||
|
||||
const titleGeneratingSessionsState = new Set<string>();
|
||||
const titleGeneratingListeners = new Set<() => void>();
|
||||
@@ -209,6 +210,7 @@ export const useSendPrompt = (opcodeUrl: string | null | undefined, directory?:
|
||||
const client = useOpenCodeClient(opcodeUrl, directory);
|
||||
const queryClient = useQueryClient();
|
||||
const hasInitializedRef = useRef<Set<string>>(new Set());
|
||||
const setSessionStatus = useSessionStatus((state) => state.setStatus);
|
||||
|
||||
const generateSessionTitle = async (sessionID: string, userPromptText: string) => {
|
||||
if (!client || hasInitializedRef.current.has(sessionID)) return;
|
||||
@@ -265,6 +267,8 @@ export const useSendPrompt = (opcodeUrl: string | null | undefined, directory?:
|
||||
}) => {
|
||||
if (!client) throw new Error("No client available");
|
||||
|
||||
setSessionStatus(sessionID, { type: "busy" });
|
||||
|
||||
const optimisticUserID = `optimistic_user_${Date.now()}_${Math.random()}`;
|
||||
|
||||
const contentParts = parts || [{ type: "text" as const, content: prompt || "", name: "" }];
|
||||
@@ -326,6 +330,7 @@ export const useSendPrompt = (opcodeUrl: string | null | undefined, directory?:
|
||||
},
|
||||
onError: (error, variables) => {
|
||||
const { sessionID } = variables;
|
||||
setSessionStatus(sessionID, { type: "idle" });
|
||||
queryClient.setQueryData<MessageListResponse>(
|
||||
["opencode", "messages", opcodeUrl, sessionID, directory],
|
||||
(old) => old?.filter((msg) => !msg.info.id.startsWith("optimistic_")),
|
||||
@@ -483,6 +488,7 @@ export const useAbortSession = (
|
||||
export const useSendShell = (opcodeUrl: string | null | undefined, directory?: string) => {
|
||||
const client = useOpenCodeClient(opcodeUrl, directory);
|
||||
const queryClient = useQueryClient();
|
||||
const setSessionStatus = useSessionStatus((state) => state.setStatus);
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({
|
||||
@@ -496,6 +502,8 @@ export const useSendShell = (opcodeUrl: string | null | undefined, directory?: s
|
||||
}) => {
|
||||
if (!client) throw new Error("No client available");
|
||||
|
||||
setSessionStatus(sessionID, { type: "busy" });
|
||||
|
||||
const optimisticUserID = `optimistic_user_${Date.now()}_${Math.random()}`;
|
||||
|
||||
const userMessage = createOptimisticUserMessage(
|
||||
@@ -517,6 +525,7 @@ export const useSendShell = (opcodeUrl: string | null | undefined, directory?: s
|
||||
},
|
||||
onError: (_, variables) => {
|
||||
const { sessionID } = variables;
|
||||
setSessionStatus(sessionID, { type: "idle" });
|
||||
queryClient.setQueryData<MessageListResponse>(
|
||||
["opencode", "messages", opcodeUrl, sessionID, directory],
|
||||
(old) => old?.filter((msg) => !msg.info.id.startsWith("optimistic_")),
|
||||
|
||||
@@ -2,6 +2,7 @@ import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { createOpenCodeClient } from '@/api/opencode'
|
||||
import { showToast } from '@/lib/toast'
|
||||
import type { MessageWithParts, MessageListResponse } from '@/api/types'
|
||||
import { useSessionStatus } from '@/stores/sessionStatusStore'
|
||||
|
||||
interface UseRemoveMessageOptions {
|
||||
opcodeUrl: string | null
|
||||
@@ -68,6 +69,7 @@ interface UseRefreshMessageOptions {
|
||||
export function useRefreshMessage({ opcodeUrl, sessionId, directory }: UseRefreshMessageOptions) {
|
||||
const queryClient = useQueryClient()
|
||||
const removeMessage = useRemoveMessage({ opcodeUrl, sessionId, directory })
|
||||
const setSessionStatus = useSessionStatus((state) => state.setStatus)
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({
|
||||
@@ -83,6 +85,8 @@ export function useRefreshMessage({ opcodeUrl, sessionId, directory }: UseRefres
|
||||
}) => {
|
||||
if (!opcodeUrl) throw new Error('OpenCode URL not available')
|
||||
|
||||
setSessionStatus(sessionId, { type: 'busy' })
|
||||
|
||||
await removeMessage.mutateAsync({ messageID: assistantMessageID })
|
||||
|
||||
const client = createOpenCodeClient(opcodeUrl, directory)
|
||||
@@ -142,8 +146,9 @@ export function useRefreshMessage({ opcodeUrl, sessionId, directory }: UseRefres
|
||||
queryKey: ['opencode', 'session', opcodeUrl, sessionId, directory]
|
||||
})
|
||||
},
|
||||
onError: (error, variables) => {
|
||||
onError: (_, variables) => {
|
||||
void variables
|
||||
setSessionStatus(sessionId, { type: 'idle' })
|
||||
queryClient.setQueryData<MessageListResponse>(
|
||||
['opencode', 'messages', opcodeUrl, sessionId, directory],
|
||||
(old) => {
|
||||
@@ -155,7 +160,6 @@ export function useRefreshMessage({ opcodeUrl, sessionId, directory }: UseRefres
|
||||
return messages
|
||||
}
|
||||
)
|
||||
console.error('Failed to refresh message:', error)
|
||||
showToast.error('Failed to refresh message')
|
||||
}
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user