summaryrefslogtreecommitdiffhomepage
path: root/packages/console/core/src
diff options
context:
space:
mode:
Diffstat (limited to 'packages/console/core/src')
-rw-r--r--packages/console/core/src/account.ts13
-rw-r--r--packages/console/core/src/billing.ts2
-rw-r--r--packages/console/core/src/key.ts7
-rw-r--r--packages/console/core/src/schema/account.sql.ts13
-rw-r--r--packages/console/core/src/schema/auth.sql.ts4
-rw-r--r--packages/console/core/src/user.ts37
6 files changed, 32 insertions, 44 deletions
diff --git a/packages/console/core/src/account.ts b/packages/console/core/src/account.ts
index f246c8e1a..c7e096586 100644
--- a/packages/console/core/src/account.ts
+++ b/packages/console/core/src/account.ts
@@ -8,7 +8,6 @@ import { AccountTable } from "./schema/account.sql"
export namespace Account {
export const create = fn(
z.object({
- email: z.string().email(),
id: z.string().optional(),
}),
async (input) =>
@@ -16,7 +15,6 @@ export namespace Account {
const id = input.id ?? Identifier.create("account")
await tx.insert(AccountTable).values({
id,
- email: input.email,
})
return id
}),
@@ -32,15 +30,4 @@ export namespace Account {
.then((rows) => rows[0])
}),
)
-
- export const fromEmail = fn(z.string().email(), async (email) =>
- Database.transaction(async (tx) => {
- return tx
- .select()
- .from(AccountTable)
- .where(eq(AccountTable.email, email))
- .execute()
- .then((rows) => rows[0])
- }),
- )
}
diff --git a/packages/console/core/src/billing.ts b/packages/console/core/src/billing.ts
index 297edebd0..ec7df7e36 100644
--- a/packages/console/core/src/billing.ts
+++ b/packages/console/core/src/billing.ts
@@ -173,7 +173,7 @@ export namespace Billing {
const user = Actor.assert("user")
const { successUrl, cancelUrl } = input
- const email = await User.getAccountEmail(user.properties.userID)
+ const email = await User.getAuthEmail(user.properties.userID)
const customer = await Billing.get()
const session = await Billing.stripe().checkout.sessions.create({
mode: "payment",
diff --git a/packages/console/core/src/key.ts b/packages/console/core/src/key.ts
index e2d5c5eff..688f19b3d 100644
--- a/packages/console/core/src/key.ts
+++ b/packages/console/core/src/key.ts
@@ -4,9 +4,8 @@ import { Actor } from "./actor"
import { and, Database, eq, isNull, sql } from "./drizzle"
import { Identifier } from "./identifier"
import { KeyTable } from "./schema/key.sql"
-import { AccountTable } from "./schema/account.sql"
import { UserTable } from "./schema/user.sql"
-import { User } from "./user"
+import { AuthTable } from "./schema/auth.sql"
export namespace Key {
export const list = fn(z.void(), async () => {
@@ -18,11 +17,11 @@ export namespace Key {
key: KeyTable.key,
timeUsed: KeyTable.timeUsed,
userID: KeyTable.userID,
- email: AccountTable.email,
+ email: AuthTable.subject,
})
.from(KeyTable)
.innerJoin(UserTable, and(eq(KeyTable.userID, UserTable.id), eq(KeyTable.workspaceID, UserTable.workspaceID)))
- .innerJoin(AccountTable, eq(UserTable.accountID, AccountTable.id))
+ .innerJoin(AuthTable, and(eq(UserTable.accountID, AuthTable.accountID), eq(AuthTable.provider, "email")))
.where(
and(
...[
diff --git a/packages/console/core/src/schema/account.sql.ts b/packages/console/core/src/schema/account.sql.ts
index 4d9937114..7bbbe68f9 100644
--- a/packages/console/core/src/schema/account.sql.ts
+++ b/packages/console/core/src/schema/account.sql.ts
@@ -1,12 +1,7 @@
import { mysqlTable, uniqueIndex, varchar } from "drizzle-orm/mysql-core"
import { id, timestamps } from "../drizzle/types"
-export const AccountTable = mysqlTable(
- "account",
- {
- id: id(),
- ...timestamps,
- email: varchar("email", { length: 255 }).notNull(),
- },
- (table) => [uniqueIndex("email").on(table.email)],
-)
+export const AccountTable = mysqlTable("account", {
+ id: id(),
+ ...timestamps,
+})
diff --git a/packages/console/core/src/schema/auth.sql.ts b/packages/console/core/src/schema/auth.sql.ts
index be2244ef3..1e12f98d4 100644
--- a/packages/console/core/src/schema/auth.sql.ts
+++ b/packages/console/core/src/schema/auth.sql.ts
@@ -1,4 +1,4 @@
-import { mysqlEnum, mysqlTable, uniqueIndex, varchar } from "drizzle-orm/mysql-core"
+import { index, mysqlEnum, mysqlTable, uniqueIndex, varchar } from "drizzle-orm/mysql-core"
import { id, timestamps, ulid } from "../drizzle/types"
export const AuthProvider = ["email", "github", "google"] as const
@@ -12,5 +12,5 @@ export const AuthTable = mysqlTable(
subject: varchar("subject", { length: 255 }).notNull(),
accountID: ulid("account_id").notNull(),
},
- (table) => [uniqueIndex("provider").on(table.provider, table.subject)],
+ (table) => [uniqueIndex("provider").on(table.provider, table.subject), index("account_id").on(table.accountID)],
)
diff --git a/packages/console/core/src/user.ts b/packages/console/core/src/user.ts
index 89148a7cd..8b7a96f44 100644
--- a/packages/console/core/src/user.ts
+++ b/packages/console/core/src/user.ts
@@ -7,11 +7,10 @@ import { Actor } from "./actor"
import { Identifier } from "./identifier"
import { render } from "@jsx-email/render"
import { AWS } from "./aws"
-import { Account } from "./account"
-import { AccountTable } from "./schema/account.sql"
import { Key } from "./key"
import { KeyTable } from "./schema/key.sql"
import { WorkspaceTable } from "./schema/workspace.sql"
+import { AuthTable } from "./schema/auth.sql"
export namespace User {
const assertNotSelf = (id: string) => {
@@ -24,10 +23,10 @@ export namespace User {
tx
.select({
...getTableColumns(UserTable),
- accountEmail: AccountTable.email,
+ authEmail: AuthTable.subject,
})
.from(UserTable)
- .leftJoin(AccountTable, eq(UserTable.accountID, AccountTable.id))
+ .leftJoin(AuthTable, and(eq(UserTable.accountID, AuthTable.accountID), eq(AuthTable.provider, "email")))
.where(and(eq(UserTable.workspaceID, Actor.workspace()), isNull(UserTable.timeDeleted))),
),
)
@@ -42,14 +41,14 @@ export namespace User {
),
)
- export const getAccountEmail = fn(z.string(), (id) =>
+ export const getAuthEmail = fn(z.string(), (id) =>
Database.use((tx) =>
tx
.select({
- email: AccountTable.email,
+ email: AuthTable.subject,
})
.from(UserTable)
- .leftJoin(AccountTable, eq(UserTable.accountID, AccountTable.id))
+ .leftJoin(AuthTable, and(eq(UserTable.accountID, AuthTable.accountID), eq(AuthTable.provider, "email")))
.where(and(eq(UserTable.workspaceID, Actor.workspace()), eq(UserTable.id, id)))
.then((rows) => rows[0]?.email),
),
@@ -66,16 +65,24 @@ export namespace User {
const workspaceID = Actor.workspace()
// create user
- const account = await Account.fromEmail(email)
+ const accountID = await Database.use((tx) =>
+ tx
+ .select({
+ accountID: AuthTable.accountID,
+ })
+ .from(AuthTable)
+ .where(and(eq(AuthTable.provider, "email"), eq(AuthTable.subject, email)))
+ .then((rows) => rows[0]?.accountID),
+ )
await Database.use((tx) =>
tx
.insert(UserTable)
.values({
id: Identifier.create("user"),
name: "",
- ...(account
+ ...(accountID
? {
- accountID: account.id,
+ accountID,
}
: {
email,
@@ -94,12 +101,12 @@ export namespace User {
)
// create api key
- if (account) {
+ if (accountID) {
await Database.use(async (tx) => {
const user = await tx
.select()
.from(UserTable)
- .where(and(eq(UserTable.workspaceID, workspaceID), eq(UserTable.accountID, account.id)))
+ .where(and(eq(UserTable.workspaceID, workspaceID), eq(UserTable.accountID, accountID)))
.then((rows) => rows[0])
const key = await tx
@@ -119,11 +126,11 @@ export namespace User {
const emailInfo = await Database.use((tx) =>
tx
.select({
- email: AccountTable.email,
+ inviterEmail: AuthTable.subject,
workspaceName: WorkspaceTable.name,
})
.from(UserTable)
- .innerJoin(AccountTable, eq(UserTable.accountID, AccountTable.id))
+ .innerJoin(AuthTable, and(eq(UserTable.accountID, AuthTable.accountID), eq(AuthTable.provider, "email")))
.innerJoin(WorkspaceTable, eq(WorkspaceTable.id, workspaceID))
.where(
and(eq(UserTable.workspaceID, workspaceID), eq(UserTable.id, Actor.assert("user").properties.userID)),
@@ -138,7 +145,7 @@ export namespace User {
body: render(
// @ts-ignore
InviteEmail({
- inviter: emailInfo.email,
+ inviter: emailInfo.inviterEmail,
assetsUrl: `https://opencode.ai/email`,
workspaceID: workspaceID,
workspaceName: emailInfo.workspaceName,