File Coverage

blib/lib/Articulate/Service/Login.pm
Criterion Covered Total %
statement 18 33 54.5
branch 0 4 0.0
condition n/a
subroutine 6 8 75.0
pod 0 2 0.0
total 24 47 51.0


line stmt bran cond sub pod time code
1             package Articulate::Service::Login;
2              
3 5     5   2350 use strict;
  5         9  
  5         145  
4 5     5   17 use warnings;
  5         5  
  5         92  
5              
6 5     5   19 use Articulate::Syntax;
  5         5  
  5         26  
7              
8 5     5   6064 use Moo;
  5         8  
  5         22  
9             with 'Articulate::Role::Service';
10              
11 5     5   1336 use Try::Tiny;
  5         9  
  5         251  
12 5     5   21 use Scalar::Util qw(blessed);
  5         6  
  5         930  
13              
14             =head1 NAME
15              
16             Articulate::Service::Login - provide login, logout
17              
18             =cut
19              
20             =head1 METHODS
21              
22             =head1 handle_login
23              
24             $self->handle_login( {user_id => 'admin', password => 'secret!' } )
25              
26             Tries to authenticate a user and, if successful, sets a session var (see L).
27              
28             Returns C<< {user_id => $user_id } >> if successful, throws an error otherwise.
29              
30             =head1 handle_logout
31              
32             $self->handle_login( {user_id => 'admin', password => 'secret!' } )
33              
34             Destroys the current session.
35              
36             Returns C<< {user_id => $user_id } >>.
37              
38             =cut
39              
40             sub handle_login {
41 0     0 0   my $self = shift;
42 0           my $request = shift;
43              
44 0           my $user_id = $request->data->{user_id};
45 0           my $password = $request->data->{password};
46              
47 0 0         if ( defined $user_id ) {
48 0 0         if ( $self->authentication->login( $user_id, $password ) ) {
49 0           $self->framework->user_id($user_id);
50 0           response success => { user_id => $user_id };
51             } # Can we handle all the exceptions with 403s?
52             else {
53 0           throw_error Forbidden => 'Incorrect credentials';
54             }
55             }
56             else {
57             # todo: see if we have email and try to identify a user and verify with that
58 0           throw_error Forbidden => 'Missing user id';
59             }
60              
61             }
62              
63             sub handle_logout {
64 0     0 0   my $self = shift;
65 0           my $request = shift;
66 0           my $user_id = $self->framework->user_id;
67 0           $self->framework->session->destroy();
68 0           response success => { user_id => $user_id };
69             }
70              
71             1;