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