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