Created
February 18, 2019 23:09
-
-
Save romaninsh/2e127ab584ed732d0caaa3b23ab56e11 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace saasty; | |
use atk4\ui\Form; | |
use saasty\Model\User; | |
chdir('..'); | |
require '../vendor/autoload.php'; | |
// added this shit here. | |
$app = new frontend\App(['appRequired'=>false, 'authRequired'=>false, 'useLayout'=>'Wide']); | |
$app->add(['Header', 'Password Reminder', 'size'=>2]); | |
$app->add(['ui'=>'hidden divider']); | |
if(isset($_GET['sent'])) { | |
$app->add(['Message', 'Email Recovery', 'success', 'icon'=>'thumbs up']) | |
->text->addParagraph('We have sent you an email with the password recovery instructions'); | |
$app->add(['Button', 'Back', 'icon'=>'left arrow'])->link(['index']); | |
exit; | |
} | |
if ($token = $_GET['token'] ?? null) { | |
try { | |
$t_model = new Model\User\Token\PasswordReset($app->db); | |
$t_model->loadBy('token', $token); | |
if(isset($_GET['changed'])) { | |
$app->add(['Message', 'Password change', 'success', 'icon'=>'thumbs up']) | |
->text->addParagraph('Your password have been successfully changed'); | |
$app->add(['Button', 'Back', 'icon'=>'left arrow'])->link(['index']); | |
exit; | |
} | |
if (!$t_model['is_valid']) { | |
throw new Exception(['Token was already used or is expired']); | |
} | |
$form = $app->add('Form'); | |
$form->stickyGet('token'); | |
$form->addField('new_password'); | |
$form->onSubmit(function(Form $form) use ($t_model) { | |
$t_model->setNewPassword($form->model['new_password']); | |
return $form->app->jsRedirect(['changed'=>'true']); | |
}); | |
$form->add(['Button', 'Back', 'icon'=>'left arrow'])->link(['index']); | |
exit; | |
} catch (\Exception $e) { | |
$app->add(['Message', 'Incorrect token', 'error']) | |
->text->addParagraph($e->getMessage()); | |
$app->add(['Button', 'Back', 'icon'=>'left arrow'])->link(['index']); | |
exit; | |
} | |
} | |
$form = $app->add(['Form']); | |
$form->buttonSave->set('Recover'); | |
$form->add(['Button', 'Back', 'secondary', 'icon'=>'left arrow'])->link(['login']); | |
$form->addField('email'); | |
$form->onSubmit(function($form) use ($app){ | |
// look up user by email | |
$user = new Model\User($app->db); | |
$user->tryLoadBy('email', $form->model['email']); | |
if (!$user->loaded()) { | |
return $form->error('email', 'Email is specified incorrectly'); | |
} | |
$user->resetPassword(); | |
//$token = $user->ref('Tokens:password-reset')->save()['token']; | |
return $form->app->jsRedirect(['sent'=>true]); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment