diff --git a/app/src/Controller/DashboardPage.php b/app/src/Controller/DashboardPage.php index 664ad85..201ed23 100644 --- a/app/src/Controller/DashboardPage.php +++ b/app/src/Controller/DashboardPage.php @@ -19,8 +19,6 @@ class DashboardPage extends Controller { // get the user details // get the channels - return $this->render('dashboard.twig', [ - 'user' => $this->session->get('user'), - ]); + return $this->render('dashboard.twig'); } } diff --git a/app/src/Controller/User/LoginUserAction.php b/app/src/Controller/User/LoginUserAction.php index 3be71df..2142923 100644 --- a/app/src/Controller/User/LoginUserAction.php +++ b/app/src/Controller/User/LoginUserAction.php @@ -45,7 +45,8 @@ class LoginUserAction extends Controller // start the session $this->session->getFlash()->add('success', "Successfully logged in."); - $this->session->set('user', [ 'id' => $user->getId(), 'authenticated' => true ]); + $this->session->set('authenticated', true); + $this->session->set('user', $user->toArray()); return $this->redirect('/dashboard', 302); } } diff --git a/app/src/Controller/User/LogoutUserAction.php b/app/src/Controller/User/LogoutUserAction.php index ee0fd3c..f114bc6 100644 --- a/app/src/Controller/User/LogoutUserAction.php +++ b/app/src/Controller/User/LogoutUserAction.php @@ -15,6 +15,10 @@ class LogoutUserAction extends Controller public function handle(): Response { + if ($this->session->has('authenticated')) { + $this->session->delete('authenticated'); + } + if ($this->session->has('user')) { $this->session->delete('user'); } diff --git a/app/src/Domain/Entity/User.php b/app/src/Domain/Entity/User.php index efdeab4..b3866af 100644 --- a/app/src/Domain/Entity/User.php +++ b/app/src/Domain/Entity/User.php @@ -79,4 +79,16 @@ class User return $user; } + + /** + * @return array + */ + public function toArray(): array + { + return [ + 'name' => $this->getName(), + 'email' => $this->getEmail(), + 'id' => $this->getId() ?? null, + ]; + } } diff --git a/app/src/Middleware/AuthenticatedMiddleware.php b/app/src/Middleware/AuthenticatedMiddleware.php index 14512b6..cca8fb5 100644 --- a/app/src/Middleware/AuthenticatedMiddleware.php +++ b/app/src/Middleware/AuthenticatedMiddleware.php @@ -18,9 +18,8 @@ class AuthenticatedMiddleware implements MiddlewareInterface public function process(Request $request, RequestHandler $handler): Response { - if ($this->session->has('user')) { - $userSession = $this->session->get('user'); - if ($userSession['authenticated']) { + if ($this->session->has('authenticated')) { + if ($this->session->get('authenticated')) { return $handler->handle($request); } } diff --git a/app/src/Routes.php b/app/src/Routes.php index 25e67c9..a978159 100644 --- a/app/src/Routes.php +++ b/app/src/Routes.php @@ -3,6 +3,7 @@ namespace Slovocast; use Slim\App; +use Slovocast\Controller\Channel\CreateChannelPage; use Slovocast\Controller\HomePage; use Slovocast\Controller\HealthCheck; use Slovocast\Controller\DashboardPage; @@ -23,11 +24,15 @@ class Routes */ public static function setup(App $app): void { - $app->get('/[home]', HomePage::class); + $app->get('/[home]', HomePage::class)->setName("home"); $app->get('/healthcheck', HealthCheck::class); + // User Routes self::users($app); self::dashboard($app); + + // Channel Routes + self::channels($app); } /** @@ -52,6 +57,17 @@ class Routes ->setName('user-logout'); } + /** + * @param App $app Instantiated Application + */ + protected static function channels(App $app): void + { + $app->get('/channel/create', CreateChannelPage::class) + ->add(FormKeyMiddleware::class) + ->add(AuthenticatedMiddleware::class) + ->setName('channel-create-page'); + } + /** * @param App $app Instance of the application */ @@ -59,6 +75,6 @@ class Routes { $app->get('/dashboard', DashboardPage::class) ->add(AuthenticatedMiddleware::class) - ->setName('user-dashboard'); + ->setName('dashboard'); } } diff --git a/app/templates/channel/create.twig b/app/templates/channel/create.twig index cf3f0af..52613ee 100644 --- a/app/templates/channel/create.twig +++ b/app/templates/channel/create.twig @@ -3,7 +3,8 @@ {% block content %}
- + +
-
diff --git a/app/templates/dashboard.twig b/app/templates/dashboard.twig index f5e19cc..2f88dda 100644 --- a/app/templates/dashboard.twig +++ b/app/templates/dashboard.twig @@ -2,6 +2,20 @@ {% block content %}
-

Dashboard

+

Hello, {{ session.get('user').name }}!

+
+
+ {% if channel %} +

{{ channel.name }}

+ + {% else %} + + {% endif %}
{% endblock %} diff --git a/app/templates/layouts/skeleton.twig b/app/templates/layouts/skeleton.twig index a61c076..d6187a1 100644 --- a/app/templates/layouts/skeleton.twig +++ b/app/templates/layouts/skeleton.twig @@ -6,25 +6,25 @@ {% endblock %} - + {% block head_js %}{% endblock %}
-

Slovocast

+

Slovocast

{% block header %}{% endblock %}