mirror of
https://github.com/kevinveenbirkenbach/coding-challenge-online-shop.git
synced 2024-11-01 00:53:10 +01:00
Implemented login
This commit is contained in:
parent
80be8e7361
commit
a444f4805c
@ -2,6 +2,7 @@
|
||||
namespace controller;
|
||||
|
||||
use core\CoreInterface;
|
||||
use router\Router;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -37,4 +38,11 @@ abstract class AbstractController
|
||||
$variables['user'] = $this->core->getUser();
|
||||
return $variables;
|
||||
}
|
||||
|
||||
protected function route(?array $get =[]):void{
|
||||
$router = new Router();
|
||||
$router->setCore($this->core);
|
||||
$router->setGet($get);
|
||||
$router->route();
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,10 @@ namespace controller\user;
|
||||
|
||||
use controller\AbstractDefaultController;
|
||||
use router\Router;
|
||||
use controller\AbstractController;
|
||||
use core\CoreInterface;
|
||||
use repository\user\User as UserRepository;
|
||||
use entity\user\User as UserEntity;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -11,24 +15,50 @@ use router\Router;
|
||||
*/
|
||||
final class User extends AbstractDefaultController implements UserInterface
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @var UserRepository
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
public function __construct(CoreInterface $core)
|
||||
{
|
||||
parent::__construct($core);
|
||||
$this->repository = new UserRepository($core);
|
||||
}
|
||||
|
||||
public function logout(): void
|
||||
{
|
||||
$this->core->setUser(null);
|
||||
$router = new Router();
|
||||
$router->setCore($this->core);
|
||||
$router->setGet([]);
|
||||
$router->route();
|
||||
$this->route();
|
||||
}
|
||||
|
||||
public function login(): void
|
||||
{
|
||||
$this->render('user/login.html.twig');
|
||||
if ($this->post) {
|
||||
try {
|
||||
$this->loginRoutine();
|
||||
} catch (\Exception $exception) {
|
||||
$this->render('frames/exception.html.twig',['message'=>$exception->getMessage()]);
|
||||
}
|
||||
} else {
|
||||
$this->render('user/login.html.twig');
|
||||
}
|
||||
}
|
||||
|
||||
private function loginRoutine(): void
|
||||
{
|
||||
$requestedUser = new UserEntity();
|
||||
$requestedUser->setPasswordHashByPassword($this->post['password']);
|
||||
$requestedUser->setEmail($this->post['email']);
|
||||
$this->core->setUser($this->repository->getUserByMailAndHash($requestedUser));
|
||||
$this->route();
|
||||
}
|
||||
|
||||
public function register(): void
|
||||
{
|
||||
$this->render('user/register.html.twig');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -56,12 +56,11 @@ final class User implements UserInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* In a real application you should use a salt ;)
|
||||
* @param string $password
|
||||
*/
|
||||
public function setPasswordHashByPassword(string $password): void
|
||||
{
|
||||
$this->passwordHash = password_hash($password, PASSWORD_BCRYPT);
|
||||
$this->passwordHash = $this->hashPassword($password);
|
||||
}
|
||||
|
||||
public function getEmail(): string
|
||||
@ -77,6 +76,17 @@ final class User implements UserInterface
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* In a real application you should use a salt ;)
|
||||
* @param string $password
|
||||
* @return string
|
||||
*/
|
||||
private function hashPassword(string $password): string
|
||||
{
|
||||
return password_hash($password, PASSWORD_BCRYPT,['salt' => '1235813471123581347112358134']);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,9 @@ class UserTest extends TestCase
|
||||
|
||||
const EMAIL = 'test@mail.world';
|
||||
|
||||
const HASH = '1235';
|
||||
const HASH = '$2y$10$123581347112358134711urUo63Gbn3BFTEe9UGbPxJwrZ80q.LbK';
|
||||
|
||||
const PASSWORD = 'passwort:)';
|
||||
|
||||
const ID = 5678;
|
||||
|
||||
@ -27,7 +29,7 @@ class UserTest extends TestCase
|
||||
$this->user = new User();
|
||||
$this->user->setName(self::NAME);
|
||||
$this->user->setEmail(self::EMAIL);
|
||||
$this->user->setPasswordHash(self::HASH);
|
||||
$this->user->setPasswordHashByPassword(self::PASSWORD);
|
||||
$this->user->setId(self::ID);
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@ namespace repository\user;
|
||||
|
||||
use repository\AbstractRepository;
|
||||
use entity\user\UserInterface as UserEntityInterface;
|
||||
use entity\user\User as UserEntity;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -22,7 +23,27 @@ final class User extends AbstractRepository implements UserInterface
|
||||
]);
|
||||
}
|
||||
|
||||
public function getUserByMailAndHash(string $mail, string $hash): UserEntityInterface
|
||||
{}
|
||||
public function getUserByMailAndHash(UserEntityInterface $user): UserEntityInterface
|
||||
{
|
||||
$statement = $this->database->prepare('SELECT * FROM `user` WHERE `hash` = ? AND `email`=?;');
|
||||
$statement->execute([
|
||||
$user->getPasswordHash(),
|
||||
$user->getEmail(),
|
||||
]);
|
||||
$result = $statement->fetch();
|
||||
if($result){
|
||||
return $this->fetchToUser($result);
|
||||
}
|
||||
throw new \Exception('Verification data is not valid!');
|
||||
}
|
||||
|
||||
private function fetchToUser(array $fetch):UserEntityInterface{
|
||||
$user = new UserEntity();
|
||||
$user->setPasswordHash($fetch['hash']);
|
||||
$user->setName($fetch['name']);
|
||||
$user->setEmail($fetch['email']);
|
||||
$user->setId($fetch['id']);
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,6 +12,6 @@ interface UserInterface
|
||||
{
|
||||
public function addUser(UserEntityInterface $user):void;
|
||||
|
||||
public function getUserByMailAndHash(string $mail,string $hash):UserEntityInterface;
|
||||
public function getUserByMailAndHash(UserEntityInterface $user):UserEntityInterface;
|
||||
}
|
||||
|
||||
|
5
src/template/frames/exception.html.twig
Normal file
5
src/template/frames/exception.html.twig
Normal file
@ -0,0 +1,5 @@
|
||||
{% extends "frames/default.html.twig" %}
|
||||
{% block content %}
|
||||
<h1>Error!</h1>
|
||||
<p>{{ message }}</p>
|
||||
{% endblock %}
|
@ -2,14 +2,14 @@
|
||||
{% block title %}login{% endblock %}
|
||||
{% block content %}
|
||||
<h1>Login</h1>
|
||||
<form>
|
||||
<form method='post'>
|
||||
<div class="form-group">
|
||||
<label for="exampleInputEmail1">Email address</label>
|
||||
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
|
||||
<label for="email">Email address</label>
|
||||
<input type="email" name="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="exampleInputPassword1">Password</label>
|
||||
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
|
||||
<label for="password">Password</label>
|
||||
<input type="password" name="password" class="form-control" id="password" placeholder="Password">
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Submit</button>
|
||||
</form>
|
||||
|
Loading…
Reference in New Issue
Block a user