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