added sign up feature & login works with new sign up
This commit is contained in:
parent
13a4d4f80e
commit
8029fe9ab9
1
main.go
1
main.go
|
@ -37,6 +37,7 @@ func main() {
|
|||
//Login logout
|
||||
http.HandleFunc("/login/", views.LoginFunc)
|
||||
http.HandleFunc("/logout/", views.RequiresLogin(views.LogoutFunc))
|
||||
http.HandleFunc("/signup/", views.SignUpFunc)
|
||||
|
||||
http.HandleFunc("/add-category/", views.RequiresLogin(views.AddCategoryFunc))
|
||||
http.HandleFunc("/add-comment/", views.RequiresLogin(views.AddCommentFunc))
|
||||
|
|
|
@ -11,8 +11,8 @@ var Store = sessions.NewCookieStore([]byte("secret-password"))
|
|||
|
||||
//IsLoggedIn will check if the user has an active session and return True
|
||||
func IsLoggedIn(r *http.Request) bool {
|
||||
session, _ := Store.Get(r, "session")
|
||||
if session.Values["loggedin"] == "true" {
|
||||
session, err := Store.Get(r, "session")
|
||||
if err == nil && (session.Values["loggedin"] == "true") {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
<div class="form-group">
|
||||
<input type="text" name="username" class="form-control" placeholder="Username" />
|
||||
<input type="password" name="password" class="form-control" placeholder="Password" />
|
||||
<input type="email" name="email" class="form-control" placeholder="demo@demo.com" />
|
||||
</div>
|
||||
|
||||
<div class="modal-footer">
|
||||
|
|
|
@ -134,3 +134,23 @@ func UpdateCategoryFunc(w http.ResponseWriter, r *http.Request) {
|
|||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package views
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/thewhitetulip/Tasks/db"
|
||||
"github.com/thewhitetulip/Tasks/sessions"
|
||||
)
|
||||
|
||||
|
@ -34,22 +36,29 @@ func LoginFunc(w http.ResponseWriter, r *http.Request) {
|
|||
session, err := sessions.Store.Get(r, "session")
|
||||
|
||||
if err != nil {
|
||||
loginTemplate.Execute(w, nil) // in case of error during fetching session info, execute login template
|
||||
} else {
|
||||
isLoggedIn := session.Values["loggedin"]
|
||||
if isLoggedIn != "true" {
|
||||
if r.Method == "POST" {
|
||||
if r.FormValue("password") == "secret" && r.FormValue("username") == "user" {
|
||||
log.Println("error identifying session")
|
||||
loginTemplate.Execute(w, nil)
|
||||
return
|
||||
}
|
||||
|
||||
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["username"] = username
|
||||
session.Save(r, w)
|
||||
log.Print("user ", username, " is authenticated")
|
||||
http.Redirect(w, r, "/", 302)
|
||||
return
|
||||
}
|
||||
} else if r.Method == "GET" {
|
||||
log.Print("Invalid user " + username)
|
||||
loginTemplate.Execute(w, nil)
|
||||
}
|
||||
} else {
|
||||
http.Redirect(w, r, "/", 302)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue