File Coverage

lib/Dancer/Plugin/Interchange6/Routes/Account.pm
Criterion Covered Total %
statement 65 65 100.0
branch 15 18 83.3
condition n/a
subroutine 11 11 100.0
pod 1 1 100.0
total 92 95 96.8


line stmt bran cond sub pod time code
1             package Dancer::Plugin::Interchange6::Routes::Account;
2              
3 2     2   6 use strict;
  2         4  
  2         41  
4 2     2   7 use warnings;
  2         2  
  2         36  
5              
6 2     2   7 use Dancer ':syntax';
  2         2  
  2         9  
7 2     2   509 use Dancer::Plugin;
  2         2  
  2         102  
8 2     2   7 use Dancer::Plugin::Interchange6;
  2         5  
  2         127  
9 2     2   7 use Dancer::Plugin::Auth::Extensible;
  2         2  
  2         143  
10 2     2   8 use Try::Tiny;
  2         2  
  2         1026  
11              
12             =head1 NAME
13              
14             Dancer::Plugin::Interchange6::Routes::Account - Account routes for Interchange6 Shop Machine
15              
16             =head1 DESCRIPTION
17              
18             The Interchange6 account routes module installs Dancer routes for
19             login and logout
20              
21             =cut
22              
23             register_hook 'before_login_display';
24              
25             =head1 FUNCTIONS
26              
27             =head2 account_routes
28              
29             Returns the account routes based on the passed routes configuration.
30              
31             =cut
32              
33             sub account_routes {
34 1     1 1 2 my $routes_config = shift;
35 1         1 my %routes;
36              
37             $routes{login}->{get} = sub {
38 5 50   5   12192 return redirect '/' if logged_in_user;
39              
40 5         10865 my %values;
41              
42 5 100       17 if ( vars->{login_failed} ) {
43 2         18 $values{error} = "Login failed";
44             }
45              
46             # record return_url in template tokens
47 5 100       33 if (my $return_url = param('return_url')) {
48 3         410 $values{return_url} = $return_url;
49             }
50              
51             # call before_login_display route so template tokens
52             # can be injected
53 5         71 execute_hook('before_login_display', \%values);
54              
55             # record return_url in the session to reuse it in post route
56 5         623 session return_url => $values{return_url};
57              
58 5         30851 template $routes_config->{account}->{login}->{template}, \%values;
59 1         10 };
60              
61             $routes{login}->{post} = sub {
62 6 50   6   15734 return redirect '/' if logged_in_user;
63              
64 6         13082 my $login_route = '/' . $routes_config->{account}->{login}->{uri};
65              
66 6         28 my $user = shop_user->find({ username => params->{username}});
67              
68 6         31707 my ($success, $realm, $current_cart);
69              
70 6 100       95 if ($user) {
71             # remember current cart object
72 4         17 $current_cart = shop_cart;
73              
74 4         19 ($success, $realm) = authenticate_user( params->{username}, params->{password} );
75             }
76              
77 6 100       5773984 if ($success) {
78 4         21 session logged_in_user => $user->username;
79 4         26835 session logged_in_user_id => $user->id;
80 4         23439 session logged_in_user_realm => $realm;
81              
82 4 50       23445 if (! $current_cart->users_id) {
83 4         19 $current_cart->set_users_id($user->id);
84             }
85              
86             # now pull back in old cart items from previous authenticated
87             # sessions were sessions_id is undef in db cart
88 4         18 $current_cart->load_saved_products;
89              
90 4 100       26050 if ( session('return_url') ) {
91 1         2599 my $url = session('return_url');
92 1         2172 session return_url => undef;
93 1         6145 return redirect $url;
94             }
95             else {
96             return redirect '/'
97 3         7928 . $routes_config->{account}->{login}->{success_uri};
98             }
99             } else {
100 2         7 debug "Authentication failed for ", params->{username};
101              
102 2         137 var login_failed => 1;
103 2         20 return forward $login_route, { return_url => params->{return_url} }, { method => 'get' };
104             }
105 1         6 };
106              
107             $routes{logout}->{any} = sub {
108 5     5   36408 my $cart = shop_cart;
109 5 100       150 if ( $cart->count > 0 ) {
110             # save our items for next login
111             try {
112 3         151 $cart->set_sessions_id(undef);
113             }
114             catch {
115 3         2958 warning "Failed to set sessions_id to undef for cart id: "
116             . $cart->id;
117 3         167 };
118             }
119             # any empty cart with sessions_id matching our session id will be
120             # destroyed here
121 5         514 session->destroy;
122 5         26608 return redirect '/';
123 1         2 };
124              
125 1         6 return \%routes;
126             }
127              
128             true;