Password Recovery

Hi there, I am developing a new theme and intend to offer the possibilty to enable Kirby’s password reset. My site blueprint is already working but I have problems to implement the dynamic SMTP settings. How can I achieve this using config.php?

config.php (excerpt)

  'auth' => [
    'methods' => ['password', 'password-reset'],
    'trials' => 10
  ],
  'email' => [
    'transport' => [
      'type' => 'smtp',
      'host' => 'TBD',
      'port' => TBD,
      'security' => 'TBD',
      'auth' => true,
      'username' => 'TBD',
      'password' => 'TBD',
    ],
       'from' => 'TBD'
  ],

site.yml (excerpt)

  authentication:
    label: Authentication
    icon: key-2-line
    columns:
      left:
        width: 1/2
        fields:
          passwordreset:
            label: Password Reset
            type: headline
          resetpassword:
            label: false
            type: toggle
            text:
              - "No"
              - "Yes"
            help: Enable password recovery by email
      right:
        width: 1/2
        fields:
          smtp:
            label: Simple Mail Transfer Protocol
            type: headline
            when:
              resetpassword: true
          email:
            label: Email address
            type: email
            required: true
            help: Used to send a login code to the user
            when:
              resetpassword: true
          username:
            label: Username
            type: text
            when:
              resetpassword: true
          password:
            label: Password
            type: text
            when:
              resetpassword: true
          host:
            label: Host
            type: text
            required: true
            when:
              resetpassword: true
          port:
            label: Port
            type: number
            width: 1/2
            when:
              resetpassword: true
          security:
            label: security
            type: toggles
            options:
              - SSL
              - TLS
            width: 1/2
            when:
              resetpassword: true

If you want to access $kirby or $site, you need to use the ready config option and define anything in there.

Note that if you define the auth methods like above, password-reset will be available but will not work, so you would have to wrap that setting conditionally in the ready option as well.

Thanks you. I implemented it this way:

  'ready' => function ($kirby) {
    $config = [];

    if ($kirby->site()->resetpassword()->toBool()) {

      $host = $kirby->site()->host()->value();
      $port = $kirby->site()->port()->toInt();
      $security = $kirby->site()->security()->value();
      $username = $kirby->site()->username()->value();
      $password = $kirby->site()->password()->value();
      $from = $kirby->site()->email()->value();

      if ($host && $port && $username && $password && $from) {
        $config['auth'] = [
          'methods' => ['password', 'password-reset']
        ];
        $config['email'] = [
          'transport' => [
            'type' => 'smtp',
            'host' => $host,
            'port' => $port,
            'security' => $security ?: 'tls',
            'auth' => true,
            'username' => $username,
            'password' => $password,
          ],
          'from' => $from
        ];
      }
    }

    return $config;
  },

Anything missing or unfavorable? What do you think?