Fix login race condition: don't call checkAuth after successful login

This commit is contained in:
CanbiZ
2025-11-28 13:34:51 +01:00
parent bef5bef875
commit c5db169441
2 changed files with 15 additions and 11 deletions

View File

@@ -97,7 +97,6 @@ export function AuthProvider({ children }: AuthProviderProps) {
const checkAuth = useCallback(() => {
return checkAuthInternal(0);
}, []);
const login = async (
@@ -115,17 +114,18 @@ export function AuthProvider({ children }: AuthProviderProps) {
});
if (response.ok) {
const data = (await response.json()) as { username: string };
const data = (await response.json()) as {
username: string;
expirationTime?: number;
};
setIsAuthenticated(true);
setUsername(data.username);
// Check auth again to get expiration time
// Add a small delay to ensure the httpOnly cookie is available
await new Promise<void>((resolve) => {
setTimeout(() => {
void checkAuth().then(() => resolve());
}, 150);
});
// Set expiration time from login response if available
if (data.expirationTime) {
setExpirationTime(data.expirationTime);
}
// Don't call checkAuth after login - we already know we're authenticated
// The cookie is set by the server response
return true;
} else {
const errorData = await response.json();

View File

@@ -41,10 +41,14 @@ export async function POST(request: NextRequest) {
const sessionDurationDays = authConfig.sessionDurationDays;
const token = generateToken(username, sessionDurationDays);
// Calculate expiration time for client
const expirationTime = Date.now() + (sessionDurationDays * 24 * 60 * 60 * 1000);
const response = NextResponse.json({
success: true,
message: 'Login successful',
username
username,
expirationTime
});
// Determine if request is over HTTPS