#!/usr/bin/env bare
/**
* Set up admin user with bearer token identity
*/
import Corestore from 'corestore'
import Hyperbee from 'hyperbee'
import process from 'bare-process'
import crypto from 'bare-crypto'
async function main() {
const token = process.argv[2]
if (!token) {
console.error('Usage: bare setup-admin-user.js <bearer-token>')
process.exit(1)
}
console.log('👤 Setting up admin user...\n')
// Initialize storage
const store = new Corestore('./pear-git-storage')
await store.ready()
const core = store.get({ name: 'pear-git-db' })
const db = new Hyperbee(core, { keyEncoding: 'utf-8', valueEncoding: 'json' })
await db.ready()
try {
// Hash the token to get identityId
const tokenHash = crypto.createHash('sha256').update(token).digest('hex')
const identityId = tokenHash
console.log('Token hash / identityId:', identityId)
console.log('')
// Check if user already exists
const existingIdentity = await db.get(`identity:${identityId}`)
if (existingIdentity) {
console.log('✅ Identity already exists')
const identity = existingIdentity.value
if (identity.userId) {
const userEntry = await db.get(`user:${identity.userId}`)
if (userEntry) {
console.log(`✅ Linked to user: ${userEntry.value.name} (${identity.userId})`)
console.log(` Permissions: ${userEntry.value.globalPermissions.join(', ')}`)
return
}
}
}
// Create admin user
const userId = 'admin-' + crypto.randomBytes(8).toString('hex')
const user = {
userId,
name: 'Admin',
email: null,
profilePicture: null,
globalPermissions: ['read', 'create-issue', 'manage-issues', 'write', 'admin'],
repos: [{ name: '*', permissions: ['read', 'create-issue', 'manage-issues', 'write', 'admin'] }],
identities: [identityId],
createdAt: Date.now(),
updatedAt: Date.now()
}
await db.put(`user:${userId}`, user)
console.log(`✅ Created user: ${user.name} (${userId})`)
// Create identity
const identity = {
identityId,
method: 'bearer-token',
userId,
credentials: { tokenHash },
createdAt: Date.now(),
updatedAt: Date.now()
}
await db.put(`identity:${identityId}`, identity)
console.log(`✅ Created identity (bearer-token)`)
// Update user's identities list
console.log(`✅ Linked identity to user`)
console.log('')
console.log('🎉 Setup complete!')
console.log('')
console.log('You can now use the bearer token to create issues:')
console.log(` curl -H "Authorization: Bearer ${token}" \\`)
console.log(' -X POST http://localhost:5005/git/tabertron/pear-git/issues \\')
console.log(' -d \'{"title":"Test","description":"Test issue"}\'')
} catch (err) {
console.error('❌ Error:', err.message)
console.error(err)
process.exit(1)
} finally {
await db.close()
await store.close()
}
}
main().catch(err => {
console.error('Fatal error:', err)
process.exit(1)
})