package LATMOS::Accounts::Web::Controller::Root;

use strict;
use warnings;
use base 'LATMOS::Accounts::Web::Controller';

#
# Sets the actions in this controller to be registered with no prefix
# so they function identically to actions created in MyApp.pm
#
__PACKAGE__->config->{namespace} = '';

=head1 NAME

LATMOS::Accounts::Web::Controller::Root - Root Controller for LATMOS::Accounts::Web

=head1 DESCRIPTION

[enter your description here]

=head1 METHODS

=cut

=head2 default

=cut

sub default : Private {
    my ( $self, $c ) = @_;
    $c->res->redirect($c->uri_for('/users', $c->user->{username})) if($c->user);
}

sub login : Local {
    my ( $self, $c ) = @_;

    $c->stash->{template} = 'login.tt';
    if ($c->req->param('username')) {
        if ($c->authenticate({
                username => $c->req->param('username'),
                password => $c->req->param('password')})) {
            my $redirurl = $c->uri_for($c->req->param('loguri') ||  '/');
            $c->res->redirect($redirurl || $c->uri_for('/'));
        } else {
            # invalid login...
        }
    }
    my ($redirurl) = grep { $_ } (
        $c->req->param('loguri'),
        ($c->req->path ? '/' . $c->req->path : undef),
        $c->uri_for('/'),
    );
    $c->stash->{redirurl} = $redirurl;
}

sub logout : Local {
    my ( $self, $c ) = @_;
    $c->logout;
    $c->res->redirect($c->uri_for('/'));
}

=head1 AUTHOR

Thauvin Olivier

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
