Browse Source

display login error message when login post error

FuXiaoHei 11 years ago
parent
commit
d8a24aff8c
2 changed files with 25 additions and 18 deletions
  1. 19 15
      routers/user/user.go
  2. 6 3
      templates/user/signin.tmpl

+ 19 - 15
routers/user/user.go

@@ -24,22 +24,26 @@ func Profile(r render.Render) {
 }
 
 func SignIn(req *http.Request, r render.Render) {
-	if req.Method == "GET" {
-		r.HTML(200, "user/signin", map[string]interface{}{
-			"Title": "Log In",
-		})
-		return
-	}
-
-	// todo sign in
-	_, err := models.LoginUserPlain(req.FormValue("account"), req.FormValue("passwd"))
-	if err != nil {
-		r.HTML(200, "base/error", map[string]interface{}{
-			"Error": fmt.Sprintf("%v", err),
-		})
-		return
+	var (
+		errString string
+		account   string
+	)
+	if req.Method == "POST" {
+		account = req.FormValue("account")
+		_, err := models.LoginUserPlain(account, req.FormValue("passwd"))
+		if err == nil {
+			// login success
+			r.Redirect("/")
+			return
+		}
+		// login fail
+		errString = fmt.Sprintf("%v", err)
 	}
-	r.Redirect("/")
+	r.HTML(200, "user/signin", map[string]interface{}{
+		"Title":   "Log In",
+		"Error":   errString,
+		"Account": account,
+	})
 }
 
 func SignUp(req *http.Request, r render.Render) {

+ 6 - 3
templates/user/signin.tmpl

@@ -2,17 +2,20 @@
 {{template "base/navbar" .}}
 <div class="container" id="gogs-body">
     <form action="/user/signin" method="post" class="form-horizontal gogs-card" id="gogs-login-card">
-        <h3>Log in</h3>
+        <h3>Log in</h3>{{if .Error}}
+        <div class="form-group">
+            <div class="col-md-6 col-md-offset-3 alert alert-danger text-center"><strong>{{.Error}}</strong></div>
+        </div>{{end}}
         <div class="form-group">
             <label class="col-md-4 control-label">Username or Email: </label>
             <div class="col-md-6">
-                <input name="account" class="form-control" placeholder="Type your username or e-mail address">
+                <input name="account" class="form-control" placeholder="Type your username or e-mail address" value="{{.Account}}" required="required">
             </div>
         </div>
         <div class="form-group">
             <label class="col-md-4 control-label">Password: </label>
             <div class="col-md-6">
-                <input name="passwd" type="password" class="form-control" placeholder="Type your password">
+                <input name="passwd" type="password" class="form-control" placeholder="Type your password" required="required">
             </div>
         </div>
         <div class="form-group">