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 channels
return $this->render('dashboard.twig', [
'user' => $this->session->get('user'),
]);
return $this->render('dashboard.twig');
}
}

View File

@ -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);
}
}

View File

@ -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');
}

View File

@ -79,4 +79,16 @@ class 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
{
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);
}
}

View File

@ -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');
}
}

View File

@ -3,7 +3,8 @@
{% block content %}
<div>
<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>
<label for="name">Channel Name<br>
<input name="name" type="text" required>
@ -14,7 +15,7 @@
<textarea name="description"></textarea>
</div>
<div>
<button type="submit" label="Submit">
<button type="submit">Create</button>
</div>
</form>
</div>

View File

@ -2,6 +2,20 @@
{% block content %}
<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>
{% endblock %}

View File

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