feat: habilitar provisionamento desktop e rotas CORS
This commit is contained in:
parent
7569986ffc
commit
152550a9a0
19 changed files with 1806 additions and 211 deletions
81
src/server/machines-session.ts
Normal file
81
src/server/machines-session.ts
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
import { ConvexHttpClient } from "convex/browser"
|
||||
|
||||
import { api } from "@/convex/_generated/api"
|
||||
import type { Id } from "@/convex/_generated/dataModel"
|
||||
import { DEFAULT_TENANT_ID } from "@/lib/constants"
|
||||
import { env } from "@/lib/env"
|
||||
import { ensureMachineAccount } from "@/server/machines-auth"
|
||||
import { auth } from "@/lib/auth"
|
||||
|
||||
export type MachineSessionContext = {
|
||||
machine: {
|
||||
id: Id<"machines">
|
||||
hostname: string
|
||||
osName: string | null
|
||||
osVersion: string | null
|
||||
architecture: string | null
|
||||
status: string | null
|
||||
lastHeartbeatAt: number | null
|
||||
companyId: Id<"companies"> | null
|
||||
companySlug: string | null
|
||||
metadata: Record<string, unknown> | null
|
||||
}
|
||||
headers: Headers
|
||||
response: unknown
|
||||
}
|
||||
|
||||
export async function createMachineSession(machineToken: string, rememberMe = true): Promise<MachineSessionContext> {
|
||||
const convexUrl = env.NEXT_PUBLIC_CONVEX_URL
|
||||
if (!convexUrl) {
|
||||
throw new Error("Convex não configurado")
|
||||
}
|
||||
|
||||
const client = new ConvexHttpClient(convexUrl)
|
||||
|
||||
const resolved = await client.mutation(api.machines.resolveToken, { machineToken })
|
||||
let machineEmail = resolved.machine.authEmail ?? null
|
||||
|
||||
if (!machineEmail) {
|
||||
const account = await ensureMachineAccount({
|
||||
machineId: resolved.machine._id,
|
||||
tenantId: resolved.machine.tenantId ?? DEFAULT_TENANT_ID,
|
||||
hostname: resolved.machine.hostname,
|
||||
machineToken,
|
||||
})
|
||||
|
||||
await client.mutation(api.machines.linkAuthAccount, {
|
||||
machineId: resolved.machine._id as Id<"machines">,
|
||||
authUserId: account.authUserId,
|
||||
authEmail: account.authEmail,
|
||||
})
|
||||
|
||||
machineEmail = account.authEmail
|
||||
}
|
||||
|
||||
const signIn = await auth.api.signInEmail({
|
||||
body: {
|
||||
email: machineEmail,
|
||||
password: machineToken,
|
||||
rememberMe,
|
||||
},
|
||||
returnHeaders: true,
|
||||
})
|
||||
|
||||
return {
|
||||
machine: {
|
||||
id: resolved.machine._id as Id<"machines">,
|
||||
hostname: resolved.machine.hostname,
|
||||
osName: resolved.machine.osName,
|
||||
osVersion: resolved.machine.osVersion,
|
||||
architecture: resolved.machine.architecture,
|
||||
status: resolved.machine.status,
|
||||
lastHeartbeatAt: resolved.machine.lastHeartbeatAt,
|
||||
companyId: (resolved.machine.companyId ?? null) as Id<"companies"> | null,
|
||||
companySlug: resolved.machine.companySlug ?? null,
|
||||
metadata: (resolved.machine.metadata ?? null) as Record<string, unknown> | null,
|
||||
},
|
||||
headers: signIn.headers,
|
||||
response: signIn.response,
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue