mirror of
https://github.com/wu736139669/hapi.git
synced 2026-08-05 06:24:37 +00:00
30 lines
756 B
TypeScript
30 lines
756 B
TypeScript
import { randomUUID } from 'node:crypto'
|
|
import { configuration } from '@/configuration'
|
|
import { updateSettings } from '@/persistence'
|
|
|
|
export async function authAndSetupMachineIfNeeded(): Promise<{
|
|
token: string
|
|
machineId: string
|
|
}> {
|
|
if (!configuration.cliApiToken) {
|
|
throw new Error('CLI_API_TOKEN is required')
|
|
}
|
|
|
|
const settings = await updateSettings((current) => {
|
|
if (!current.machineId) {
|
|
return {
|
|
...current,
|
|
machineId: randomUUID()
|
|
}
|
|
}
|
|
return current
|
|
})
|
|
|
|
if (!settings.machineId) {
|
|
throw new Error('Failed to initialize machineId')
|
|
}
|
|
|
|
return { token: configuration.cliApiToken, machineId: settings.machineId }
|
|
}
|
|
|