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

@@ -2,13 +2,13 @@ import { subjects } from "@/subjects";
import { MiddlewareHandler } from "hono";
import { Actor } from "@nestri/core/actor";
import { Api } from "@nestri/core/api/index";
import { Member } from "@nestri/core/member/index";
import { Steam } from "@nestri/core/steam/index";
import { createClient } from "@openauthjs/openauth/client";
import { VisibleError, ErrorCodes } from "@nestri/core/error";
const client = createClient({
clientID: "api",
issuer: process.env.AUTH_URL,
issuer: process.env.AUTH_URL || "http://localhost:3000",
subjects,
});
@@ -52,15 +52,14 @@ export const auth: MiddlewareHandler = async (c, next) => {
ErrorCodes.Authentication.INVALID_TOKEN,
"Invalid bearer token",
);
if (result.subject.type === "team") {
const memberID =
c.req.header("x-nestri-member") || c.req.query("memberID");
if (!memberID) {
if (result.subject.type === "user") {
const steamID = c.req.header("x-nestri-steam") || c.req.query("steamID");
if (!steamID) {
return Actor.provide(
"team",
"user",
{
email: result.subject.properties.email,
teamID: result.subject.properties.teamID,
userID: result.subject.properties.userID,
},
next,
);
@@ -69,17 +68,20 @@ export const auth: MiddlewareHandler = async (c, next) => {
return Actor.provide(
"system",
{
teamID: result.subject.properties.teamID,
userID: result.subject.properties.userID,
},
async () => {
const member = await Member.fromID(email);
if (!member || member.timeDeleted) {
c.status(401);
return c.text("Unauthorized: User not found");
const steamAcc = await Steam.fromID(steamID);
if (!steamAcc) {
throw new VisibleError(
"authentication",
ErrorCodes.Authentication.INVALID_TOKEN,
"Invalid bearer token",
);
}
return Actor.provide(
"member",
{ memberID: member.id, teamID: member.workspaceID },
"steam",
{ steamID: steamAcc.id, userID: steamAcc.ownerID },
next,
);
},