Nasty hack for redirect on login/logout
This commit is contained in:
parent
7af220de2e
commit
50769ed32f
|
@ -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 { deleteCookie, getCookies } from "$std/http/cookie.ts";
|
||||||
import { getUserFromNonExpiredLoginToken } from "@/db/mod.ts";
|
import { getUserFromNonExpiredLoginToken } from "@/db/mod.ts";
|
||||||
import { type ContextState, type PublicUser, type User } from "@/types.ts";
|
import { type ContextState, type PublicUser, type User } from "@/types.ts";
|
||||||
|
@ -14,22 +14,10 @@ function toPublicUser(user: User): PublicUser {
|
||||||
return 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(
|
async function currentUser(
|
||||||
request: Request,
|
request: Request,
|
||||||
context: MiddlewareHandlerContext<ContextState>,
|
context: MiddlewareHandlerContext<ContextState>,
|
||||||
) {
|
) {
|
||||||
context.layout = Layout;
|
|
||||||
let hasBadAuthCookie = false;
|
let hasBadAuthCookie = false;
|
||||||
const { lsauth } = getCookies(request.headers);
|
const { lsauth } = getCookies(request.headers);
|
||||||
log.debug("lsauth cookie:", lsauth);
|
log.debug("lsauth cookie:", lsauth);
|
||||||
|
|
|
@ -41,9 +41,21 @@ export const handler: Handlers<UserID | LoginError | null> = {
|
||||||
if (!token || !token.bytes) throw "failed to create token";
|
if (!token || !token.bytes) throw "failed to create token";
|
||||||
const cookie = base64.encode(token.bytes);
|
const cookie = base64.encode(token.bytes);
|
||||||
|
|
||||||
|
const newUrl = new URL(request.url);
|
||||||
|
newUrl.pathname = "/dashboard";
|
||||||
const response = await context.render(user.id);
|
const response = await context.render(user.id);
|
||||||
setCookie(response.headers, { name: "lsauth", value: cookie });
|
const headers = new Headers(response.headers);
|
||||||
return response;
|
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;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -10,9 +10,22 @@ export const handler: Handlers<unknown> = {
|
||||||
if (lsauth) {
|
if (lsauth) {
|
||||||
console.log("deleteToken:", await deleteToken(lsauth));
|
console.log("deleteToken:", await deleteToken(lsauth));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const newUrl = new URL(request.url);
|
||||||
|
newUrl.pathname = "/login";
|
||||||
const response = await context.render();
|
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;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue