Extend and fix session management, /me API
This commit is contained in:
@@ -1 +1,59 @@
|
||||
export default () => null;
|
||||
import { Schema as S } from "@effect/schema";
|
||||
import { Me } from "common/api";
|
||||
import { Effect, Fiber, Option as O, Option, pipe } from "effect";
|
||||
import { useLocation } from "preact-iso";
|
||||
import { useEffect } from "preact/hooks";
|
||||
import { useStore } from "../store";
|
||||
import * as style from "./Home.css";
|
||||
|
||||
export const Home = () => {
|
||||
|
||||
const { route } = useLocation();
|
||||
|
||||
const user = Option.getOrNull(useStore(state => state.user));
|
||||
const setUser = useStore(state => state.setUser);
|
||||
|
||||
useEffect(() => {
|
||||
if (user !== null) return;
|
||||
|
||||
const effect = Effect.gen(function* () {
|
||||
const res = yield* Effect.promise((signal) => fetch("http://localhost:3000/me", {
|
||||
method: "GET",
|
||||
signal,
|
||||
credentials: "include",
|
||||
}));
|
||||
|
||||
if (!res.ok) {
|
||||
route("/login");
|
||||
return;
|
||||
}
|
||||
|
||||
const responseData = yield* pipe(
|
||||
Effect.promise(() => res.json()),
|
||||
Effect.flatMap(S.decodeUnknown(Me.props.response[200].schema)),
|
||||
Effect.orDie,
|
||||
);
|
||||
|
||||
setUser(O.some(responseData));
|
||||
});
|
||||
|
||||
const fiber = Effect.runFork(effect);
|
||||
return () => Effect.runFork(Fiber.interrupt(fiber), { immediate: true });
|
||||
}, []);
|
||||
|
||||
if (user === null) {
|
||||
return (
|
||||
<div class={style.container}>
|
||||
<div class={style.loading}>Ładowanie…</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div class={style.container}>
|
||||
<div class={style.content}>
|
||||
Użytkownik: {user.username}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user