## Starting the application

Install dependencies:

```bash
{{packageManager}} install
```

{{#if adminUserTableInstructions}}
Create the admin users table in your database before starting the app: AdminForth needs it for back-office authentication, and no Prisma migrations were generated for this project.

{{{adminUserTableInstructions}}}

The generated app will seed the default `adminforth` / `adminforth` user on first start if the table is empty.
{{/if}}

{{#if prismaDbUrl}}
Create the initial migration and apply it to the database:

```bash
{{packageManagerRun}} makemigration{{packageManagerScriptArgSeparator}}--name init
{{packageManagerRun}} migrate:local
```
{{/if}}

First-time setup on a fresh clone: `create-app` wrote a random `ADMINFORTH_SECRET` into `.env`, but that file is gitignored, so every developer and every deployment creates their own (see `.env.example`). The command below only creates `.env` when it does not exist yet:

```bash
# On Windows (PowerShell): node -e "require('fs').writeFileSync('.env', 'ADMINFORTH_SECRET=' + require('crypto').randomBytes(32).toString('hex') + '\n', { flag: 'wx' })"
[ -f .env ] || (umask 077; echo "ADMINFORTH_SECRET=$(openssl rand -hex 32)" > .env)
```

Start the server:

```bash
{{packageManagerRun}} dev
```

{{#if prismaDbUrl}}
## Changing schema

Open `schema.prisma` and change schema as needed: add new tables, columns, etc (See [Prisma schema reference](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema)).

Run the following command to generate a new migration and apply it instantly in local database:

```bash
{{packageManagerRun}} makemigration{{packageManagerScriptArgSeparator}}--name <name_of_changes>
```

Your colleagues will need to pull the changes and run `{{packageManagerRun}} migrate:local` to apply the migration in their local database.
{{/if}}

## Deployment tips

You have Dockerfile ready for production deployment. You can test the build with:

```bash
# Generate a signing key once and keep it in your secret store (never commit it).
# On Windows without openssl: node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
export ADMINFORTH_SECRET="$(openssl rand -hex 32)"

docker build -t {{appName}}-image .
docker run -p 3500:3500 -e ADMINFORTH_SECRET="$ADMINFORTH_SECRET" {{#if sqliteFile}}-v $(pwd)/db:/code/db {{/if}}{{appName}}-image
```

To set non-sensitive environment variables in production, use `.env.prod` file.
For sensitive variables, use direct docker environment variables or secrets from your vault.

## Documentation

- [Customizing AdminForth Branding](https://adminforth.dev/docs/tutorial/Customization/branding/)
- [Custom Field Rendering](https://adminforth.dev/docs/tutorial/Customization/customFieldRendering/)
- [Hooks](https://adminforth.dev/docs/tutorial/Customization/hooks/)
- [Custom Pages](https://adminforth.dev/docs/tutorial/Customization/customPages/)
