Working sessions stored in sqlite

This commit is contained in:
Al Beano 2017-07-15 22:08:11 +01:00
parent 8f341f15a2
commit 9fc3263944
4 changed files with 29 additions and 5 deletions

View File

@ -7,6 +7,7 @@ use Math::Random::Secure qw(rand irand);
##### #####
# cyberman.pm # cyberman.pm
# index page and authentication # index page and authentication
# maybe this could be split into another file at a later juncture
##### #####
# misc authentication subs # misc authentication subs
@ -49,7 +50,7 @@ hook 'before' => sub {
} }
} }
my $uid = cookieval("token"); my $uid = cookieval("id");
my $token = cookieval("token"); my $token = cookieval("token");
my $auth = 0; my $auth = 0;
if ($uid && $token) { if ($uid && $token) {
@ -59,10 +60,11 @@ hook 'before' => sub {
var auth => $auth; var auth => $auth;
}; };
get '/' => sub { get qr{^/(index)?$} => sub {
if (!vars->{auth}) { if (!vars->{auth}) {
template 'index'; return template 'index';
} }
return "well done, you logged in, nothing to see yet";
}; };
post '/register' => sub { post '/register' => sub {
@ -155,7 +157,23 @@ post '/login' => sub {
# checks finished, we can create a session now # 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; true;

View File

@ -10,7 +10,7 @@ create table user (
drop table if exists session; drop table if exists session;
create table session ( create table session (
id integer primary key, id integer primary key,
uid text not null, uid integer not null,
since integer not null, since integer not null,
token text not null token text not null
); );

View File

@ -3,6 +3,9 @@
<head> <head>
<meta charset="utf8" /> <meta charset="utf8" />
<meta name="viewport" content="width=device-width" /> <meta name="viewport" content="width=device-width" />
<% IF redir %>
<meta http-equiv="refresh" content="0;<% redir %>" />
<% END %>
<title>cybNIC</title> <title>cybNIC</title>
<style> <style>
* { * {

3
views/redir.tt Normal file
View File

@ -0,0 +1,3 @@
<div class="body">
Redirecting, please wait.
</div>