Improved user management routes
diff --git a/lib/Kalamar.pm b/lib/Kalamar.pm
index 580ccf3..9cea0b1 100644
--- a/lib/Kalamar.pm
+++ b/lib/Kalamar.pm
@@ -83,7 +83,8 @@
'Search', # Abstract Search framework
'CHI', # Global caching mechanism
'TagHelpers::MailToChiffre', # Obfuscate email addresses
- 'KalamarHelpers' # Specific Helpers for Kalamar
+ 'KalamarHelpers', # Specific Helpers for Kalamar
+ 'KalamarUser' # Specific Helpers for Kalamar
) {
$self->plugin($_);
};
@@ -105,6 +106,7 @@
$r->get('/')->to('search#query')->name('index');
# Collection route
+ # TODO: Probably rename to /corpus
$r->get('/collection')->to('Search#collections')->name('collections');
$r->get('/collection/:id')->to('Search#collection')->name('collection');
@@ -124,8 +126,12 @@
my $match = $text->get('/:match_id');
$match->to('search#match_info')->name('match');
- $r->post('/login')->to('User#login');
- $r->post('/logout')->to('User#logout');
+ # User Management
+ $r->any('/user')->to(controller => 'User');
+ $r->post('/login')->to(action => 'login')->name('login');
+ $r->get('/logout')->to(action => 'logout')->name('logout');
+ $r->any('/register')->to(action => 'register')->name('register');
+ $r->any('/forgotten')->to(action => 'pwdforgotten')->name('pwdforgotten');
# Default user is called 'korap'
# $r->route('/user/:user/:collection')
diff --git a/lib/Kalamar/Controller/User.pm b/lib/Kalamar/Controller/User.pm
index 691d648..f59f932 100644
--- a/lib/Kalamar/Controller/User.pm
+++ b/lib/Kalamar/Controller/User.pm
@@ -3,11 +3,34 @@
sub login {
my $c = shift;
+ my $v = $c->validator;
+ $v->required('handle_or_email');
+ $v->required('pwd');
+
+ my $handle = $v->param('handle_or_email');
+ my $pwd = $v->param('pwd');
+
+ $c->user->login($handle, $pwd);
+
+ return $c->redirect_to;
+};
+
+sub logout {
+ shift->user->logout;
+};
+
+sub register {
+ my $c = shift;
$c->render(json => {
- response => 'logged in'
+ response => 'register'
});
};
-sub logout {};
+sub pwdforgotten {
+ my $c = shift;
+ $c->render(json => {
+ response => 'pwdforgotten'
+ });
+};
1;
diff --git a/lib/Kalamar/Plugin/KalamarHelpers.pm b/lib/Kalamar/Plugin/KalamarHelpers.pm
index 97e3d55..8ae6c1e 100644
--- a/lib/Kalamar/Plugin/KalamarHelpers.pm
+++ b/lib/Kalamar/Plugin/KalamarHelpers.pm
@@ -106,7 +106,7 @@
my $scope = shift;
# Create unordered list
- my $html = "<ul>\n";
+ my $html = '<ul class="nav">'."\n";
# Embed all link tags
foreach (@$items) {
diff --git a/lib/Kalamar/Plugin/KalamarUser.pm b/lib/Kalamar/Plugin/KalamarUser.pm
index 209b8cb..8d12d30 100644
--- a/lib/Kalamar/Plugin/KalamarUser.pm
+++ b/lib/Kalamar/Plugin/KalamarUser.pm
@@ -54,6 +54,7 @@
# Login successful
if (my $res = $tx->success) {
+
my $jwt = $res->json;
my $auth = $jwt->{token_type} . ' ' . $jwt->{token};
@@ -92,6 +93,7 @@
my $c = shift;
my $param = shift;
+ # 'info' is useless!
return unless $param =~ m/^details|settings$/;
# The user may be logged in
@@ -110,14 +112,16 @@
my $tx = $plugin->build_authorized_tx($auth, 'GET', 'user/' . $param);
$tx = $plugin->ua->start($tx);
+
unless ($value = $tx->success) {
-# warn $tx->code;
return;
}
# else {
# warn $c->dumper($value->json);
# };
- $value = $value->json;
+ if ($value) {
+ $value = $value->json;
+ };
$chi->set($user . '_' . $param => $value);
};
@@ -127,6 +131,42 @@
}
);
+ $mojo->helper(
+ 'user.set' => sub {
+ my $c = shift;
+ my $param = shift;
+
+ # 'info' is useless!
+ return unless $param =~ m/^details|settings$/;
+
+ my $json_obj = shift;
+
+ # The user may be logged in
+ my $auth = ($c->stash('auth') || $c->session('auth')) or return;
+
+ # Get namespaced cache
+ my $chi = $c->chi('user');
+
+ # Get user and check, if the user is real
+ my $user = $chi->get($auth);
+
+ # Build a JSON transaction object
+ my $tx = $plugin->build_authorized_tx(
+ $auth, 'POST', 'user/' . $param, json => $json_obj
+ );
+
+ # Start
+ $tx = $plugin->ua->start($tx);
+
+ my $res = $tx->success or return;
+
+ # Kill all caches!!
+ $chi->remove($user . '_' . $param);
+
+ # Return value
+ return $res->json;
+ }
+ );
# Logout
$mojo->helper(
@@ -162,6 +202,7 @@
my $url = Mojo::URL->new($plugin->api)->path($path);
+
$header->{Authorization} = $auth;
return $ua->build_tx($method, $url => $header => @values);