File Coverage

blib/lib/Dancer2/Plugin/Interchange6/Routes/Account.pm
Criterion Covered Total %
statement 59 59 100.0
branch 15 18 83.3
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 82 85 96.4


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::Interchange6::Routes::Account;
2              
3 2     2   7 use strict;
  2         2  
  2         44  
4 2     2   6 use warnings;
  2         2  
  2         35  
5              
6 2     2   396 use Try::Tiny;
  2         833  
  2         896  
7              
8             =head1 NAME
9              
10             Dancer2::Plugin::Interchange6::Routes::Account - Account routes for Interchange6 Shop Machine
11              
12             =head1 DESCRIPTION
13              
14             The Interchange6 account routes module installs Dancer2 routes for
15             login and logout
16              
17             =cut
18              
19             =head1 FUNCTIONS
20              
21             =head2 account_routes
22              
23             Returns the account routes based on the plugin configuration.
24              
25             =cut
26              
27             sub account_routes {
28 2     2 1 3 my $plugin = shift;
29 2         4 my %routes;
30              
31             $routes{login}->{get} = sub {
32 5     5   702 my $app = shift;
33 5         16 my $d2pae = $app->with_plugin('Dancer2::Plugin::Auth::Extensible');
34 5 50       222 return $app->redirect('/') if $d2pae->logged_in_user;
35              
36 5         198 my %values;
37              
38 5 100       59 if ( $app->request->param('login_failed') ) {
39 2         23 $values{error} = "Login failed";
40             }
41              
42             # record return_url in template tokens
43 5 100       50 if ( my $return_url = $app->request->param('return_url') ) {
44 3         33 $values{return_url} = $return_url;
45             }
46              
47             # call before_login_display route so template tokens
48             # can be injected
49 5         112 $app->execute_hook( 'plugin.interchange6.before_login_display',
50             \%values );
51              
52             # record return_url in the session to reuse it in post route
53 5         3000 $app->session->write( return_url => $values{return_url} );
54              
55 5         323 $app->template( $plugin->login_template, \%values );
56 2         13 };
57              
58             $routes{login}->{post} = sub {
59 6     6   927 my $app = shift;
60 6         21 my $d2pae = $app->with_plugin('Dancer2::Plugin::Auth::Extensible');
61 6         243 my $d2pic6 = $app->with_plugin('Dancer2::Plugin::Interchange6');
62              
63 6 50       168 return $app->redirect('/') if $d2pae->logged_in_user;
64              
65 6         321 my $login_route = '/' . $plugin->login_uri;
66              
67             my $user = $d2pic6->shop_user->find(
68 6         53 { username => $app->request->params->{username} } );
69              
70 6         29261 my ( $success, $realm, $current_cart );
71              
72 6 100       93 if ($user) {
73              
74             # remember current cart object
75 4         17 $current_cart = $d2pic6->shop_cart;
76              
77             ( $success, $realm ) = $d2pae->authenticate_user(
78             $app->request->params->{username},
79             $app->request->params->{password}
80 4         22 );
81             }
82              
83 6 100       5768792 if ($success) {
84 4         161 $app->session->write( logged_in_user => $user->username );
85 4         446 $app->session->write( logged_in_user_id => $user->id );
86 4         369 $app->session->write( logged_in_user_realm => $realm );
87              
88 4 50       193 if ( !$current_cart->users_id ) {
89 4         14 $current_cart->set_users_id( $user->id );
90             }
91              
92             # now pull back in old cart items from previous authenticated
93             # sessions were sessions_id is undef in db cart
94 4         23 $current_cart->load_saved_products;
95              
96 4 100       26374 if ( $app->session->read('return_url') ) {
97 1         45 my $url = $app->session->read('return_url');
98 1         32 $app->session->write( return_url => undef );
99 1         56 return $app->redirect($url);
100             }
101             else {
102 3         137 return $app->redirect( '/' . $plugin->login_success_uri );
103             }
104             }
105             else {
106             $app->log(
107             "debug",
108             "Authentication failed for ",
109             $app->request->params->{username}
110 2         12 );
111              
112             return $app->forward(
113             $login_route,
114             {
115             return_url => $app->request->params->{return_url},
116 2         917 login_failed => 1
117             },
118             { method => 'get' }
119             );
120             }
121 2         9 };
122              
123             $routes{logout}->{any} = sub {
124 5     5   809 my $app = shift;
125 5         19 my $d2pic6 = $app->with_plugin('Dancer2::Plugin::Interchange6');
126 5         236 my $cart = $d2pic6->shop_cart;
127 5 100       93 if ( $cart->count > 0 ) {
128              
129             # save our items for next login
130             try {
131 3         132 $cart->set_sessions_id(undef);
132             }
133             catch {
134 3         2914 $app->log( "warning",
135             "Failed to set sessions_id to undef for cart id: ",
136             $cart->id );
137 3         165 };
138             }
139              
140             # any empty cart with sessions_id matching our session id will be
141             # destroyed here
142 5         1529 $app->destroy_session;
143 5         19422 return $app->redirect('/');
144 2         9 };
145              
146 2         6 return \%routes;
147             }
148              
149             1;