refactor: simplify setup flow — eliminate redundant DB calls and login round-trips
- Add AdminExists() to UserRepository (EXISTS query, not full table scan) - SetupInitialAdmin returns tokens directly (skip separate Login call) - Add SetupRequest DTO to auth_dto.go (replace inline struct) - Extract defaultEmail() helper (removes duplicated email logic) - AuthPage uses setup tokens directly (skip redundant apiLogin call) - Use customAxiosInstance for auth API calls (consistent with codebase)
This commit is contained in:
@ -220,9 +220,9 @@ export const login = postAuthLogin;
|
||||
export const register = postAuthRegister;
|
||||
export const refreshAuth = postAuthRefresh;
|
||||
export const fetchAuthStatus = () =>
|
||||
AXIOS_INSTANCE.get<{ needsSetup: boolean; hasUsers: boolean }>("/auth/status").then((r) => r.data);
|
||||
customAxiosInstance<{ needsSetup: boolean; hasUsers: boolean }>({ url: "/auth/status", method: "GET" });
|
||||
export const setupInitialAdmin = (data: { username: string; password: string; email?: string }) =>
|
||||
AXIOS_INSTANCE.post<{ accessToken: string; refreshToken: string }>("/auth/setup", data).then((r) => r.data);
|
||||
customAxiosInstance<{ accessToken: string; refreshToken: string }>({ url: "/auth/setup", method: "POST", data });
|
||||
export const listUsers = () => customAxiosInstance<UserResponse[]>({ url: "/users", method: "GET" });
|
||||
export const createUser = (data: AdminCreateUserRequest) =>
|
||||
customAxiosInstance<UserResponse>({ url: "/users", method: "POST", data });
|
||||
|
||||
@ -59,20 +59,20 @@ const AuthPage: React.FC<Props> = ({ onLogin }) => {
|
||||
toastInfo("Creating admin account...", { title: "Setup", durationMs: 1200 });
|
||||
|
||||
try {
|
||||
await setupInitialAdmin({
|
||||
const result = await setupInitialAdmin({
|
||||
username: setupUsername,
|
||||
password: setupPassword,
|
||||
email: setupEmail || undefined,
|
||||
});
|
||||
|
||||
// Login with the returned tokens
|
||||
const loginResponse = await apiLogin({
|
||||
// setupInitialAdmin returns tokens — use them directly to avoid redundant login
|
||||
onLogin({
|
||||
accessToken: result.accessToken,
|
||||
refreshToken: result.refreshToken,
|
||||
username: setupUsername,
|
||||
password: setupPassword,
|
||||
});
|
||||
} as any);
|
||||
|
||||
toastSuccess("Admin account created. Welcome!");
|
||||
onLogin(loginResponse);
|
||||
navigate("/home", { replace: true });
|
||||
} catch (err: unknown) {
|
||||
const msg = getErrorMessage(err, "Setup failed. Please try again later.");
|
||||
|
||||
Reference in New Issue
Block a user