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