Skip to content

Commit 704f050

Browse files
committedFeb 4, 2025
Tweaks, bug fixes, and simplifications
- Removed use of hook to simplify tutorial - Added parameter types to all relevant routes - Other minor improvements and fixes
1 parent a446c7c commit 704f050

File tree

2 files changed

+9
-47
lines changed

2 files changed

+9
-47
lines changed
 

‎lib/DLBlog.pm

+8-46
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,6 @@ use Dancer2::Plugin::Auth::Tiny;
55
use Dancer2::Plugin::CryptPassphrase;
66
use Feature::Compat::Try;
77

8-
=pod
9-
10-
Running list of updates to the tutorial
11-
12-
cpanm \
13-
Dancer2::Plugin::Auth::Tiny \
14-
Dancer2::Plugin::CryptPassphrase
15-
16-
Then add those to cpanfile
17-
18-
Create users:
19-
20-
sqlite3 db/dlblog.db < db/users.sql
21-
22-
Then rerun:
23-
24-
dbicdump -o dump_directory=./lib \
25-
-o components='["InflateColumn::DateTime"]' \
26-
DLBlog::Schema dbi:SQLite:db/dlblog.db '{ quote_char => "\"" }'
27-
28-
Explain that Auth::Tiny has other features, but we configured it
29-
minimally.
30-
31-
Use sessions in auth only.
32-
33-
TODO: README
34-
35-
Create a new password:
36-
37-
perl -MCrypt::Passphrase -E'my $auth=Crypt::Passphrase->new(encoder=>"Argon2"); say $auth->hash_password("test")'
38-
39-
=cut
40-
418
get '/' => sub {
429
# Give us the most recent first
4310
my @entries = resultset('Entry')->search(
@@ -50,12 +17,12 @@ get '/' => sub {
5017
get '/entry/:id[Int]' => sub {
5118
my $id = route_parameters->get('id');
5219
my $entry = resultset( 'Entry' )->find( $id );
53-
template 'entry', { entry => $entry, page_title => $entry->title };
20+
template 'entry', { entry => $entry };
5421
};
5522

5623
get '/create' => needs login => sub {
5724
# Vars are passed to templates automatically
58-
template 'create_update', { post_to => uri_for '/create', page_title => 'Create Entry' };
25+
template 'create_update', { post_to => uri_for '/create' };
5926
};
6027

6128
post '/create' => needs login => sub {
@@ -88,7 +55,7 @@ get '/update/:id[Int]' => needs login => sub {
8855
my $id = route_parameters->get('id');
8956
my $entry = resultset( 'Entry' )->find( $id );
9057
var $_ => $entry->$_ foreach qw< title summary content >;
91-
template 'create_update', { post_to => uri_for "/update/$id", page_title => "Edit Entry $id" };
58+
template 'create_update', { post_to => uri_for "/update/$id" };
9259
};
9360

9461
post '/update/:id[Int]' => needs login => sub {
@@ -121,13 +88,13 @@ post '/update/:id[Int]' => needs login => sub {
12188
redirect uri_for "/entry/" . $entry->id; # redirect does not need a return
12289
};
12390

124-
get '/delete/:id' => needs login => sub {
91+
get '/delete/:id[Int]' => needs login => sub {
12592
my $id = route_parameters->get('id');
12693
my $entry = resultset( 'Entry' )->find( $id );
127-
template 'delete', { entry => $entry, page_title => "Delete Entry $id" };
94+
template 'delete', { entry => $entry };
12895
};
12996

130-
post '/delete/:id' => needs login => sub {
97+
post '/delete/:id[Int]' => needs login => sub {
13198
my $id = route_parameters->get('id');
13299
my $entry = resultset( 'Entry' )->find( $id );
133100
if( !$entry ) {
@@ -148,7 +115,7 @@ post '/delete/:id' => needs login => sub {
148115
};
149116

150117
get '/login' => sub {
151-
template 'login' => { return_url => params->{ return_url }, page_title => 'Login' };
118+
template 'login' => { return_url => params->{ return_url } };
152119
};
153120

154121
post '/login' => sub {
@@ -158,8 +125,8 @@ post '/login' => sub {
158125

159126
my $user = resultset( 'User' )->find({ username => $username });
160127
if ( $user and verify_password( $password, $user->password) ) {
161-
session user => $username;
162128
app->change_session_id;
129+
session user => $username;
163130
info "$username successfully logged in";
164131
return redirect $return_url || '/';
165132
}
@@ -174,10 +141,5 @@ get '/logout' => sub {
174141
redirect uri_for "/";
175142
};
176143

177-
hook before_layout_render => sub {
178-
my ($tokens, $ref_content) = @_;
179-
$tokens->{ page_title } = 'Welcome!' unless $tokens->{ page_title };
180-
};
181-
182144
true;
183145

‎views/layouts/main.tt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="<% settings.charset %>">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
6-
<title>Danceyland Blog: <% page_title | html_entity %></title>
6+
<title>Danceyland Blog</title>
77
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
88
</head>
99
<body>

0 commit comments

Comments
 (0)
Please sign in to comment.