feat: fix authentication

This commit is contained in:
mrjvs
2026-08-08 20:55:56 +02:00
parent 69d133ab85
commit 0e174905f0
16 changed files with 154 additions and 116 deletions

View File

@@ -1,22 +1,22 @@
export type AuthContext = {
pid: number;
}
};
export function setAuthContext(event: H3Event, context: AuthContext) {
export function setAuthContext(event: H3Event, context: AuthContext): void {
event.context.auth = context;
}
export function getAuthContext(event: H3Event): AuthContext | null {
return event.context.auth ?? null;
}
export function enforceLoggedIn(event: H3Event): AuthContext {
const context = getAuthContext(event);
if (!context) throw createError({
status: 401,
statusText: "This action requires authentication",
})
if (!context) {
throw createError({
status: 401,
statusText: 'This action requires authentication'
});
}
return context;
}

View File

@@ -1,10 +1,12 @@
import type { ZodType } from "zod";
import type { z, ZodType } from 'zod';
export async function readZodBody<T extends ZodType>(event: H3Event, schema: T) {
export async function readZodBody<T extends ZodType>(event: H3Event, schema: T): Promise<z.infer<T>> {
const body = await readValidatedBody(event, schema.safeParse);
if (!body.success) throw createError({
status: 400,
statusText: 'Invalid input'
})
if (!body.success) {
throw createError({
status: 400,
statusText: 'Invalid input'
});
}
return body.data;
}

View File

@@ -1,31 +1,48 @@
import { APIDefinition } from "@pretendonetwork/grpc/api/api_service";
import { createChannel, createClient, Channel, type Client, Metadata } from "nice-grpc";
import { createChannel, createClient, Metadata } from 'nice-grpc';
import { APIDefinition } from '@pretendonetwork/grpc/api/api_service';
import { AccountServiceDefinition } from '@pretendonetwork/grpc/account/v2/account_service';
import type { Channel, Client, CompatServiceDefinition } from 'nice-grpc';
let grpc: { channel: Channel, client: Client<APIDefinition> } | null = null;
let grpc: { channel: Channel } | null = null;
function getGrpcClient<T extends CompatServiceDefinition>(event: H3Event, def: T, token?: string): Client<T> {
const config = useRuntimeConfig(event);
function getGrpcClient(event: typeof H3Event): Client<APIDefinition> {
if (!grpc) {
const config = useRuntimeConfig();
if (!config.grpcHost || !config.grpcApiKey) {
throw new Error("GRPC not configured");
if (!config.grpcHost) {
throw new Error('GRPC not configured');
}
const channel = createChannel(config.grpcHost);
const metadata = new Metadata();
metadata.append("X-API-Key", config.grpcApiKey);
grpc = {
channel,
client: createClient(APIDefinition, channel, {
"*": {
metadata
}
})
}
channel: createChannel(config.grpcHost)
};
}
return grpc.client;
const metadata = new Metadata();
if (config.grpcApiKey) {
metadata.append('X-API-Key', config.grpcApiKey);
}
if (token) {
metadata.append('X-Token', token);
}
const client = createClient(def, grpc.channel, {
'*': {
metadata
}
});
return client;
}
export function useGrpc(event: H3Event) {
return getGrpcClient(event);
export function useApiGrpc(event: H3Event): Client<APIDefinition> {
return getGrpcClient(event, APIDefinition);
}
export function useAccountGrpc(event: H3Event): Client<AccountServiceDefinition> {
return getGrpcClient(event, AccountServiceDefinition);
}
export function useApiGrpcWithToken(event: H3Event, token: string): Client<APIDefinition> {
return getGrpcClient(event, APIDefinition, token);
}