feat: Add user to auth.ts function

This commit is contained in:
Wanjohi
2025-10-26 05:56:22 +03:00
parent 752f3e94d0
commit 53390524ff
8 changed files with 116 additions and 31 deletions

View File

@@ -3,7 +3,7 @@ import { RedisClient } from "bun";
import { logger } from "hono/logger";
import { subjects } from "@/subjects.js";
import { issuer } from "@openauthjs/openauth";
import { Team } from "@nestri/core/team/index";
import { User } from "@nestri/core/user/index";
import { RedisStorage } from "./src/storage.js";
import { CodeProvider } from "@openauthjs/openauth/provider/code";
@@ -48,15 +48,17 @@ const app = issuer({
let email = res.claims.email;
if (!email) throw new Error("No email found");
let teamID = await Team.fromEmail(email).then((x) => x?.id);
if (!teamID) {
let userID = await User.fromEmail(email).then((x) => x?.id);
if (!userID) {
console.log("creating account for", email);
teamID = await Team.create({
userID = await User.create({
email: email!,
name: null,
});
}
return ctx.subject("team", email!, {
teamID: teamID!,
return ctx.subject("user", email!, {
userID: userID!,
email: email,
});
},

View File

@@ -2,8 +2,8 @@ import { z } from "zod/v4";
import { createSubjects } from "@openauthjs/openauth/subject";
export const subjects = createSubjects({
team: z.object({
user: z.object({
email: z.email(),
teamID: z.string(),
userID: z.string(),
}),
});