public function forgotPasswordAction(ClientInterface $client)
{
switch ($_SERVER['REQUEST_METHOD']) {
case "GET":
echo $this->twig->render('auth/forgotpass.twig');
break;
case "POST":
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
if ($email) {
$response = false;
// FIND ACCOUNT
$user = Gatekeeper::findUserByEmail($email);
// Ignore use case when user isn't found.
// We don't want to reveal that someone has an email in our database
if ($user) {
// GENERATE CODE
$code = $user->getResetPasswordCode();
// GENERATE HTML
$html = $this->twig->render('emails/forgotpass.twig', ['code' => $code, 'email' => base64_encode($email)]);
// CONFIGURE SENDER AND URL
$replyto = $this->site_config['replyto'] ?? $this->site_config['sender'];
$url = 'https://api.mailgun.net/v3/' . $this->mailgun_config['domain'] . '/messages';
// SEND EMAIL
$response = $client->request('POST', $url, ['auth' => ['api', $this->mailgun_config['key']], 'multipart' => [['name' => 'to', 'contents' => $email], ['name' => 'from', 'contents' => $replyto], ['name' => 'subject', 'contents' => 'Forgot your password?'], ['name' => 'html', 'contents' => $html]]]);
}
if ($response && $response->getStatusCode() == "200") {
// REDIRECT
$this->flasher->info('Password reset email is on its way!');
} else {
$this->flasher->error('Email could not be sent :(');
}
$this->redirect('/auth');
} else {
$this->flasher->error('Invalid email provided!');
$this->redirect('/auth/forgotpass');
}
break;
default:
echo $this->twig->render('error405.twig');
}
}