[Catalyst] Session

Yuval Kogman nothingmuch at woobling.org
Tue Mar 28 12:58:23 CEST 2006


On Mon, Mar 27, 2006 at 21:17:18 -0800, Will Smith wrote:
> Hello,
>   I have a couple questions that I would like to ask. I am using these Session plugins for my app:
>   Session Session::Store::File Session::State::Cookie
>    
>   I have a sub that logs the user out and kills the session:
>   $c->session->delete();

$c->session returns a hash reference, not an object.

>   1- Could someone please show me how to kill this session. I have
>   tried $c->session_delete(); and clear(), but still get errors.

In Catalyst::Plugin::Session's documentation (e.g. here:
http://search.cpan.org/perldoc?Catalyst%3A%3APlugin%3A%3ASession)
you can find the delete_session method, which does what you want.

>   2- How could I control a user's session, in other words, a user
>   can login at one machine only; if that user (or someone else)
>   tries to login at another machine at the same time, the previous
>   login will be kicked out. Please give me some sample code that I
>   could start with.

I hope you're using Catalyst::Plugin::Authentication

Todo this you need to keep a link to the session data within the
user table. Then you could override the set_authenticated method
(called after a successful login, or any other credential):

	sub set_authenticated {
		my ( $c, $user ) = @_;
		$user->sessionid( $c->sessionid );
		$self->NEXT::set_authenticated( $user );
	}

Then override the 'prepare' routine in post order, to perform
additional verification:

	sub prepare {
		my $c = shift->NEXT::prepare(@_); # post order

		if ( $c->user_exists ) {
			my $user = $c->user;
			unless ( $user->sessionid eq $c->sessionid ) {
				$c->stash->{logout_reason} = "logged in in separate session";
				$c->logout;
			}
		}
	}

I hope this works for you!

-- 
  Yuval Kogman <nothingmuch at woobling.org>
http://nothingmuch.woobling.org  0xEBD27418

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 191 bytes
Desc: not available
Url : http://lists.rawmode.org/pipermail/catalyst/attachments/20060328/bde82a1c/attachment.pgp 


More information about the Catalyst mailing list