From 9fc32639442670e0a249aa8a6ff11831aa2167a4 Mon Sep 17 00:00:00 2001 From: Al Beano Date: Sat, 15 Jul 2017 22:08:11 +0100 Subject: [PATCH] Working sessions stored in sqlite --- lib/cyberman.pm | 26 ++++++++++++++++++++++---- schema.sql | 2 +- views/layouts/main.tt | 3 +++ views/redir.tt | 3 +++ 4 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 views/redir.tt diff --git a/lib/cyberman.pm b/lib/cyberman.pm index 11e8735..72f183a 100644 --- a/lib/cyberman.pm +++ b/lib/cyberman.pm @@ -7,6 +7,7 @@ use Math::Random::Secure qw(rand irand); ##### # cyberman.pm # index page and authentication +# maybe this could be split into another file at a later juncture ##### # misc authentication subs @@ -49,7 +50,7 @@ hook 'before' => sub { } } - my $uid = cookieval("token"); + my $uid = cookieval("id"); my $token = cookieval("token"); my $auth = 0; if ($uid && $token) { @@ -59,10 +60,11 @@ hook 'before' => sub { var auth => $auth; }; -get '/' => sub { +get qr{^/(index)?$} => sub { if (!vars->{auth}) { - template 'index'; + return template 'index'; } + return "well done, you logged in, nothing to see yet"; }; post '/register' => sub { @@ -155,7 +157,23 @@ post '/login' => sub { # checks finished, we can create a session now - return; + my $token = randstring(32); + + database->quick_insert( + "session", + { + "token" => $token, + "uid" => $user->{"id"}, + "since" => time, + }, + ); + + cookie id => $user->{"id"}; + cookie token => $token; + + template redir => { + "redir" => "index", + }; }; true; diff --git a/schema.sql b/schema.sql index c65922c..ff6de9c 100644 --- a/schema.sql +++ b/schema.sql @@ -10,7 +10,7 @@ create table user ( drop table if exists session; create table session ( id integer primary key, - uid text not null, + uid integer not null, since integer not null, token text not null ); diff --git a/views/layouts/main.tt b/views/layouts/main.tt index 7b80575..145f206 100644 --- a/views/layouts/main.tt +++ b/views/layouts/main.tt @@ -3,6 +3,9 @@ + <% IF redir %> + + <% END %> cybNIC