1
0

Replace unauthorized status with a redirect

This commit is contained in:
Zef Hemel 2023-07-04 16:53:39 +02:00
parent 5ff9a5692b
commit f39ab26cea
2 changed files with 10 additions and 13 deletions

View File

@ -21,13 +21,13 @@ export class HttpSpacePrimitives implements SpacePrimitives {
options.headers = { ...options.headers, ...{ "X-Sync-Mode": "true" } };
}
const result = await fetch(url, { ...options });
if (
this.getRealStatus(result) === 401
) {
// Invalid credentials, reloading the browser should trigger authentication
console.log("Going to redirect after", url);
location.href = "/.auth?refer=" + location.pathname;
const result = await fetch(url, {
...options,
});
if (result.redirected) {
// Got a redirect, we'll assume this is due to invalid credentials and redirecting to an auth page
console.log("Got a redirect via the API so will redirect to URL", url);
location.href = result.url;
throw new Error("Invalid credentials");
}
return result;

View File

@ -218,8 +218,7 @@ export class HttpServer {
}
return;
} else {
response.status = 401;
response.body = "Unauthorized";
response.redirect("/.auth");
return;
}
} else {
@ -232,8 +231,7 @@ export class HttpServer {
if (!excludedPaths.includes(request.url.pathname)) {
const authCookie = await cookies.get("auth");
if (!authCookie) {
response.status = 401;
response.body = "Unauthorized, please authenticate";
response.redirect("/.auth");
return;
}
const [username, hashedPassword] = authCookie.split(":");
@ -243,8 +241,7 @@ export class HttpServer {
hashedPassword,
)
) {
response.status = 401;
response.body = "Invalid username/password, please reauthenticate";
response.redirect("/.auth");
return;
}
}