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