changed default cookie name to Catalyst::Utils::appprefix($c)
[catagits/Catalyst-Plugin-Session-State-Cookie.git] / lib / Catalyst / Plugin / Session / State / Cookie.pm
CommitLineData
1a776a0c 1package Catalyst::Plugin::Session::State::Cookie;
2use base qw/Catalyst::Plugin::Session::State/;
bf2bce67 3
4use strict;
1a776a0c 5use warnings;
bf2bce67 6
1a776a0c 7use NEXT;
bf2bce67 8
81eb8ebf 9our $VERSION = "0.01";
10
5e50008f 11sub setup_session {
20e33791 12 my $c = shift;
5e50008f 13
20e33791 14 $c->NEXT::setup_session(@_);
c702e4ca 15 $c->config->{session}{cookie_name} ||= Catalyst::Utils::appprefix( $c );
5e50008f 16}
17
d52e5079 18sub finalize_cookies {
b2f8df5e 19 my $c = shift;
1a776a0c 20
58730edc 21 if ( $c->sessionid ) {
22 $c->update_session_cookie( $c->make_session_cookie );
23 }
db1cda22 24
25 return $c->NEXT::finalize_cookies(@_);
26}
27
28sub update_session_cookie {
58730edc 29 my ( $c, $updated ) = @_;
20e33791 30 my $cookie_name = $c->config->{session}{cookie_name};
58730edc 31 $c->response->cookies->{$cookie_name} = $updated;
db1cda22 32}
5e50008f 33
db1cda22 34sub make_session_cookie {
58730edc 35 my $c = shift;
36
37 my $cfg = $c->config->{session};
38 my $cookie = {
39 value => $c->sessionid,
40 ( $cfg->{cookie_domain} ? ( domain => $cfg->{cookie_domain} ) : () ),
41 };
42
43 if ( exists $cfg->{cookie_expires} ) {
44 if ( my $ttl = $cfg->{cookie_expires} ) {
45 $cookie->{expires} = time() + $ttl;
46 } # else { cookie is non-persistent }
47 }
48 else {
49 $cookie->{expires} = $c->session->{__expires};
50 }
51
52 return $cookie;
bf2bce67 53}
54
1a776a0c 55sub prepare_cookies {
bf2bce67 56 my $c = shift;
1a776a0c 57
7acb108b 58 my $ret = $c->NEXT::prepare_cookies(@_);
59
20e33791 60 my $cookie_name = $c->config->{session}{cookie_name};
5e50008f 61
62 if ( my $cookie = $c->request->cookies->{$cookie_name} ) {
bf2bce67 63 my $sid = $cookie->value;
64 $c->sessionid($sid);
65 $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug;
66 }
bf2bce67 67
20e33791 68 return $ret;
bf2bce67 69}
70
1a776a0c 71__PACKAGE__
57dbf608 72
1a776a0c 73__END__
bf2bce67 74
1a776a0c 75=pod
b2f8df5e 76
1a776a0c 77=head1 NAME
bf2bce67 78
1a776a0c 79Catalyst::Plugin::Session::State::Cookie - A session ID
bf2bce67 80
1a776a0c 81=head1 SYNOPSIS
bf2bce67 82
20e33791 83 use Catalyst qw/Session Session::State::Cookie Session::Store::Foo/;
bf2bce67 84
1a776a0c 85=head1 DESCRIPTION
bf2bce67 86
1a776a0c 87In order for L<Catalyst::Plugin::Session> to work the session ID needs to be
88stored on the client, and the session data needs to be stored on the server.
bf2bce67 89
1a776a0c 90This plugin stores the session ID on the client using the cookie mechanism.
57dbf608 91
724a6173 92=head1 METHODS
93
94=over 4
95
96=item make_session_cookie
97
98Returns a hash reference with the default values for new cookies.
99
100=item update_session_cookie $hash_ref
101
102Sets the cookie based on C<cookie_name> in the response object.
103
104=back
105
1a776a0c 106=head1 EXTENDED METHODS
58c05d1a 107
57dbf608 108=over 4
109
1a776a0c 110=item prepare_cookies
57dbf608 111
1a776a0c 112Will restore if an appropriate cookie is found.
58c05d1a 113
d52e5079 114=item finalize_cookies
58c05d1a 115
19c2baa1 116Will set a cookie called C<session> if it doesn't exist or if it's value is not
117the current session id.
118
119=item setup_session
120
121Will set the C<cookie_name> parameter to it's default value if it isn't set.
58c05d1a 122
57dbf608 123=back
58c05d1a 124
5e50008f 125=head1 CONFIGURATION
126
127=over 4
128
129=item cookie_name
130
c702e4ca 131The name of the cookie to store (defaults to C<Catalyst::Utils::apprefix($c)>).
5e50008f 132
41b4b15c 133=item cookie_domain
134
135The name of the domain to store in the cookie (defaults to current host)
136
5e50008f 137=back
138
724a6173 139=head1 CAVEATS
db1cda22 140
141Sessions have to be created before the first write to be saved. For example:
142
143 sub action : Local {
144 my ( $self, $c ) = @_;
145 $c->res->write("foo");
146 $c->session( ... );
147 ...
148 }
149
150Will cause a session ID to not be set, because by the time a session is
151actually created the headers have already been sent to the client.
152
bf2bce67 153=head1 SEE ALSO
154
1a776a0c 155L<Catalyst>, L<Catalyst::Plugin::Session>.
bf2bce67 156
47f47da5 157=head1 AUTHORS
bf2bce67 158
47f47da5 159This module is derived from L<Catalyst::Plugin::Session::FastMmap> code, and
160has been heavily modified since.
161
162Andrew Ford
163Andy Grundman
164Christian Hansen
165Yuval Kogman, C<nothingmuch@woobling.org>
166Marcus Ramberg
167Sebastian Riedel
bf2bce67 168
169=head1 COPYRIGHT
170
bfeb5ca0 171This program is free software, you can redistribute it and/or modify it
172under the same terms as Perl itself.
bf2bce67 173
174=cut
175
1761;