Skip to content

Instantly share code, notes, and snippets.

@jjn1056
Created October 19, 2021 19:44
Show Gist options
  • Save jjn1056/f065c4abb28d100f26f590d01f5dbaef to your computer and use it in GitHub Desktop.
Save jjn1056/f065c4abb28d100f26f590d01f5dbaef to your computer and use it in GitHub Desktop.
package MyApp::Model::Session;
use Moose;
extend 'Catalyst::ApplicationModel';
has 'person' => (is=>'ro', required=>1, model=>'Schema::Person');
sub authenticate($self, $username='', $password='') {
my $user = $self->person->authenticate($username, $password);
$self->ctx->session->{user_id} = $user->id if !$user->has_errors;
return $user;
}
sub user($self) {
return $self->ctx->stash->{user} ||= do {
my $id = $self->ctx->session->{user_id} // return;
my $person = $self->person->find({id=>$id}) // return;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment