refactor: remove login/auth for public dashboard
Deploy to VPS / deploy (push) Failing after 34s
Deploy to VPS / deploy (push) Failing after 34s
- Remove login page, redirect / to /dashboard directly - Remove AuthProvider, DashboardGuard from dashboard layout - Remove use-auth hook and auth API module - Remove X-Admin-Password header from API client - Remove logout button from sidebar
This commit is contained in:
@@ -1,13 +0,0 @@
|
||||
import { ApiError, api } from "./client";
|
||||
|
||||
export async function login(password: string): Promise<boolean> {
|
||||
try {
|
||||
const resp = await api.post<{ ok: boolean }>("/api/auth/login", {
|
||||
password,
|
||||
});
|
||||
return resp.ok;
|
||||
} catch (err) {
|
||||
if (err instanceof ApiError) return false;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
@@ -12,27 +12,17 @@ function getBaseUrl(): string {
|
||||
if (typeof window === "undefined") return "";
|
||||
const protocol = window.location.protocol.replace(":", "");
|
||||
const host = window.location.host;
|
||||
// In dev, Next.js proxy can be configured, but default to same-host assumption
|
||||
return `${protocol}://${host}`;
|
||||
}
|
||||
|
||||
function getAuthHeader(): string | null {
|
||||
if (typeof window === "undefined") return null;
|
||||
return localStorage.getItem("admin-password");
|
||||
}
|
||||
|
||||
export async function apiRequest<T>(
|
||||
method: string,
|
||||
path: string,
|
||||
body?: unknown,
|
||||
): Promise<T> {
|
||||
const url = `${getBaseUrl()}${path}`;
|
||||
const password = getAuthHeader();
|
||||
|
||||
const headers: Record<string, string> = {};
|
||||
if (password) {
|
||||
headers["X-Admin-Password"] = password;
|
||||
}
|
||||
if (body !== undefined) {
|
||||
headers["Content-Type"] = "application/json";
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
export { login } from "./auth";
|
||||
export { ApiError, api, apiRequest } from "./client";
|
||||
export { configApi } from "./config";
|
||||
export { dashboardApi } from "./dashboard";
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import {
|
||||
createContext,
|
||||
type ReactNode,
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
useState,
|
||||
} from "react";
|
||||
import { login } from "@/lib/api";
|
||||
|
||||
interface AuthContextValue {
|
||||
authenticated: boolean;
|
||||
loading: boolean;
|
||||
login: (password: string) => Promise<boolean>;
|
||||
logout: () => void;
|
||||
}
|
||||
|
||||
const AuthContext = createContext<AuthContextValue | null>(null);
|
||||
|
||||
export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
const [authenticated, setAuthenticated] = useState(false);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
const password = localStorage.getItem("admin-password");
|
||||
if (password) {
|
||||
// Verify stored password still works
|
||||
login(password)
|
||||
.then((ok) => {
|
||||
setAuthenticated(ok);
|
||||
setLoading(false);
|
||||
})
|
||||
.catch(() => {
|
||||
localStorage.removeItem("admin-password");
|
||||
setLoading(false);
|
||||
});
|
||||
} else {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const handleLogin = useCallback(async (password: string) => {
|
||||
const ok = await login(password);
|
||||
if (ok) {
|
||||
localStorage.setItem("admin-password", password);
|
||||
setAuthenticated(true);
|
||||
}
|
||||
return ok;
|
||||
}, []);
|
||||
|
||||
const handleLogout = useCallback(() => {
|
||||
localStorage.removeItem("admin-password");
|
||||
setAuthenticated(false);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<AuthContext.Provider
|
||||
value={{
|
||||
authenticated,
|
||||
loading,
|
||||
login: handleLogin,
|
||||
logout: handleLogout,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useAuth(): AuthContextValue {
|
||||
const ctx = useContext(AuthContext);
|
||||
if (!ctx) {
|
||||
throw new Error("useAuth must be used within an AuthProvider");
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
Reference in New Issue
Block a user