added sign up feature & login works with new sign up

This commit is contained in:
Suraj Patil 2016-05-13 13:53:23 +05:30
parent 13a4d4f80e
commit 8029fe9ab9
5 changed files with 48 additions and 17 deletions

View File

@ -37,6 +37,7 @@ func main() {
//Login logout //Login logout
http.HandleFunc("/login/", views.LoginFunc) http.HandleFunc("/login/", views.LoginFunc)
http.HandleFunc("/logout/", views.RequiresLogin(views.LogoutFunc)) http.HandleFunc("/logout/", views.RequiresLogin(views.LogoutFunc))
http.HandleFunc("/signup/", views.SignUpFunc)
http.HandleFunc("/add-category/", views.RequiresLogin(views.AddCategoryFunc)) http.HandleFunc("/add-category/", views.RequiresLogin(views.AddCategoryFunc))
http.HandleFunc("/add-comment/", views.RequiresLogin(views.AddCommentFunc)) http.HandleFunc("/add-comment/", views.RequiresLogin(views.AddCommentFunc))

View File

@ -11,8 +11,8 @@ var Store = sessions.NewCookieStore([]byte("secret-password"))
//IsLoggedIn will check if the user has an active session and return True //IsLoggedIn will check if the user has an active session and return True
func IsLoggedIn(r *http.Request) bool { func IsLoggedIn(r *http.Request) bool {
session, _ := Store.Get(r, "session") session, err := Store.Get(r, "session")
if session.Values["loggedin"] == "true" { if err == nil && (session.Values["loggedin"] == "true") {
return true return true
} }
return false return false

View File

@ -20,6 +20,7 @@
<div class="form-group"> <div class="form-group">
<input type="text" name="username" class="form-control" placeholder="Username" /> <input type="text" name="username" class="form-control" placeholder="Username" />
<input type="password" name="password" class="form-control" placeholder="Password" /> <input type="password" name="password" class="form-control" placeholder="Password" />
<input type="email" name="email" class="form-control" placeholder="demo@demo.com" />
</div> </div>
<div class="modal-footer"> <div class="modal-footer">

View File

@ -134,3 +134,23 @@ func UpdateCategoryFunc(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, redirectURL, http.StatusFound) http.Redirect(w, r, redirectURL, http.StatusFound)
} }
} }
//SignUpFunc will enable new users to sign up to our service
func SignUpFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
r.ParseForm()
username := r.Form.Get("username")
password := r.Form.Get("password")
email := r.Form.Get("email")
log.Println(username, password, email)
err := db.CreateUser(username, password, email)
if err != nil {
http.Error(w, "Unable to sign user up", http.StatusInternalServerError)
} else {
http.Redirect(w, r, "/login/", 302)
}
}
}

View File

@ -1,8 +1,10 @@
package views package views
import ( import (
"log"
"net/http" "net/http"
"github.com/thewhitetulip/Tasks/db"
"github.com/thewhitetulip/Tasks/sessions" "github.com/thewhitetulip/Tasks/sessions"
) )
@ -34,22 +36,29 @@ func LoginFunc(w http.ResponseWriter, r *http.Request) {
session, err := sessions.Store.Get(r, "session") session, err := sessions.Store.Get(r, "session")
if err != nil { if err != nil {
loginTemplate.Execute(w, nil) // in case of error during fetching session info, execute login template log.Println("error identifying session")
} else { loginTemplate.Execute(w, nil)
isLoggedIn := session.Values["loggedin"] return
if isLoggedIn != "true" { }
if r.Method == "POST" {
if r.FormValue("password") == "secret" && r.FormValue("username") == "user" { switch r.Method {
case "GET":
loginTemplate.Execute(w, nil)
case "POST":
log.Print("Inside POST")
r.ParseForm()
username := r.Form.Get("username")
password := r.Form.Get("password")
if (username != "" && password != "") && db.ValidUser(username, password) {
session.Values["loggedin"] = "true" session.Values["loggedin"] = "true"
session.Values["username"] = username
session.Save(r, w) session.Save(r, w)
log.Print("user ", username, " is authenticated")
http.Redirect(w, r, "/", 302) http.Redirect(w, r, "/", 302)
return return
} }
} else if r.Method == "GET" { log.Print("Invalid user " + username)
loginTemplate.Execute(w, nil) loginTemplate.Execute(w, nil)
} }
} else {
http.Redirect(w, r, "/", 302)
}
}
} }