fix: Fix the auth API middleware

This commit is contained in:
Wanjohi
2025-10-27 01:23:14 +03:00
parent 53390524ff
commit ef5e626c9d
4 changed files with 68 additions and 34 deletions

View File

@@ -3,27 +3,26 @@ import { Context } from "./context.js";
import { ErrorCodes, VisibleError } from "./error.js";
export namespace Actor {
export interface Member {
type: "member";
export interface User {
type: "user";
properties: {
userID: string;
email: string;
teamID: string;
memberID: string;
};
}
export interface Team {
type: "team";
export interface Steam {
type: "steam";
properties: {
teamID: string;
email: string;
userID: string;
steamID: string;
};
}
export interface System {
type: "system";
properties: {
teamID: string;
userID: string;
};
}
@@ -40,13 +39,13 @@ export namespace Actor {
properties: {};
}
export type Info = Team | Member | Public | Token | System;
export type Info = Steam | User | Public | Token | System;
export const ctx = Context.create<Info>();
export function teamID() {
export function userID() {
const actor = ctx.use();
if ("teamID" in actor.properties) return actor.properties.teamID;
if ("userID" in actor.properties) return actor.properties.userID;
throw new VisibleError(
"authentication",
ErrorCodes.Authentication.UNAUTHORIZED,

View File

@@ -22,7 +22,6 @@ export namespace Examples {
id: "74839300282033", // Steam ID
ownerID: User.id,
name: "JD The 65th",
username: "jdoe",
realName: "John Doe",
memberSince: new Date("2010-01-26T21:00:00.000Z"),
avatarHash: "3a5e805fd4c1e04e26a97af0b9c6fab2dee91a19",

View File

@@ -1,6 +1,10 @@
import { z } from "zod/v4";
import { Examples } from "../examples.js";
import { Limitations } from "./steam.sql.js";
import { Limitations, steamTable } from "./steam.sql.js";
import { fn } from "../utils/fn.js";
import { Actor } from "../actor.js";
import { Database } from "../drizzle/index.js";
import { eq, isNull, and } from "drizzle-orm";
export namespace Steam {
export const Info = z
@@ -18,10 +22,6 @@ export namespace Steam {
description: "The display name of the Steam account",
example: Examples.SteamAccount.name,
}),
username: z.string().meta({
description: "The username of the Steam account",
example: Examples.SteamAccount.username,
}),
realName: z.string().nullable().meta({
description: "The real name associated with the Steam account",
example: Examples.SteamAccount.realName,
@@ -38,7 +38,7 @@ export namespace Steam {
description: "Account limitations and restrictions",
example: Examples.SteamAccount.limitations,
}),
profileUrl: z.string().meta({
profileUrl: z.string().nullable().meta({
description: "The Steam community profile URL or identifier",
example: Examples.SteamAccount.profileUrl,
}),
@@ -54,4 +54,38 @@ export namespace Steam {
});
export type Info = z.infer<typeof Info>;
export const fromID = fn(Info.shape.id, async (id) => {
const userID = Actor.userID();
return Database.transaction((tx) =>
tx
.select()
.from(steamTable)
.where(
and(
eq(steamTable.id, id),
eq(steamTable.ownerID, userID),
isNull(steamTable.timeDeleted),
),
)
.then((rows) => rows.map(serialize).at(0)),
);
});
export function serialize(
input: typeof steamTable.$inferSelect,
): z.infer<typeof Info> {
return {
id: input.id,
name: input.name,
ownerID: input.ownerID,
realName: input.realName,
profileUrl: input.profileUrl,
memberSince: input.memberSince,
avatarHash: input.avatarHash,
limitations: input.limitations,
lastSyncedAt: input.lastSyncedAt,
};
}
}