| 1 | 1 | ▶ | use v5.40; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use experimental 'signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use Future::AsyncAwait; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use PAGI::Nano; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | # Cookie-backed sessions via enable 'Session'. The middleware sets/reads a | ||
| 7 | # session cookie and exposes the session on the context as $c->session (get/set/ | ||
| 8 | # delete). A POST that changes session state then redirects (303), the | ||
| 9 | # Post/Redirect/Get pattern. The default store is in-memory (development only). | ||
| 10 | # | ||
| 11 | # pagi-server app.pl | ||
| 12 | # # carry the cookie jar across requests: | ||
| 13 | # curl -c jar -b jar http://127.0.0.1:5000/ | ||
| 14 | # curl -c jar -b jar -d 'user=ada' http://127.0.0.1:5000/login -L | ||
| 15 | # curl -c jar -b jar http://127.0.0.1:5000/ | ||
| 16 | # curl -c jar -b jar -X POST http://127.0.0.1:5000/logout -L | ||
| 17 | |||
| 18 | 1 | ▶ | my $app = app { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
| |||
| 19 | 1 | enable 'Session', secret => 'dev-secret-change-me'; # cookie + in-memory store | |
| 20 | |||
| 21 | 2 | ▶ | get '/' => sub ($c) { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 22 | # get('key', $default) is the lenient form; one-arg get dies if missing. | ||
| 23 | 2 | my $views = $c->session->get('views', 0) + 1; | |
| 24 | 2 | $c->session->set(views => $views); | |
| 25 | { | ||
| 26 | 2 | you_are => $c->session->get('user', 'guest'), | |
| 27 | views_this_session => $views, | ||
| 28 | }; | ||
| 29 | 1 | }; | |
| 30 | |||
| 31 | 1 | ▶ | post '/login' => async sub ($c) { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 32 | my $attrs = await $c->params->required( | ||
| 33 | 'user', | ||
| 34 | 0 | ▶ | sub ($c, $missing) { $c->json({ error => 'missing', fields => $missing }, status => 400) }, |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 35 | 1 | ); | |
| 36 | 1 | $c->session->set(user => $attrs->{user}); | |
| 37 | 1 | $c->redirect('/', 303); # Post/Redirect/Get | |
| 38 | 1 | }; | |
| 39 | |||
| 40 | 0 | ▶ | post '/logout' => sub ($c) { |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 41 | 0 | $c->session->delete('user'); | |
| 42 | 0 | $c->redirect('/', 303); | |
| 43 | 1 | }; | |
| 44 | 1 | }; | |
| 45 | |||
| 46 | 1 | $app; | |