Use the url_for method, skeleton changes and dashboard fill out. Still not getting flash messages but am getting session data.

This commit is contained in:
Dave Smith-Hayes 2024-12-03 22:01:31 -05:00
parent d45800cfc4
commit 5b15aeea4e
9 changed files with 64 additions and 19 deletions

View File

@ -19,8 +19,6 @@ class DashboardPage extends Controller
{ {
// get the user details // get the user details
// get the channels // get the channels
return $this->render('dashboard.twig', [ return $this->render('dashboard.twig');
'user' => $this->session->get('user'),
]);
} }
} }

View File

@ -45,7 +45,8 @@ class LoginUserAction extends Controller
// start the session // start the session
$this->session->getFlash()->add('success', "Successfully logged in."); $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); return $this->redirect('/dashboard', 302);
} }
} }

View File

@ -15,6 +15,10 @@ class LogoutUserAction extends Controller
public function handle(): Response public function handle(): Response
{ {
if ($this->session->has('authenticated')) {
$this->session->delete('authenticated');
}
if ($this->session->has('user')) { if ($this->session->has('user')) {
$this->session->delete('user'); $this->session->delete('user');
} }

View File

@ -79,4 +79,16 @@ class User
return $user; return $user;
} }
/**
* @return array<string,mixed>
*/
public function toArray(): array
{
return [
'name' => $this->getName(),
'email' => $this->getEmail(),
'id' => $this->getId() ?? null,
];
}
} }

View File

@ -18,9 +18,8 @@ class AuthenticatedMiddleware implements MiddlewareInterface
public function process(Request $request, RequestHandler $handler): Response public function process(Request $request, RequestHandler $handler): Response
{ {
if ($this->session->has('user')) { if ($this->session->has('authenticated')) {
$userSession = $this->session->get('user'); if ($this->session->get('authenticated')) {
if ($userSession['authenticated']) {
return $handler->handle($request); return $handler->handle($request);
} }
} }

View File

@ -3,6 +3,7 @@
namespace Slovocast; namespace Slovocast;
use Slim\App; use Slim\App;
use Slovocast\Controller\Channel\CreateChannelPage;
use Slovocast\Controller\HomePage; use Slovocast\Controller\HomePage;
use Slovocast\Controller\HealthCheck; use Slovocast\Controller\HealthCheck;
use Slovocast\Controller\DashboardPage; use Slovocast\Controller\DashboardPage;
@ -23,11 +24,15 @@ class Routes
*/ */
public static function setup(App $app): void public static function setup(App $app): void
{ {
$app->get('/[home]', HomePage::class); $app->get('/[home]', HomePage::class)->setName("home");
$app->get('/healthcheck', HealthCheck::class); $app->get('/healthcheck', HealthCheck::class);
// User Routes // User Routes
self::users($app); self::users($app);
self::dashboard($app); self::dashboard($app);
// Channel Routes
self::channels($app);
} }
/** /**
@ -52,6 +57,17 @@ class Routes
->setName('user-logout'); ->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 * @param App $app Instance of the application
*/ */
@ -59,6 +75,6 @@ class Routes
{ {
$app->get('/dashboard', DashboardPage::class) $app->get('/dashboard', DashboardPage::class)
->add(AuthenticatedMiddleware::class) ->add(AuthenticatedMiddleware::class)
->setName('user-dashboard'); ->setName('dashboard');
} }
} }

View File

@ -3,7 +3,8 @@
{% block content %} {% block content %}
<div> <div>
<form action="/channel/create" method="post"> <form action="/channel/create" method="post">
<input name="form_key" type="hidden" value="{{ form_key }}"> <input name="form_key" type="hidden" value="{{ session.get('form_key') }}">
<input name="user_id" type="hidden" value="{{ session.get('user').getId() }}">
<div> <div>
<label for="name">Channel Name<br> <label for="name">Channel Name<br>
<input name="name" type="text" required> <input name="name" type="text" required>
@ -14,7 +15,7 @@
<textarea name="description"></textarea> <textarea name="description"></textarea>
</div> </div>
<div> <div>
<button type="submit" label="Submit"> <button type="submit">Create</button>
</div> </div>
</form> </form>
</div> </div>

View File

@ -2,6 +2,20 @@
{% block content %} {% block content %}
<div> <div>
<h1>Dashboard</h1> <h1>Hello, {{ session.get('user').name }}!</h1>
</div>
<div>
{% if channel %}
<h2>{{ channel.name }}</h2>
<ul>
<li>Add Episode</li>
<li>Manage Episodes</li>
<li>Generate Feed</li>
</ul>
{% else %}
<ul>
<li><a href="/channel/create">Create Channel</a></li>
</ul>
{% endif %}
</div> </div>
{% endblock %} {% endblock %}

View File

@ -6,25 +6,25 @@
<meta name="description" value="{{ site_description }}"> <meta name="description" value="{{ site_description }}">
<meta name="keywords" value="{{ page_tags }}"> <meta name="keywords" value="{{ page_tags }}">
{% endblock %} {% endblock %}
<link rel="stylesheet" href="static/main.css"> <link rel="stylesheet" href="/static/main.css">
{% block head_js %}{% endblock %} {% block head_js %}{% endblock %}
</head> </head>
<body> <body>
<div class="container"> <div class="container">
<header> <header>
<h1><a href="/home">Slovocast</a></h1> <h1><a href="{{ url_for("home") }}">Slovocast</a></h1>
{% block header %}{% endblock %} {% block header %}{% endblock %}
</header> </header>
<nav> <nav>
<ul> <ul>
{% if session.get('user').authenticated %} {% if session.get('authenticated') %}
<li><a href="/dashboard">Dashboard</a></li> <li><a href="{{ url_for("dashboard") }}">Dashboard</a></li>
<li><a href="/logout">Logout</a></li> <li><a href="{{ url_for("user-logout") }}">Logout</a></li>
{% else %} {% else %}
<li><a href="/login">Login</a></li> <li><a href="{{ url_for("user-login-page") }}">Login</a></li>
<li><a href="/register">Register</a></li> <li><a href="{{ url_for("user-register-page") }}">Register</a></li>
{% endif %} {% endif %}
</ul> </ul>
</nav> </nav>