summary refs log tree commit diff stats
path: root/lib/Crater/Routes/Auth.rakumod
blob: 46100f5e5f21ad98995f4731b0af3939494e1907 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use Cro::HTTP::Router;
use Cro::WebApp::Template;

use Crater::Session;

#| auth-routes contains routes related to authentication.
sub auth-routes(
    Str :$password!, #= password for authentication
) is export {
    route {
        get -> LoggedIn $session, 'login' {
            redirect '/', :see-other;
        }
        get -> Crater::Session $session, 'login' {
            template 'login.crotmp', { :!error };
        }
        post -> Crater::Session $session, 'login' {
            request-body -> (:$pass!, *%) {
                if $password eq $pass {
                    $session.logged-in = True;
                    redirect :see-other, '/';
                } else {
                    template 'login.crotmp', {
                        error => 'Incorrect password.'
                    };
                }
            }
        }
        get -> LoggedIn $session, 'logout' {
            $session.logged-in = False;
            redirect :see-other, '/';
        }
    }
}