This commit is contained in:
47
app/Controllers/Auth/LoginController.php
Normal file
47
app/Controllers/Auth/LoginController.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers\Auth;
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
use App\Models\UserModel;
|
||||
|
||||
class LoginController extends BaseController
|
||||
{
|
||||
public function showLoginForm()
|
||||
{
|
||||
helper(['form']);
|
||||
echo view('auth/login');
|
||||
}
|
||||
|
||||
public function login()
|
||||
{
|
||||
$email = $this->request->getVar('email');
|
||||
$password = $this->request->getVar('password');
|
||||
|
||||
$user = new UserModel();
|
||||
$data = $user->where('email', $email)->first();
|
||||
|
||||
if ($data) {
|
||||
$pass = $data['password'];
|
||||
$authenticatePassword = password_verify($password, $pass);
|
||||
if ($authenticatePassword) {
|
||||
$authSession = new SessionConroller();
|
||||
$authSession->authorised($data);
|
||||
return redirect()->route('/');
|
||||
} else {
|
||||
$session->setFlashdata('msg', 'Password is incorrect.');
|
||||
return redirect()->back()->withInput();
|
||||
}
|
||||
} else {
|
||||
$session->setFlashdata('msg', 'Email does not exist.');
|
||||
return redirect()->back()->withInput();
|
||||
}
|
||||
}
|
||||
|
||||
public function logout()
|
||||
{
|
||||
$authSession = new SessionConroller();
|
||||
$authSession->unauthorised();
|
||||
return redirect()->route('/');
|
||||
}
|
||||
}
|
||||
13
app/Controllers/Auth/ProfileController.php
Normal file
13
app/Controllers/Auth/ProfileController.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers\Auth;
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
|
||||
class ProfileController extends BaseController
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
echo view('auth/profile');
|
||||
}
|
||||
}
|
||||
45
app/Controllers/Auth/RegisterController.php
Normal file
45
app/Controllers/Auth/RegisterController.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers\Auth;
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
use App\Models\UserModel;
|
||||
|
||||
class RegisterController extends BaseController
|
||||
{
|
||||
public function showRegistrationForm()
|
||||
{
|
||||
helper(['form']);
|
||||
$data = [];
|
||||
echo view('auth/register', $data);
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
helper(['form']);
|
||||
$rules = [
|
||||
'name' => 'required|min_length[2]|max_length[50]',
|
||||
'email' => 'required|min_length[4]|max_length[100]|valid_email|is_unique[users.email]',
|
||||
'password' => 'required|min_length[4]|max_length[50]',
|
||||
'confirmpassword' => 'matches[password]'
|
||||
];
|
||||
|
||||
if ($this->validate($rules)) {
|
||||
$user = new userModel();
|
||||
$data = [
|
||||
'name' => $this->request->getVar('name'),
|
||||
'email' => $this->request->getVar('email'),
|
||||
'password' => password_hash($this->request->getVar('password'), PASSWORD_DEFAULT)
|
||||
];
|
||||
|
||||
$user->save($data);
|
||||
$authSession = new SessionConroller();
|
||||
$authSession->authorised($data);
|
||||
|
||||
return redirect()->to('/');
|
||||
} else {
|
||||
$data['validation'] = $this->validator;
|
||||
echo view('auth/register', $data);
|
||||
}
|
||||
}
|
||||
}
|
||||
28
app/Controllers/Auth/SessionConroller.php
Normal file
28
app/Controllers/Auth/SessionConroller.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers\Auth;
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
|
||||
class SessionConroller extends BaseController
|
||||
{
|
||||
public function authorised($data)
|
||||
{
|
||||
$session = session();
|
||||
$authData = [
|
||||
'name' => $data['name'],
|
||||
'email' => $data['email'],
|
||||
'isLoggedIn' => true
|
||||
];
|
||||
|
||||
$session->set($authData);
|
||||
}
|
||||
|
||||
public function unauthorised()
|
||||
{
|
||||
$session = session();
|
||||
$authData = ['isLoggedIn' => false];
|
||||
|
||||
$session->set($authData);
|
||||
}
|
||||
}
|
||||
52
app/Controllers/BaseController.php
Normal file
52
app/Controllers/BaseController.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use CodeIgniter\Controller;
|
||||
use CodeIgniter\HTTP\CLIRequest;
|
||||
use CodeIgniter\HTTP\IncomingRequest;
|
||||
use CodeIgniter\HTTP\RequestInterface;
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Class BaseController
|
||||
*
|
||||
* BaseController provides a convenient place for loading components
|
||||
* and performing functions that are needed by all your controllers.
|
||||
* Extend this class in any new controllers:
|
||||
* class Home extends BaseController
|
||||
*
|
||||
* For security be sure to declare any new methods as protected or private.
|
||||
*/
|
||||
abstract class BaseController extends Controller
|
||||
{
|
||||
/**
|
||||
* Instance of the main Request object.
|
||||
*
|
||||
* @var CLIRequest|IncomingRequest
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* An array of helpers to be loaded automatically upon
|
||||
* class instantiation. These helpers will be available
|
||||
* to all other controllers that extend BaseController.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $helpers = [];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
||||
{
|
||||
// Do Not Edit This Line
|
||||
parent::initController($request, $response, $logger);
|
||||
|
||||
// Preload any models, libraries, etc, here.
|
||||
|
||||
// E.g.: $this->session = \Config\Services::session();
|
||||
}
|
||||
}
|
||||
19
app/Controllers/Home.php
Normal file
19
app/Controllers/Home.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
class Home extends BaseController
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$session = session();
|
||||
|
||||
$data = [
|
||||
'name' => $session->get('name'),
|
||||
'email' => $session->get('email'),
|
||||
'authorised' => $session->get('isLoggedIn'),
|
||||
];
|
||||
|
||||
return view('home', $data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user