brumbrum_brum
brumbrum_brum9mo ago

Not getting "User" type on context.

I am creating a few "custom" procedures that uses middleware to authenticate a user, like this:
const ownerProcedure = t.procedure.use(async(opts) => await securityHandler.authenticateOwner(opts));
const ownerProcedure = t.procedure.use(async(opts) => await securityHandler.authenticateOwner(opts));
This is the method used for authenticating (It's part of a class with other methods btw):
async authenticateOwner(opts: Record<any, any>) {
const { ctx } = opts;

const jwt = this.getClientSupabaseJWT(ctx);
const userDetails = await this.authenticateUser(jwt);
const clientOrganizationId = this.getClientOrganizationId(ctx);

const { data, error } = await supabase
.from("users_secure_metadata")
.select("*")
.eq("user_id", userDetails.id)
.eq("organization_id", clientOrganizationId)
.single();

if (error || !data) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}

if (data.organization_role !== "owner") {
throw new TRPCError({ code: "UNAUTHORIZED" });
}

const userWithMetadata: IUserWithMetadata = {
metadata: data,
user: userDetails,
};

return opts.next({
ctx: {
user: userWithMetadata,
},
});
}
async authenticateOwner(opts: Record<any, any>) {
const { ctx } = opts;

const jwt = this.getClientSupabaseJWT(ctx);
const userDetails = await this.authenticateUser(jwt);
const clientOrganizationId = this.getClientOrganizationId(ctx);

const { data, error } = await supabase
.from("users_secure_metadata")
.select("*")
.eq("user_id", userDetails.id)
.eq("organization_id", clientOrganizationId)
.single();

if (error || !data) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}

if (data.organization_role !== "owner") {
throw new TRPCError({ code: "UNAUTHORIZED" });
}

const userWithMetadata: IUserWithMetadata = {
metadata: data,
user: userDetails,
};

return opts.next({
ctx: {
user: userWithMetadata,
},
});
}
But for some reason when I use the procedure I can't access the "user" object, showed in image. Why is this?
No description
2 Replies
Alex / KATT 🐱
In-line the security handler function instead of creating a separate fn
brumbrum_brum
brumbrum_brumOP9mo ago
Alrighty, thank you!