99 lines
3.3 KiB
PHP
99 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Auth;
|
|
|
|
use App\Controllers\BaseController;
|
|
use App\Models\UserModel;
|
|
|
|
class PasswordController extends BaseController
|
|
{
|
|
public function forgot()
|
|
{
|
|
return view('auth/forgot', ['title' => 'Forgot Password | Chart-Board']);
|
|
}
|
|
|
|
public function sendResetLink()
|
|
{
|
|
$rules = [
|
|
'email' => 'required|valid_email|max_length[255]',
|
|
];
|
|
|
|
if (! $this->validate($rules)) {
|
|
return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
|
|
}
|
|
|
|
$emailAddress = strtolower((string) $this->request->getPost('email'));
|
|
$userModel = new UserModel();
|
|
$user = $userModel->findByEmail($emailAddress);
|
|
|
|
if ($user) {
|
|
$token = bin2hex(random_bytes(32));
|
|
$expiry = date('Y-m-d H:i:s', strtotime('+30 minutes'));
|
|
|
|
$userModel->update((int) $user['id'], [
|
|
'reset_token' => $token,
|
|
'reset_token_expiry' => $expiry,
|
|
]);
|
|
|
|
$resetLink = base_url('reset-password/' . $token);
|
|
$email = service('email');
|
|
$email->setTo($emailAddress);
|
|
$email->setSubject('Reset your Chart-Board password');
|
|
$email->setMessage('Click to reset your password: <a href="' . esc($resetLink, 'attr') . '">' . esc($resetLink) . '</a>');
|
|
$email->send();
|
|
}
|
|
|
|
return redirect()->back()->with('success', 'If the email exists, a reset link has been sent.');
|
|
}
|
|
|
|
public function reset(string $token)
|
|
{
|
|
$userModel = new UserModel();
|
|
$user = $userModel->where('reset_token', $token)->first();
|
|
|
|
if (! $user) {
|
|
return redirect()->to('/forgot-password')->with('error', 'Invalid reset link.');
|
|
}
|
|
|
|
if (! empty($user['reset_token_expiry']) && strtotime((string) $user['reset_token_expiry']) < time()) {
|
|
return redirect()->to('/forgot-password')->with('error', 'Reset link has expired.');
|
|
}
|
|
|
|
return view('auth/reset', [
|
|
'title' => 'Reset Password | Chart-Board',
|
|
'token' => $token,
|
|
]);
|
|
}
|
|
|
|
public function updatePassword(string $token)
|
|
{
|
|
$rules = [
|
|
'password' => 'required|min_length[8]|max_length[255]',
|
|
'confirm_password' => 'required|matches[password]',
|
|
];
|
|
|
|
if (! $this->validate($rules)) {
|
|
return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
|
|
}
|
|
|
|
$userModel = new UserModel();
|
|
$user = $userModel->where('reset_token', $token)->first();
|
|
|
|
if (! $user) {
|
|
return redirect()->to('/forgot-password')->with('error', 'Invalid reset token.');
|
|
}
|
|
|
|
if (! empty($user['reset_token_expiry']) && strtotime((string) $user['reset_token_expiry']) < time()) {
|
|
return redirect()->to('/forgot-password')->with('error', 'Reset token expired.');
|
|
}
|
|
|
|
$userModel->update((int) $user['id'], [
|
|
'password' => (string) $this->request->getPost('password'),
|
|
'reset_token' => null,
|
|
'reset_token_expiry' => null,
|
|
]);
|
|
|
|
return redirect()->to('/login')->with('success', 'Password reset successful. Please login.');
|
|
}
|
|
}
|