Prepare State::Cookie for distribution
[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 {
12 my $c = shift;
13
14 $c->NEXT::setup_session(@_);
15
16 $c->config->{session}{cookie_name} ||= "session";
17}
18
bf2bce67 19sub finalize {
b2f8df5e 20 my $c = shift;
1a776a0c 21
5e50008f 22 my $cookie_name = $c->config->{session}{cookie_name};
23
bf2bce67 24 if ( my $sid = $c->sessionid ) {
5e50008f 25 my $cookie = $c->request->cookies->{$cookie_name};
1a776a0c 26 if ( !$cookie or $cookie->value ne $sid ) {
5e50008f 27 $c->response->cookies->{$cookie_name} = { value => $sid };
1a776a0c 28 $c->log->debug(qq/A cookie with the session id "$sid" was saved/)
29 if $c->debug;
58c05d1a 30 }
bf2bce67 31 }
1a776a0c 32
bf2bce67 33 return $c->NEXT::finalize(@_);
34}
35
1a776a0c 36sub prepare_cookies {
bf2bce67 37 my $c = shift;
1a776a0c 38
5e50008f 39 my $cookie_name = $c->config->{session}{cookie_name};
40
41 if ( my $cookie = $c->request->cookies->{$cookie_name} ) {
bf2bce67 42 my $sid = $cookie->value;
43 $c->sessionid($sid);
44 $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug;
45 }
bf2bce67 46
1a776a0c 47 $c->NEXT::prepare_cookies(@_);
bf2bce67 48}
49
1a776a0c 50__PACKAGE__
57dbf608 51
1a776a0c 52__END__
bf2bce67 53
1a776a0c 54=pod
b2f8df5e 55
1a776a0c 56=head1 NAME
bf2bce67 57
1a776a0c 58Catalyst::Plugin::Session::State::Cookie - A session ID
bf2bce67 59
1a776a0c 60=head1 SYNOPSIS
bf2bce67 61
1a776a0c 62 use Catalyst qw/Session Session::State::Cookie Session::Store::Foo/;
bf2bce67 63
1a776a0c 64=head1 DESCRIPTION
bf2bce67 65
1a776a0c 66In order for L<Catalyst::Plugin::Session> to work the session ID needs to be
67stored on the client, and the session data needs to be stored on the server.
bf2bce67 68
1a776a0c 69This plugin stores the session ID on the client using the cookie mechanism.
57dbf608 70
1a776a0c 71=head1 EXTENDED METHODS
58c05d1a 72
57dbf608 73=over 4
74
1a776a0c 75=item prepare_cookies
57dbf608 76
1a776a0c 77Will restore if an appropriate cookie is found.
58c05d1a 78
1a776a0c 79=item finalize
58c05d1a 80
1a776a0c 81Will set a cookie called C<session> if it doesn't exist or if it's value is not the current session id.
58c05d1a 82
57dbf608 83=back
58c05d1a 84
5e50008f 85=head1 CONFIGURATION
86
87=over 4
88
89=item cookie_name
90
91The name of the cookie to store (defaults to C<session>).
92
93=back
94
bf2bce67 95=head1 SEE ALSO
96
1a776a0c 97L<Catalyst>, L<Catalyst::Plugin::Session>.
bf2bce67 98
99=head1 AUTHOR
100
57dbf608 101Sebastian Riedel E<lt>C<sri@cpan.org>E<gt>,
102Marcus Ramberg E<lt>C<mramberg@cpan.org>E<gt>,
1a776a0c 103Andrew Ford E<lt>C<andrewf@cpan.org>E<gt>,
104Yuval Kogman E<lt>C<nothingmuch@woobling.org>E<gt>
bf2bce67 105
106=head1 COPYRIGHT
107
bfeb5ca0 108This program is free software, you can redistribute it and/or modify it
109under the same terms as Perl itself.
bf2bce67 110
111=cut
112
1131;