2022-10-15 11:22:08 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Modules\Auth\Controllers;
|
|
|
|
|
|
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
|
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
|
|
use CodeIgniter\Shield\Controllers\MagicLinkController as ShieldMagicLinkController;
|
2024-04-28 16:39:01 +00:00
|
|
|
use CodeIgniter\Shield\Entities\User;
|
2024-05-29 10:24:13 +00:00
|
|
|
use Override;
|
2022-10-15 11:22:08 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use ViewThemes\Theme;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles "Magic Link" logins - an email-based no-password login protocol. This works much like password reset would,
|
|
|
|
* but Shield provides this in place of password reset. It can also be used on it's own without an email/password login
|
|
|
|
* strategy.
|
|
|
|
*/
|
|
|
|
class MagicLinkController extends ShieldMagicLinkController
|
|
|
|
{
|
2024-05-29 10:24:13 +00:00
|
|
|
#[Override]
|
2022-10-15 11:22:08 +00:00
|
|
|
public function initController(
|
|
|
|
RequestInterface $request,
|
|
|
|
ResponseInterface $response,
|
|
|
|
LoggerInterface $logger
|
|
|
|
): void {
|
|
|
|
parent::initController($request, $response, $logger);
|
|
|
|
|
|
|
|
Theme::setTheme('auth');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPasswordView(): string | RedirectResponse
|
|
|
|
{
|
|
|
|
if (! session('magicLogin')) {
|
2024-04-28 16:39:01 +00:00
|
|
|
return redirect()->to(config('Auth')->loginRedirect());
|
2022-10-15 11:22:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return view(setting('Auth.views')['magic-link-set-password']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPasswordAction(): RedirectResponse
|
|
|
|
{
|
|
|
|
$rules = [
|
|
|
|
'new_password' => 'required|strong_password',
|
|
|
|
];
|
|
|
|
|
|
|
|
if (! $this->validate($rules)) {
|
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
2023-10-03 16:21:08 +00:00
|
|
|
->with('errors', $this->validator->getErrors());
|
2022-10-15 11:22:08 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 15:42:52 +00:00
|
|
|
$validData = $this->validator->getValidated();
|
|
|
|
|
2024-04-28 16:39:01 +00:00
|
|
|
$user = auth()
|
|
|
|
->user();
|
2022-10-15 11:22:08 +00:00
|
|
|
|
2024-04-28 16:39:01 +00:00
|
|
|
if ($user instanceof User) {
|
|
|
|
// set new password to user
|
|
|
|
$user->password = $validData['new_password'];
|
|
|
|
|
|
|
|
$userModel = auth()
|
|
|
|
->getProvider();
|
|
|
|
$userModel->save($user);
|
2022-10-15 11:22:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove magic login session to reinstate normal check
|
|
|
|
if (session('magicLogin')) {
|
|
|
|
session()->removeTempdata('magicLogin');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success!
|
2024-04-28 16:39:01 +00:00
|
|
|
return redirect()->to(config('Auth')->loginRedirect())
|
2022-10-15 11:22:08 +00:00
|
|
|
->with('message', lang('MyAccount.messages.passwordChangeSuccess'));
|
|
|
|
}
|
|
|
|
}
|