easyappointments/application/controllers/Login.php

94 lines
2.4 KiB
PHP
Raw Normal View History

<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
2022-01-18 15:05:42 +03:00
* Easy!Appointments - Online Appointment Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
2021-12-18 19:43:45 +03:00
* @copyright Copyright (c) Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.5.0
* ---------------------------------------------------------------------------- */
/**
* Login controller.
*
* Handles the login page functionality.
*
* @package Controllers
*/
class Login extends EA_Controller {
/**
* Login constructor.
*/
public function __construct()
{
parent::__construct();
$this->load->library('accounts');
$this->load->library('email_messages');
2021-12-14 10:06:10 +03:00
script_vars([
'dest_url' => session('dest_url', site_url('calendar')),
2021-12-14 10:06:10 +03:00
]);
}
/**
* Render the login page.
*/
public function index()
{
html_vars([
2022-10-16 18:02:47 +03:00
'page_title' => lang('login'),
'base_url' => config('base_url'),
'dest_url' => session('dest_url', site_url('calendar')),
2021-12-10 10:34:56 +03:00
'company_name' => setting('company_name')
]);
$this->load->view('pages/login');
}
/**
* Validate the provided credentials and start a new session if the validation was successful.
*/
public function validate()
{
try
{
$username = request('username');
if (empty($username))
{
throw new InvalidArgumentException('No username value provided.');
}
$password = request('password');
if (empty($password))
{
throw new InvalidArgumentException('No password value provided.');
}
$user_data = $this->accounts->check_login($username, $password);
if (empty($user_data))
{
throw new InvalidArgumentException('Invalid credentials provided, please try again.');
}
$this->session->sess_regenerate();
session($user_data); // Save data in the session.
json_response([
'success' => TRUE,
]);
}
catch (Throwable $e)
{
json_exception($e);
}
}
}