IonAuth is running in the ThirdParty folder on Codeigniter4. I have extended the controller, in that I have Auth.php running in my app/Controllers directory and that is working.
How can I extend the IonAuth library, the model and the language files etc? These files get ignored, unlike the Auth.php controller. I don't want to edit the files in the ThirdParty folder, to make it easier to upgrade etc. Thanks.
look at my code do it like me follow my approach ctl auth
<?php namespace Modules\Auth\Controllers;
use Modules\Auth\Config\ModuleAuthConfig;
use Modules\Auth\Config\Services;
use Modules\Auth\Entities\AuthEntity;
use Modules\Auth\Enums\RoleType;
use Modules\Auth\Interfaces\AuthControllerInterface;
use Modules\Shared\Config\ModuleSharedConfig;
use Modules\Shared\Controllers\BaseController;
use Modules\Shared\Enums\NotificationType;
use CodeIgniter\HTTP\ResponseInterface;
use Myth\Auth\AuthTrait;
use Pusher\Pusher;
use ReCaptcha\ReCaptcha;
class Auth extends BaseController implements AuthControllerInterface
{
use AuthTrait;
public function signInJwt(): ResponseInterface
{
/**
* Attempts to verify the user's credentials
* through a POST request.
*/
$rules = [
'login' => 'required',
'password' => 'required'
];
$config = config('Myth\Auth\Config\Auth');
if ($config->validFields == ['email']) {
$rules['login'] .= '|valid_email';
};
if (!$this->validate($rules)) {
return $this->response->setJSON(['error' => $this->validator->getErrors()])
->setStatusCode(ResponseInterface::HTTP_NOT_ACCEPTABLE, lang('Authenticate.auth.validation'))
->setContentType('application/json');
}
$authEntity = new AuthEntity((array)$this->request->getVar());
$authEntity->logInMode()->loginDate()->ipAddress($this->request->getIPAddress());
$authService = Services::authService();
$data = $authService->signInJwt($authEntity);
$authConfig = new ModuleAuthConfig();
return $this->response->setHeader($authConfig->jwt['name'], $data['jwt']['token'])
->setCookie($authConfig->jwt['name'], $data['jwt']['token'], $data['jwt']['expire'])->
setJSON($data)->setStatusCode(ResponseInterface::HTTP_OK, lang('Authenticate.auth.logIn'))
->setContentType('application/json');
}
public function signIn(): ResponseInterface
{
$this->setupAuthClasses();
/**
* Attempts to verify the user's credentials
* through a POST request.
*/
$rules = [
'login' => 'required',
'password' => 'required'
];
$config = config('Myth\Auth\Config\Auth');
if ($config->validFields == ['email']) {
$rules['login'] .= '|valid_email';
};
if (!$this->validate($rules)) {
return $this->response->setJSON(['error' => $this->validator->getErrors()])
->setStatusCode(ResponseInterface::HTTP_NOT_ACCEPTABLE, lang('Authenticate.auth.validation'))
->setContentType('application/json');
}
$authEntity = new AuthEntity((array)$this->request->getVar());
$authEntity->logInMode();
$remember = $authEntity->remember ?? false;
if (!$this->authenticate->attempt([$authEntity->loginType => $authEntity->login, 'password' => $authEntity->password], $remember)) {
return $this->response->setJSON(['error' => $this->authenticate->error(),
])
->setStatusCode(ResponseInterface::HTTP_UNAUTHORIZED, lang('Auth.badAttempt'))
->setContentType('application/json');
}
// Is the user being forced to reset their password?
if ($this->authenticate->user()->force_pass_reset === true) {
// return redirect()->to(route_to('reset-password') . '?token=' . $this->auth->user()->reset_hash)->withCookies();
return $this->response->setJSON(['token' => $this->authenticate->user()->reset_hash])
->setStatusCode(ResponseInterface::HTTP_UNAUTHORIZED, lang('Authenticate.auth.foreResetPassword'))
->setContentType('application/json');
}
$authService = Services::authService();
$data = $authService->signIn($authEntity);
return $this->response->setJSON(
$data
)->setStatusCode(ResponseInterface::HTTP_OK, lang('Authenticate.auth.logIn'))
->setContentType('application/json');
}
/**
* Log the user out.
*/
public function signOut(): ResponseInterface
{
$this->setupAuthClasses();
$authConfig = new ModuleAuthConfig();
$jwtHeader = $this->request->getServer('HTTP_AUTHORIZATION');
$jwtCookie = $this->request->getCookie($authConfig->jwt['name']);
if ($this->authenticate->check()) {
$this->authenticate->logout();
} else if (!is_null($jwtHeader) || !is_null($jwtCookie)) {
$authService = Services::authService();
$requestWithUser = Services::requestWithUser();
$authService->signOutJwt($requestWithUser->getUser());
$this->response->setHeader($authConfig->jwt['name'], '');
$this->response->setCookie($authConfig->jwt['name'], '', 0);
}
return $this->response->setJSON(['success' => true])
->setStatusCode(ResponseInterface::HTTP_OK, lang('Authenticate.auth.logOut'))
->setContentType('application/json');
}
/**
* Log the user out.
*/
public function isSignIn(): ResponseInterface
{
$this->setupAuthClasses();
$requestWithUser = Services::requestWithUser();
if ($this->authenticate->check() || isset($requestWithUser->getUser()->id)) {
return $this->response->setJSON(['success' => true])
->setStatusCode(ResponseInterface::HTTP_OK, lang('check is sign in'))
->setContentType('application/json');
} else {
return $this->response->setJSON(['success' => false,
])
->setStatusCode(ResponseInterface::HTTP_OK, lang('check is sign in'))
->setContentType('application/json');
}
}
//
entity auth
{-code-2}
service auth
<?php namespace Modules\Auth\Services;
use Codeigniter\Config\Services;
use CodeIgniter\Session\SessionInterface;
use Config\Email;
use Modules\Auth\Config\ModuleAuthConfig;
use Modules\Auth\Entities\AuthEntity;
use Modules\Auth\Interfaces\AuthServiceInterface;
use Modules\Auth\Models\GroupsPermissionModel;
use Modules\Auth\Models\UsersPermissionModel;
use Modules\Shared\Config\ModuleSharedConfig;
use Modules\Shared\Libraries\MainService;
use Modules\Shared\Libraries\Sms;
use CodeIgniter\HTTP\ResponseInterface;
use Modules\Auth\Libraries\CsrfSecurity;
use Myth\Auth\Authorization\GroupModel;
use Myth\Auth\Authorization\PermissionModel;
use Myth\Auth\AuthTrait;
use Myth\Auth\Models\LoginModel;
use Myth\Auth\Models\UserModel;
use Myth\Auth\Password;
class AuthService extends MainService implements AuthServiceInterface
{
use AuthTrait;
private CsrfSecurity $csrfSecurity;
private GroupModel $groupModel;
private PermissionModel $permissionModel;
private ModuleAuthConfig $authConfig;
private UserModel $userModel;
private SessionInterface $session;
private LoginModel $loginModel;
private ModuleSharedConfig $sharedConfig;
private Sms $sms;
private GroupsPermissionModel $groupsPermissionModel;
private UsersPermissionModel $usersPermissionModel;
private \CodeIgniter\Email\Email $email;
public function __construct()
{
$this->userModel = new UserModel();
$this->authConfig = new ModuleAuthConfig();
$this->groupModel = new GroupModel();
$this->csrfSecurity = new CsrfSecurity();
$this->permissionModel = new PermissionModel();
$this->loginModel = new LoginModel();
$this->session = \Config\Services::session();
$this->sharedConfig = new ModuleSharedConfig();
$this->sms = new Sms($this->sharedConfig->sms['userName'],
$this->sharedConfig->sms['password'],
0);
$this->groupsPermissionModel = new GroupsPermissionModel();
$this->usersPermissionModel = new UsersPermissionModel();
$this->email = Services::email();
$emailConfig = new Email();
$this->email->initialize($emailConfig)->
setFrom($emailConfig->fromEmail, getenv('siteAddress'));
}
public function signInJwt(AuthEntity $entity): array
{
if (is_null($entity)) $this->httpException(lang('Shared.api.validation'), ResponseInterface::HTTP_CONFLICT);
$findUser = $this->userModel->asObject()->where($entity->loginType, $entity->login)->first();
if (!$findUser) {
$entity->loginSuccess(false);
$this->loginModel->save($entity);
$this->httpException(lang('Authenticate.auth.accountNotExist'), ResponseInterface::HTTP_CONFLICT);
}
if ($findUser->active == false) {
$entity->loginSuccess(false);
$this->loginModel->save($entity);
$this->httpException(lang('Authenticate.auth.accountNotActive'), ResponseInterface::HTTP_CONFLICT);
}
if ($findUser->status == true) {
$entity->loginSuccess(false);
$this->loginModel->save($entity);
$this->httpException(lang('Authenticate.auth.accountIsBand'), ResponseInterface::HTTP_CONFLICT);
}
if (!Password::verify($entity->password, $findUser->password_hash)) {
$entity->loginSuccess(false);
$this->loginModel->save($entity);
$this->httpException(lang('Authenticate.auth.accountNotExist'), ResponseInterface::HTTP_CONFLICT);
}
$entity->loginSuccess(true);
$this->loginModel->save($entity);
$role = $this->groupModel->asObject()->getGroupsForUser($findUser->id);
$permissions = $this->permissionModel->asObject()->where('active', '1')->findAll();
$permissionUser = $this->usersPermissionModel->permissionsOfUser($findUser->id);
$permissionGroup = $this->groupsPermissionModel->permissionsOfGroup($role[0]['group_id']);
helper('jwt');
$timeJwt = isset($entity->remember) ? timeJwt(true) : timeJwt(false);
$jwtToken = generateJWT($findUser->id, $timeJwt['init'], $timeJwt['expire'], $this->authConfig->jwt['secretKey']);
$data = [
'success' => true,
'role' => [
'name' => $role[0]['name'],
'id' => $role[0]['group_id']
],
'permissions' => $permissions,
'permissionUser' => $permissionUser,
'permissionGroup' => $permissionGroup,
'userInformation' => [
'id' => $findUser->id,
'userName' => $findUser->username,
'image' => $findUser->image,
'firstName' => $findUser->first_name,
'lastName' => $findUser->last_name,
'email' => $findUser->email,
'phone' => $findUser->phone,
],
'csrf' => $this->csrfSecurity->init(),
'jwt' => [
"token" => $jwtToken,
"expire" => $timeJwt['expire'],
],
];
return $data;
}
public function signIn(AuthEntity $entity): array
{
if (is_null($entity)) $this->httpException(lang('Shared.api.validation'), ResponseInterface::HTTP_CONFLICT);
helper('cookie');
//
$findUser = $this->userModel->asObject()->where($entity->loginType, $entity->login)->first();
$role = $this->groupModel->asObject()->getGroupsForUser($findUser->id);
$permissions = $this->permissionModel->asObject()->where('active', '1')->findAll();
$permissionUser = $this->usersPermissionModel->permissionsOfUser($findUser->id);
$permissionGroup = $this->groupsPermissionModel->permissionsOfGroup($findUser->id);
// store user inof in session
// $this->session->set('userInformation', [
// 'userName' => $this->authenticate->user()->username,
// 'image' => $this->authenticate->user()->image,
// 'firstName' => $this->authenticate->user()->first_name,
// 'lastName' => $this->authenticate->user()->last_name,
// 'email' => $this->authenticate->user()->email,
// 'phone' => $this->authenticate->user()->phone,
// ]);
$data = [
'success' => true,
'role' => [
'name' => $role[0]['name'],
'id' => $role[0]['group_id']
],
'permissions' => $permissions,
'permissionUser' => $permissionUser,
'permissionGroup' => $permissionGroup,
'userInformation' => [
'id' => $findUser->id,
'userName' => $findUser->username,
'image' => $findUser->image,
'firstName' => $findUser->first_name,
'lastName' => $findUser->last_name,
'email' => $findUser->email,
'phone' => $findUser->phone,
],
'csrf' => $this->csrfSecurity->init(),
];
return $data;
}
/**
* Log the user out.
* @param object $userData
*/
public function signOutJwt(object $userData): void
{
if (!isset($userData->id)) $this->httpException(lang('Authenticate.filter.jwt'), ResponseInterface::HTTP_CONFLICT);
$findUser = $this->userModel->asObject()->where("id", $userData->id)->first();
if (is_null($findUser)) $this->httpException(lang('Auth.forgotNoUser'), ResponseInterface::HTTP_UNAUTHORIZED);
}
//
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
CodeIgniter is a framework that is known for requiring a minimum amount of customization to get it up and running. This allows those who choose it to work at a good pace. It has been updated many times since its inception in 2006. Now the most recent version is 4.0.3.
https://www.codeigniter.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.