diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index ccab394..d1d20c5 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -48,10 +48,9 @@ sub startup { my $r = $self->routes; - $r->any('/')->to('root#index'); + $r->get('/')->to('root#index'); + $r->post('/')->to('root#auth_login'); $r->any('/logout')->to('root#auth_logout'); - $r->get('/admin')->to('admin#index'); - $r->post('/admin')->to('admin#login'); my $api = $r->under('/api' => sub { my $c = shift; @@ -101,13 +100,17 @@ sub startup { my $admin_routes = $r->under('/admin')->to('admin#under'); - $admin_routes->get('/home')->to('admin#home'); + $admin_routes->get('/')->to('admin#home'); $admin_routes->get('/tokens')->to('admin-tokens#index'); $admin_routes->post('/tokens')->to('admin-tokens#create'); $admin_routes->get('/tokens/:id')->to('admin-tokens#read'); $admin_routes->post('/tokens/:id')->to('admin-tokens#update'); $admin_routes->post('/tokens/:id/delete')->to('admin-tokens#delete'); + my $user_routes = $r->under('/')->to('root#under'); + + $user_routes->get('/home')->to('root#home'); + $self->hook( before_dispatch => sub { my $self = shift; diff --git a/lib/Pear/LocalLoop/Controller/Admin.pm b/lib/Pear/LocalLoop/Controller/Admin.pm index 4747677..75972e6 100644 --- a/lib/Pear/LocalLoop/Controller/Admin.pm +++ b/lib/Pear/LocalLoop/Controller/Admin.pm @@ -1,28 +1,16 @@ package Pear::LocalLoop::Controller::Admin; use Mojo::Base 'Mojolicious::Controller'; -sub index { - my $c = shift; -} - sub under { my $c = shift; if ( $c->is_user_authenticated ) { return 1 if defined $c->current_user->administrator; - } - $c->redirect_to('/'); - return undef; -} - -sub login { - my $c = shift; - - if ( $c->authenticate($c->param('email'), $c->param('password')) ) { - $c->redirect_to('/admin/home'); + $c->redirect_to('/home'); } else { - $c->redirect_to('/admin'); + $c->redirect_to('/'); } + return undef; } sub home { diff --git a/lib/Pear/LocalLoop/Controller/Root.pm b/lib/Pear/LocalLoop/Controller/Root.pm index 8de9259..a99bb28 100644 --- a/lib/Pear/LocalLoop/Controller/Root.pm +++ b/lib/Pear/LocalLoop/Controller/Root.pm @@ -3,7 +3,26 @@ use Mojo::Base 'Mojolicious::Controller'; sub index { my $c = shift; +} +sub under { + my $c = shift; + + if ( $c->is_user_authenticated ) { + return 1; + } + $c->redirect_to('/'); + return undef; +} + +sub auth_login { + my $c = shift; + + if ( $c->authenticate($c->param('email'), $c->param('password')) ) { + $c->redirect_to('/home'); + } else { + $c->redirect_to('/'); + } } sub auth_logout { @@ -13,5 +32,9 @@ sub auth_logout { $c->redirect_to('/'); } +sub home { + my $c = shift; + $c->render(text => 'Root Home'); +} 1; diff --git a/t/admin/login.t b/t/admin/login.t index 24ef175..a246886 100644 --- a/t/admin/login.t +++ b/t/admin/login.t @@ -49,38 +49,36 @@ my $location_is = sub { }; $t->get_ok('/admin') - ->status_is(200) - ->content_like(qr/Login/); - -$t->ua->max_redirects(10); -$t->post_ok('/admin', form => { - email => 'user@example.com', - password => 'abc123', -})->status_is(200) - ->content_like(qr/Hello!/, 'Redirected to root as not an admin'); - -$t->ua->max_redirects(0); -$t->get_ok('/admin/home') ->status_is(302) ->$location_is('/'); +$t->ua->max_redirects(10); +$t->post_ok('/', form => { + email => 'user@example.com', + password => 'abc123', +})->status_is(200); + +$t->ua->max_redirects(0); +$t->get_ok('/admin') + ->status_is(302) + ->$location_is('/home'); + $t->get_ok('/logout') ->status_is(302) ->$location_is('/'); -$t->get_ok('/admin/home') +$t->get_ok('/admin') ->status_is(302) ->$location_is('/', 'Logged out'); $t->ua->max_redirects(10); -$t->post_ok('/admin', form => { +$t->post_ok('/', form => { email => 'admin@example.com', password => 'abc123', -})->status_is(200) - ->content_like(qr/Admin/); +})->status_is(200); $t->ua->max_redirects(0); -$t->get_ok('/admin/home') +$t->get_ok('/admin') ->status_is(200) ->content_like(qr/Admin/); @@ -88,7 +86,7 @@ $t->get_ok('/logout') ->status_is(302) ->$location_is('/'); -$t->get_ok('/admin/home') +$t->get_ok('/admin') ->status_is(302) ->$location_is('/', 'Logged out'); diff --git a/templates/admin/home.html.ep b/templates/admin/home.html.ep index 1133aa3..05038ac 100644 --- a/templates/admin/home.html.ep +++ b/templates/admin/home.html.ep @@ -1,5 +1,5 @@ -% layout 'default'; -% title 'LocalLoop Admin - Login'; +% layout 'admin'; +% title 'Home'; % content_for css => begin -% end -% content_for javascript => begin -% end - -