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   5 use warnings;
  2         4  
  2         34  
5              
6 2     2   403 use Try::Tiny;
  2         910  
  2         901  
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   912 my $app = shift;
33 5         18 my $d2pae = $app->with_plugin('Dancer2::Plugin::Auth::Extensible');
34 5 50       244 return $app->redirect('/') if $d2pae->logged_in_user;
35              
36 5         202 my %values;
37              
38 5 100       29 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       52 if ( my $return_url = $app->request->param('return_url') ) {
44 3         27 $values{return_url} = $return_url;
45             }
46              
47             # call before_login_display route so template tokens
48             # can be injected
49 5         100 $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         3407 $app->session->write( return_url => $values{return_url} );
54              
55 5         405 $app->template( $plugin->login_template, \%values );
56 2         14 };
57              
58             $routes{login}->{post} = sub {
59 7     7   1402 my $app = shift;
60 7         34 my $d2pae = $app->with_plugin('Dancer2::Plugin::Auth::Extensible');
61 7         365 my $d2pic6 = $app->with_plugin('Dancer2::Plugin::Interchange6');
62              
63 7 50       203 return $app->redirect('/') if $d2pae->logged_in_user;
64              
65 7         384 my $login_route = '/' . $plugin->login_uri;
66              
67             my $user = $d2pic6->shop_user->find(
68 7         85 { username => $app->request->params->{username} } );
69              
70 7         37546 my ( $success, $realm, $current_cart );
71              
72 7 100       126 if ($user) {
73              
74             # remember current cart object
75 5         31 $current_cart = $d2pic6->shop_cart;
76              
77             ( $success, $realm ) = $d2pae->authenticate_user(
78             $app->request->params->{username},
79             $app->request->params->{password}
80 5         25 );
81             }
82              
83 7 100       7380064 if ($success) {
84 5         114 $app->session->write( logged_in_user => $user->username );
85 5         688 $app->session->write( logged_in_user_id => $user->id );
86 5         574 $app->session->write( logged_in_user_realm => $realm );
87              
88 5 50       316 if ( !$current_cart->users_id ) {
89 5         23 $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 5         35 $current_cart->load_saved_products;
95              
96 5 100       24684 if ( $app->session->read('return_url') ) {
97 1         51 my $url = $app->session->read('return_url');
98 1         33 $app->session->write( return_url => undef );
99 1         67 return $app->redirect($url);
100             }
101             else {
102 4         196 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         15 );
111              
112             return $app->forward(
113             $login_route,
114             {
115             return_url => $app->request->params->{return_url},
116 2         1033 login_failed => 1
117             },
118             { method => 'get' }
119             );
120             }
121 2         10 };
122              
123             $routes{logout}->{any} = sub {
124 5     5   949 my $app = shift;
125 5         20 my $d2pic6 = $app->with_plugin('Dancer2::Plugin::Interchange6');
126 5         314 my $cart = $d2pic6->shop_cart;
127 5 100       97 if ( $cart->count > 0 ) {
128              
129             # save our items for next login
130             try {
131 3         137 $cart->set_sessions_id(undef);
132             }
133             catch {
134 3         2984 $app->log( "warning",
135             "Failed to set sessions_id to undef for cart id: ",
136             $cart->id );
137 3         181 };
138             }
139              
140             # any empty cart with sessions_id matching our session id will be
141             # destroyed here
142 5         1568 $app->destroy_session;
143 5         22201 return $app->redirect('/');
144 2         9 };
145              
146 2         6 return \%routes;
147             }
148              
149             1;