e865c3e816d9901fa6a078bf15570a967fd10897
[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 sub finalize {
10     my $c = shift;
11
12     if ( my $sid = $c->sessionid ) {
13         my $cookie = $c->request->cookies->{session};
14         if ( !$cookie or $cookie->value ne $sid ) {
15             $c->response->cookies->{session} = { value => $sid };
16             $c->log->debug(qq/A cookie with the session id "$sid" was saved/)
17               if $c->debug;
18         }
19     }
20
21     return $c->NEXT::finalize(@_);
22 }
23
24 sub prepare_cookies {
25     my $c = shift;
26
27     if ( my $cookie = $c->request->cookies->{session} ) {
28         my $sid = $cookie->value;
29         $c->sessionid($sid);
30         $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug;
31     }
32
33     $c->NEXT::prepare_cookies(@_);
34 }
35
36 __PACKAGE__
37
38 __END__
39
40 =pod
41
42 =head1 NAME
43
44 Catalyst::Plugin::Session::State::Cookie - A session ID 
45
46 =head1 SYNOPSIS
47
48         use Catalyst qw/Session Session::State::Cookie Session::Store::Foo/;
49
50 =head1 DESCRIPTION
51
52 In order for L<Catalyst::Plugin::Session> to work the session ID needs to be
53 stored on the client, and the session data needs to be stored on the server.
54
55 This plugin stores the session ID on the client using the cookie mechanism.
56
57 =head1 EXTENDED METHODS
58
59 =over 4
60
61 =item prepare_cookies
62
63 Will restore if an appropriate cookie is found.
64
65 =item finalize
66
67 Will set a cookie called C<session> if it doesn't exist or if it's value is not the current session id.
68
69 =back
70
71 =head1 SEE ALSO
72
73 L<Catalyst>, L<Catalyst::Plugin::Session>.
74
75 =head1 AUTHOR
76
77 Sebastian Riedel E<lt>C<sri@cpan.org>E<gt>,
78 Marcus Ramberg E<lt>C<mramberg@cpan.org>E<gt>,
79 Andrew Ford E<lt>C<andrewf@cpan.org>E<gt>,
80 Yuval Kogman E<lt>C<nothingmuch@woobling.org>E<gt>
81
82 =head1 COPYRIGHT
83
84 This program is free software, you can redistribute it and/or modify it
85 under the same terms as Perl itself.
86
87 =cut
88
89 1;