I am totally new to dancer perl. Have some experience in j2ee/dot net web development. Below is my login subroutine and main.tt master template to display name and role of logged in user.
subroutine:
post '/login' => sub {
my $err;
my $next_home;
my $full_name;
my $pwd;
my $role;
my $given_uid=params->{'username'};
my $given_pwd=params->{'password'};
my $db_con = DBI->connect('<db_connection_string>')
or die $DBI::errstr;
my $sql = "<qwery to fetch user details>";
my $rs = $db_con->prepare($sql) or die $db_con->errstr;
$rs->execute or die $rs->errstr;
my @row = $rs->fetchrow_array;
if ( $#row lt 1 ) {
$err="Invalid Username";
$next_home="index.tt";
}
else {
$full_name=$row[0];
$pwd=$row[1];
$role=$row[2];
if ( $given_pwd ne $pwd ) {
$err = "Invalid Password";
$next_home="index.tt";
}
else {
session 'logged_in' => true;
$err = 'Logged in Successfully';
if ( $role eq 'DEVELOPER') {
$next_home="developer_home.tt";
}
elsif ( $role eq 'ADMIN' ) {
$next_home="admin_home.tt";
}
elsif ( $role eq 'DEPLOYER' ) {
$next_home="deployer_home.tt";
}
elsif ( $role eq 'APPROVER' ) {
$next_home="approver_home.tt";
}
else {
$err = "Invalid Role";
$next_home="index.tt";
}
}
session user_logged_in => $full_name;
session role_of_user_logged_in => $role;
template "$next_home" , {
'NAME' => $full_name,
'ROLE' => $role,
}
}
};
main.tt:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html; charset=<% settings.charset %>" />
<title>MYFIRSTDANCE2</title>
</head>
<body>
<div id="banner" style="min-width:100%; background-image:url(<% request.uri_base %>/images/header.jpg);">
<h1 style="color:white"><center> -:DEPLOY EVERYTHING WITH EASE:- </center></h1>
<% IF session.logged_in %>
<h3>Welcome <% session.user_logged_in %> ,</h3>
<h3>Role: <% session.role_of_user_logged_in %></h3>
<% END %>
<h3>
<hr>
<br>
</div>
<div id="main">
<% content %>
</div>
<div id="footer">
</div>
</body>
</html>
I have 2 questions:
IF session.logged_in is not working in main.tt . When I am not logged in then also it displays Welcome Blank Space, Role: Blank Space.
How can I redirect to index.tt page when username/password is wrong. In current code, browser window goes blank if any of them is wrong.
Any help is appreciated.
tags. Somehow, IF is not working.
– Rony Aug 10 '15 at 14:22