mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
Merge pull request #4084 from Ocelot-Social-Community/4012-default-admin
feat: 🍰 Create Default Admin User
This commit is contained in:
commit
5e396d1458
@ -1,11 +1,60 @@
|
|||||||
import { getDriver, getNeode } from '../../db/neo4j'
|
import { getDriver, getNeode } from '../../db/neo4j'
|
||||||
|
import { hashSync } from 'bcryptjs'
|
||||||
|
import { v4 as uuid } from 'uuid'
|
||||||
|
|
||||||
|
const defaultAdmin = {
|
||||||
|
email: 'admin@example.org',
|
||||||
|
password: hashSync('1234', 10),
|
||||||
|
name: 'admin',
|
||||||
|
id: uuid(),
|
||||||
|
slug: 'admin',
|
||||||
|
}
|
||||||
|
|
||||||
|
const createDefaultAdminUser = async (session) => {
|
||||||
|
const readTxResultPromise = session.readTransaction(async (txc) => {
|
||||||
|
const result = await txc.run('MATCH (user:User) RETURN count(user) AS userCount')
|
||||||
|
return result.records.map((r) => r.get('userCount'))
|
||||||
|
})
|
||||||
|
let createAdmin = false
|
||||||
|
try {
|
||||||
|
const userCount = parseInt(String(await readTxResultPromise))
|
||||||
|
if (userCount === 0) createAdmin = true
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error) // eslint-disable-line no-console
|
||||||
|
}
|
||||||
|
if (createAdmin) {
|
||||||
|
const createAdminTxResultPromise = session.writeTransaction(async (txc) => {
|
||||||
|
txc.run(
|
||||||
|
`MERGE (e:EmailAddress {
|
||||||
|
email: "${defaultAdmin.email}",
|
||||||
|
createdAt: toString(datetime())
|
||||||
|
})-[:BELONGS_TO]->(u:User {
|
||||||
|
name: "${defaultAdmin.name}",
|
||||||
|
encryptedPassword: "${defaultAdmin.password}",
|
||||||
|
role: "admin",
|
||||||
|
id: "${defaultAdmin.id}",
|
||||||
|
slug: "${defaultAdmin.slug}",
|
||||||
|
createdAt: toString(datetime()),
|
||||||
|
deleted: false,
|
||||||
|
disabled: false
|
||||||
|
})-[:PRIMARY_EMAIL]->(e)`,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
try {
|
||||||
|
await createAdminTxResultPromise
|
||||||
|
console.log('Successfully created default admin user') // eslint-disable-line no-console
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error) // eslint-disable-line no-console
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class Store {
|
class Store {
|
||||||
async init(next) {
|
async init(next) {
|
||||||
const neode = getNeode()
|
const neode = getNeode()
|
||||||
const { driver } = neode
|
const { driver } = neode
|
||||||
const session = driver.session()
|
const session = driver.session()
|
||||||
// eslint-disable-next-line no-console
|
await createDefaultAdminUser(session)
|
||||||
const writeTxResultPromise = session.writeTransaction(async (txc) => {
|
const writeTxResultPromise = session.writeTransaction(async (txc) => {
|
||||||
await txc.run('CALL apoc.schema.assert({},{},true)') // drop all indices
|
await txc.run('CALL apoc.schema.assert({},{},true)') // drop all indices
|
||||||
return Promise.all(
|
return Promise.all(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user