// .app/shared/api/effectApi.ts
import { HttpApi, HttpApiEndpoint, HttpApiGroup } from "@effect/platform";
import { Authentication } from "./authMiddleware";
export const EffectServerApi = HttpApi.make("EffectServer").add(
HttpApiGroup.make("profiles")
.add(
HttpApiEndpoint.post("createProfile", `/create_profile`)
.middleware(Authentication)
)
)
)
// .app/shared/api/authMiddleware.ts
import { Context, Schema } from "effect"
import { HttpApiMiddleware, HttpApiSecurity, OpenApi, HttpApiSchema } from "@effect/platform"
export class AuthenticatedUserContext extends Context.Tag("AuthenticatedUserContext")<
AuthenticatedUserContext,
{
readonly userName: string;
readonly userId: string;
}
>() {}
export class Unauthorized extends Schema.TaggedError<Unauthorized>()(
"Unauthorized",
{
message: Schema.String
},
HttpApiSchema.annotations({ status: 401 })
) {}
export class Authentication extends HttpApiMiddleware.Tag<Authentication>()("Authentication", {
optional: false,
failure: Unauthorized,
provides: AuthenticatedUserContext,
security: {
bearer: HttpApiSecurity.bearer.pipe(
HttpApiSecurity.annotate(OpenApi.Format, 'jwt'),
)
}
}) {}
// .app/shared/api/effectApi.ts
import { HttpApi, HttpApiEndpoint, HttpApiGroup } from "@effect/platform";
import { Authentication } from "./authMiddleware";
export const EffectServerApi = HttpApi.make("EffectServer").add(
HttpApiGroup.make("profiles")
.add(
HttpApiEndpoint.post("createProfile", `/create_profile`)
.middleware(Authentication)
)
)
)
// .app/shared/api/authMiddleware.ts
import { Context, Schema } from "effect"
import { HttpApiMiddleware, HttpApiSecurity, OpenApi, HttpApiSchema } from "@effect/platform"
export class AuthenticatedUserContext extends Context.Tag("AuthenticatedUserContext")<
AuthenticatedUserContext,
{
readonly userName: string;
readonly userId: string;
}
>() {}
export class Unauthorized extends Schema.TaggedError<Unauthorized>()(
"Unauthorized",
{
message: Schema.String
},
HttpApiSchema.annotations({ status: 401 })
) {}
export class Authentication extends HttpApiMiddleware.Tag<Authentication>()("Authentication", {
optional: false,
failure: Unauthorized,
provides: AuthenticatedUserContext,
security: {
bearer: HttpApiSecurity.bearer.pipe(
HttpApiSecurity.annotate(OpenApi.Format, 'jwt'),
)
}
}) {}