Nasty hack for redirect on login/logout

This commit is contained in:
Daniel Flanagan 2022-10-20 22:33:33 -05:00
parent 7af220de2e
commit 50769ed32f
Signed by untrusted user: lytedev-divvy
GPG key ID: 6D69CEEE4ABBCD82
3 changed files with 30 additions and 17 deletions

View file

@ -1,4 +1,4 @@
import { LayoutProps, MiddlewareHandlerContext } from "$fresh/server.ts";
import { MiddlewareHandlerContext } from "$fresh/server.ts";
import { deleteCookie, getCookies } from "$std/http/cookie.ts";
import { getUserFromNonExpiredLoginToken } from "@/db/mod.ts";
import { type ContextState, type PublicUser, type User } from "@/types.ts";
@ -14,22 +14,10 @@ function toPublicUser(user: User): PublicUser {
return publicUser;
}
function Layout({ state, Component }: LayoutProps<ContextState>) {
return (
<>
{state.something
? <p>Oh my! You said something!</p>
: <p>You aren't saying anything.</p>}
<Component />
</>
);
}
async function currentUser(
request: Request,
context: MiddlewareHandlerContext<ContextState>,
) {
context.layout = Layout;
let hasBadAuthCookie = false;
const { lsauth } = getCookies(request.headers);
log.debug("lsauth cookie:", lsauth);

View file

@ -41,9 +41,21 @@ export const handler: Handlers<UserID | LoginError | null> = {
if (!token || !token.bytes) throw "failed to create token";
const cookie = base64.encode(token.bytes);
const newUrl = new URL(request.url);
newUrl.pathname = "/dashboard";
const response = await context.render(user.id);
setCookie(response.headers, { name: "lsauth", value: cookie });
return response;
const headers = new Headers(response.headers);
setCookie(headers, { name: "lsauth", value: cookie });
headers.set("location", newUrl.toString());
const actualResponse = new Response(response.body, {
...response,
headers: headers,
status: 302,
});
// response.headers.set("location", newUrl.toString());
// response.status = 302;
return actualResponse;
},
};

View file

@ -10,9 +10,22 @@ export const handler: Handlers<unknown> = {
if (lsauth) {
console.log("deleteToken:", await deleteToken(lsauth));
}
const newUrl = new URL(request.url);
newUrl.pathname = "/login";
const response = await context.render();
deleteCookie(response.headers, "lsauth");
return response;
const headers = new Headers(response.headers);
deleteCookie(headers, "lsauth");
headers.set("location", newUrl.toString());
const actualResponse = new Response(response.body, {
...response,
headers: headers,
status: 302,
});
return actualResponse;
},
};