fix: auth disabled routing and UI

This commit is contained in:
Leon
2025-07-19 10:42:11 +02:00
parent d267c7271b
commit ab45139e7e
6 changed files with 111 additions and 24 deletions

View File

@@ -6,6 +6,7 @@ import { useRouter } from "next/navigation"
interface AuthContextType {
isAuthenticated: boolean
isAuthEnabled: boolean
login: (username: string, password: string) => Promise<void>
logout: () => void
isLoading: boolean
@@ -15,6 +16,7 @@ export const AuthContext = createContext<AuthContextType | undefined>(undefined)
export const AuthProvider = ({ children }: { children: ReactNode }) => {
const [isAuthenticated, setIsAuthenticated] = useState(false)
const [isAuthEnabled, setIsAuthEnabled] = useState(false)
const [isLoading, setIsLoading] = useState(true)
const router = useRouter()
@@ -23,6 +25,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
setIsLoading(true);
try {
const { auth_enabled } = await getAuthStatus();
setIsAuthEnabled(auth_enabled);
if (!auth_enabled) {
setIsAuthenticated(true);
} else {
@@ -67,7 +70,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
}
return (
<AuthContext.Provider value={{ isAuthenticated, login, logout, isLoading }}>
<AuthContext.Provider value={{ isAuthenticated, isAuthEnabled, login, logout, isLoading }}>
{children}
</AuthContext.Provider>
)

View File

@@ -28,21 +28,27 @@ describe("AuthContext", () => {
consoleError.mockRestore()
})
it("authenticates if auth is not enabled", async () => {
it("authenticates and sets auth enabled to false if auth is not enabled on server", async () => {
mockedApi.getAuthStatus.mockResolvedValue({ auth_enabled: false })
render(
<AuthProvider>
<AuthContext.Consumer>
{(value) => (
<span>
Is Authenticated: {value?.isAuthenticated.toString()}
</span>
<div>
<span>
Is Authenticated: {value?.isAuthenticated.toString()}
</span>
<span>
Is Auth Enabled: {value?.isAuthEnabled.toString()}
</span>
</div>
)}
</AuthContext.Consumer>
</AuthProvider>
)
await waitFor(() => {
expect(screen.getByText("Is Authenticated: true")).toBeInTheDocument()
expect(screen.getByText("Is Auth Enabled: false")).toBeInTheDocument()
})
})
@@ -54,15 +60,21 @@ describe("AuthContext", () => {
<AuthProvider>
<AuthContext.Consumer>
{(value) => (
<span>
Is Authenticated: {value?.isAuthenticated.toString()}
</span>
<div>
<span>
Is Authenticated: {value?.isAuthenticated.toString()}
</span>
<span>
Is Auth Enabled: {value?.isAuthEnabled.toString()}
</span>
</div>
)}
</AuthContext.Consumer>
</AuthProvider>
)
await waitFor(() => {
expect(screen.getByText("Is Authenticated: true")).toBeInTheDocument()
expect(screen.getByText("Is Auth Enabled: true")).toBeInTheDocument()
})
})