Skip to content

Commit

Permalink
fix login
Browse files Browse the repository at this point in the history
  • Loading branch information
0c34 committed Oct 24, 2017
1 parent 31a8888 commit 3458f61
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
11 changes: 7 additions & 4 deletions templates/template.login.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{{define "template.login"}}
<!DOCTYPE html>
<html>

<head>
<title>{{.Title}}</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style type="text/css">
html,
body {
padding-top: 30px;
padding: 30px;
height: 100%;
background: #f4f4f4;
}
</style>
</head>
Expand All @@ -19,7 +22,7 @@
<div class="col-md-4 col-md-offset-4">
<div class="login-panel panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title" style="text-align:center">GoVWA User Login</h3>
<h3 class="panel-title">GOVWA User Login</h3>
</div>
<div class="panel-body">
{{.message}}
Expand All @@ -41,7 +44,7 @@ <h3 class="panel-title" style="text-align:center">GoVWA User Login</h3>
</div>
</body>
<script>
$(document).ready(function(){
$(document).ready(function() {
$("#message").delay(2000).fadeOut();
});
</script>
Expand Down
26 changes: 20 additions & 6 deletions user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,17 @@ func LoginViewHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Para
}

if r.Method == "POST" {
if loginAction(w, r, ps) {
util.Redirect(w, r, "index", 302)
} else {
//the best solution instead of using ajax request
data["message"] = template.HTML("<div id=\"message\" class=\"alert alert-danger\"><p>Incorrect Username or Password</p></div>")
log.Println("Login Failed")

if !validateForm(w,r,ps) {
data["message"] = template.HTML("<div id=\"message\" class=\"alert alert-danger\"><p>Empty Username or Password</p></div>")
}else{
if loginAction(w, r, ps) {
util.Redirect(w, r, "index", 302)
} else {
//the best solution instead of using ajax request
data["message"] = template.HTML("<div id=\"message\" class=\"alert alert-danger\"><p>Incorrect Username or Password</p></div>")
log.Println("Login Failed")
}
}
}
util.SafeRender(w, "template.login", data)
Expand Down Expand Up @@ -98,6 +103,15 @@ func Logout(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
util.Redirect(w, r, "login", 302)
}

func validateForm(w http.ResponseWriter, r *http.Request, _ httprouter.Params)bool{
uname := r.FormValue("username")
pass := r.FormValue("password")
if uname == "" || pass == ""{
return false
}
return true
}

/* type to handle user data that return form query */
type UserData struct {
id int
Expand Down

0 comments on commit 3458f61

Please sign in to comment.