File Coverage

blib/lib/Mojo/Hakkefuin/Sessions.pm
Criterion Covered Total %
statement 19 19 100.0
branch 7 12 58.3
condition 7 15 46.6
subroutine 2 2 100.0
pod 1 1 100.0
total 36 49 73.4


line stmt bran cond sub pod time code
1             package Mojo::Hakkefuin::Sessions;
2 5     5   763016 use Mojo::Base 'Mojolicious::Sessions';
  5         9  
  5         33  
3              
4             has 'max_age';
5              
6             sub store {
7 41     41 1 51638 my ($self, $c) = @_;
8              
9             # Make sure session was active
10 41         151 my $stash = $c->stash;
11 41 100       481 return unless my $session = $stash->{'mojo.session'};
12 31 0 33     104 return unless keys %$session || $stash->{'mojo.active_session'};
13              
14             # Don't reset flash for static files
15 31         73 my $old = delete $session->{flash};
16 31 50       92 $session->{new_flash} = $old if $stash->{'mojo.static'};
17 31 50       51 delete $session->{new_flash} unless keys %{$session->{new_flash}};
  31         146  
18              
19             # Generate "expires" value from "expiration" if necessary
20 31   33     116 my $expiration = $session->{expiration} // $self->default_expiration;
21 31         57 my $default = delete $session->{expires};
22 31 50 66     225 $session->{expires} = $default || time + $expiration
      33        
23             if $expiration || $default;
24              
25             # "max-age" is set if necessary
26             my $max_age = $session->{expires} - time
27 31 100 66     123 if $self->max_age && $session->{expires} > time;
28              
29 31         309 my $value = Mojo::Util::b64_encode $self->serialize->($session), '';
30 31         907 $value =~ y/=/-/;
31             my $options = {
32             domain => $self->cookie_domain,
33             expires => $session->{expires},
34 31         115 httponly => 1,
35             max_age => $max_age,
36             path => $self->cookie_path,
37             samesite => $self->samesite,
38             secure => $self->secure
39             };
40 31         689 $c->signed_cookie($self->cookie_name, $value, $options);
41             }
42              
43             1;
44              
45             =encoding utf8
46              
47             =head1 NAME
48              
49             Mojo::Hakkefuin::Sessions - Session manager with available set up max-age
50              
51             =head1 SYNOPSIS
52              
53             use Mojo::Hakkefuin::Sessions;
54              
55             my $sessions = Mojo::Hakkefuin::Sessions->new;
56             $sessions->cookie_name('myapp');
57             $sessions->default_expiration(86400);
58             $sessions->max_age(1);
59              
60             =head1 DESCRIPTION
61              
62             L inherits all from L.
63             Its meant to available setup B.
64              
65             =head1 ATTRIBUTES
66              
67             L implements the attributes from
68             L, and additional attributes as the following.
69              
70             =head2 max_age
71            
72             my $bool = $sessions->max_age;
73             $sessions = $sessions->max_age($bool);
74              
75             Set the C for all session cookies. If "max_age" is set, the session cookie
76             will have the "expires" and "max-age" attributes, and when the browser finds the "max-age"
77             attribute in a cookie, the cookie expiration will use "max-age" as a top priority.
78             The "max-age" attribute only applies if the browser supports this attribute.
79             Before set this attribute, please see
80             L
81              
82             =head1 METHODS
83              
84             L from all methods L
85             and implements the following new ones.
86              
87             =head2 store
88              
89             $sessions->store;
90              
91             Store session data in signed cookie.
92              
93             =head1 SEE ALSO
94              
95             L, L, L, L.
96              
97             =cut