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