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