feat: add effect api
This commit is contained in:
@@ -11,9 +11,9 @@ import {
|
||||
} from "effect";
|
||||
import { FileSystem } from "@effect/platform";
|
||||
import { config } from "./config";
|
||||
import { AuthState } from "./schema";
|
||||
import { authClient } from "@/lib/auth-client";
|
||||
import type { BetterFetchResponse } from "@better-fetch/fetch";
|
||||
import { AuthSchema } from "@money/shared/auth";
|
||||
|
||||
class AuthClientUnknownError extends Data.TaggedError(
|
||||
"AuthClientUnknownError",
|
||||
@@ -129,7 +129,7 @@ const pollToken = ({ device_code }: { device_code: string }) =>
|
||||
const getFromFromDisk = Effect.gen(function* () {
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
const content = yield* fs.readFileString(config.authPath);
|
||||
const auth = yield* Schema.decode(Schema.parseJson(AuthState))(content);
|
||||
const auth = yield* Schema.decode(Schema.parseJson(AuthSchema))(content);
|
||||
if (auth.session.expiresAt < new Date())
|
||||
yield* Effect.fail(new AuthClientExpiredToken());
|
||||
return auth;
|
||||
@@ -160,7 +160,7 @@ const requestAuth = Effect.gen(function* () {
|
||||
);
|
||||
if (sessionData == null) return yield* Effect.fail(new AuthClientNoData());
|
||||
|
||||
const result = yield* Schema.decodeUnknown(AuthState)(sessionData);
|
||||
const result = yield* Schema.decodeUnknown(AuthSchema)(sessionData);
|
||||
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
yield* fs.writeFileString(config.authPath, JSON.stringify(result));
|
||||
|
||||
@@ -5,13 +5,13 @@ import { ZeroProvider } from "@rocicorp/zero/react";
|
||||
import { schema } from "@money/shared";
|
||||
import { useState } from "react";
|
||||
import { AuthClientLayer, getAuth } from "./auth";
|
||||
import { Effect } from "effect";
|
||||
import { Effect, Redacted } from "effect";
|
||||
import { BunContext } from "@effect/platform-bun";
|
||||
import type { AuthData } from "./schema";
|
||||
import { kvStore } from "./store";
|
||||
import { config } from "./config";
|
||||
import { type AuthSchemaType } from "@money/shared/auth";
|
||||
|
||||
function Main({ auth }: { auth: AuthData }) {
|
||||
function Main({ auth }: { auth: AuthSchemaType }) {
|
||||
const [route, setRoute] = useState<Route>("/");
|
||||
|
||||
useKeyboard((key) => {
|
||||
@@ -22,7 +22,7 @@ function Main({ auth }: { auth: AuthData }) {
|
||||
<ZeroProvider
|
||||
{...{
|
||||
userID: auth.user.id,
|
||||
auth: auth.session.token,
|
||||
auth: Redacted.value(auth.session.token),
|
||||
server: config.zeroUrl,
|
||||
schema,
|
||||
kvStore,
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
import { Schema } from "effect";
|
||||
|
||||
const DateFromDateOrString = Schema.Union(
|
||||
Schema.DateFromString,
|
||||
Schema.DateFromSelf,
|
||||
);
|
||||
|
||||
const SessionSchema = Schema.Struct({
|
||||
expiresAt: DateFromDateOrString,
|
||||
token: Schema.String,
|
||||
createdAt: DateFromDateOrString,
|
||||
updatedAt: DateFromDateOrString,
|
||||
ipAddress: Schema.optional(Schema.NullishOr(Schema.String)),
|
||||
userAgent: Schema.optional(Schema.NullishOr(Schema.String)),
|
||||
userId: Schema.String,
|
||||
id: Schema.String,
|
||||
});
|
||||
|
||||
const UserSchema = Schema.Struct({
|
||||
name: Schema.String,
|
||||
email: Schema.String,
|
||||
emailVerified: Schema.Boolean,
|
||||
image: Schema.optional(Schema.NullishOr(Schema.String)),
|
||||
createdAt: DateFromDateOrString,
|
||||
updatedAt: DateFromDateOrString,
|
||||
id: Schema.String,
|
||||
});
|
||||
|
||||
export const AuthState = Schema.Struct({
|
||||
session: SessionSchema,
|
||||
user: UserSchema,
|
||||
});
|
||||
|
||||
export type AuthData = typeof AuthState.Type;
|
||||
Reference in New Issue
Block a user