app redirects to login page is user isn't authenticated

This commit is contained in:
Suraj Patil 2016-05-09 23:06:35 +05:30
parent eb1bf99924
commit dacbef54e4
4 changed files with 370 additions and 338 deletions

View File

@ -22,7 +22,8 @@ import (
// UploadedFileHandler is used to handle the uploaded file related requests // UploadedFileHandler is used to handle the uploaded file related requests
func UploadedFileHandler(w http.ResponseWriter, r *http.Request) { func UploadedFileHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && sessions.IsLoggedIn(r) { if sessions.IsLoggedIn(r) {
if r.Method == "GET" {
token := r.URL.Path[len("/files/"):] token := r.URL.Path[len("/files/"):]
//file, err := db.GetFileName(token) //file, err := db.GetFileName(token)
@ -31,11 +32,13 @@ func UploadedFileHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./files/"+token) http.ServeFile(w, r, "./files/"+token)
//} //}
} }
}
} }
//AddTaskFunc is used to handle the addition of new task, "/add" URL //AddTaskFunc is used to handle the addition of new task, "/add" URL
func AddTaskFunc(w http.ResponseWriter, r *http.Request) { func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && sessions.IsLoggedIn(r) { // Will work only for POST requests, will redirect to home if sessions.IsLoggedIn(r) {
if r.Method == "POST" { // Will work only for POST requests, will redirect to home
var filelink string // will store the html when we have files to be uploaded, appened to the note content var filelink string // will store the html when we have files to be uploaded, appened to the note content
r.ParseForm() r.ParseForm()
file, handler, err := r.FormFile("uploadfile") file, handler, err := r.FormFile("uploadfile")
@ -120,14 +123,15 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusInternalServerError) http.Redirect(w, r, "/", http.StatusInternalServerError)
} }
}
} else { } else {
message = "Method not allowed" http.Redirect(w, r, "/login/", 302)
http.Redirect(w, r, "/", http.StatusFound)
} }
} }
//AddCategoryFunc used to add new categories to the database //AddCategoryFunc used to add new categories to the database
func AddCategoryFunc(w http.ResponseWriter, r *http.Request) { func AddCategoryFunc(w http.ResponseWriter, r *http.Request) {
if sessions.IsLoggedIn(r) {
r.ParseForm() r.ParseForm()
category := r.Form.Get("category") category := r.Form.Get("category")
if strings.Trim(category, " ") != "" { if strings.Trim(category, " ") != "" {
@ -139,40 +143,42 @@ func AddCategoryFunc(w http.ResponseWriter, r *http.Request) {
message = "Added category" message = "Added category"
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
} }
}
} else { } else {
message = "Invalid Category Name" http.Redirect(w, r, "/login/", 302)
http.Redirect(w, r, "/", http.StatusBadRequest)
} }
} }
//EditTaskFunc is used to edit tasks, handles "/edit/" URL //EditTaskFunc is used to edit tasks, handles "/edit/" URL
func EditTaskFunc(w http.ResponseWriter, r *http.Request) { func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && sessions.IsLoggedIn(r) { if sessions.IsLoggedIn(r) {
if r.Method == "GET" {
id, err := strconv.Atoi(r.URL.Path[len("/edit/"):]) id, err := strconv.Atoi(r.URL.Path[len("/edit/"):])
if err != nil { if err != nil {
log.Println(err) log.Println(err)
http.Redirect(w, r, "/", http.StatusBadRequest) http.Redirect(w, r, "/", http.StatusBadRequest)
} else { } else {
redirectUrl := utils.GetRedirectUrl(r.Referer()) redirectURL := utils.GetRedirectUrl(r.Referer())
task, err := db.GetTaskByID(id) task, err := db.GetTaskByID(id)
categories := db.GetCategories() categories := db.GetCategories()
task.Categories = categories task.Categories = categories
task.Referer = redirectUrl task.Referer = redirectURL
if err != nil { if err != nil {
task.Message = "Error fetching Tasks" task.Message = "Error fetching Tasks"
} }
editTemplate.Execute(w, task) editTemplate.Execute(w, task)
} }
}
} else { } else {
message = "Method not allowed" http.Redirect(w, r, "/login/", http.StatusFound)
http.Redirect(w, r, "/", http.StatusFound)
} }
} }
//AddCommentFunc will be used //AddCommentFunc will be used
func AddCommentFunc(w http.ResponseWriter, r *http.Request) { func AddCommentFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && sessions.IsLoggedIn(r) { if sessions.IsLoggedIn(r) {
if r.Method == "POST" {
r.ParseForm() r.ParseForm()
text := r.Form.Get("commentText") text := r.Form.Get("commentText")
id := r.Form.Get("taskID") id := r.Form.Get("taskID")
@ -196,4 +202,7 @@ func AddCommentFunc(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
} }
} else {
http.Redirect(w, r, "/login", 302)
}
} }

View File

@ -18,14 +18,15 @@ import (
func TrashTaskFunc(w http.ResponseWriter, r *http.Request) { func TrashTaskFunc(w http.ResponseWriter, r *http.Request) {
//for best UX we want the user to be returned to the page making //for best UX we want the user to be returned to the page making
//the delete transaction, we use the r.Referer() function to get the link //the delete transaction, we use the r.Referer() function to get the link
redirectUrl := utils.GetRedirectUrl(r.Referer()) redirectURL := utils.GetRedirectUrl(r.Referer())
if r.Method == "GET" && sessions.IsLoggedIn(r) { if sessions.IsLoggedIn(r) {
if r.Method == "GET" {
id, err := strconv.Atoi(r.URL.Path[len("/trash/"):]) id, err := strconv.Atoi(r.URL.Path[len("/trash/"):])
if err != nil { if err != nil {
log.Println("TrashTaskFunc", err) log.Println("TrashTaskFunc", err)
message = "Incorrect command" message = "Incorrect command"
http.Redirect(w, r, redirectUrl, http.StatusFound) http.Redirect(w, r, redirectURL, http.StatusFound)
} else { } else {
err = db.TrashTask(id) err = db.TrashTask(id)
if err != nil { if err != nil {
@ -33,17 +34,18 @@ func TrashTaskFunc(w http.ResponseWriter, r *http.Request) {
} else { } else {
message = "Task trashed" message = "Task trashed"
} }
http.Redirect(w, r, redirectUrl, http.StatusFound) http.Redirect(w, r, redirectURL, http.StatusFound)
}
} }
} else { } else {
message = "Method not allowed" http.Redirect(w, r, "/login/", 302)
http.Redirect(w, r, redirectUrl, http.StatusFound)
} }
} }
//RestoreTaskFunc is used to restore task from trash, handles "/restore/" URL //RestoreTaskFunc is used to restore task from trash, handles "/restore/" URL
func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) { func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && sessions.IsLoggedIn(r) { if sessions.IsLoggedIn(r) {
if r.Method == "GET" {
id, err := strconv.Atoi(r.URL.Path[len("/restore/"):]) id, err := strconv.Atoi(r.URL.Path[len("/restore/"):])
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -57,15 +59,16 @@ func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) {
} }
http.Redirect(w, r, "/deleted/", http.StatusFound) http.Redirect(w, r, "/deleted/", http.StatusFound)
} }
}
} else { } else {
message = "Method not allowed" http.Redirect(w, r, "/login/", 302)
http.Redirect(w, r, "/", http.StatusFound)
} }
} }
//DeleteTaskFunc is used to delete a task, trash = move to recycle bin, delete = permanent delete //DeleteTaskFunc is used to delete a task, trash = move to recycle bin, delete = permanent delete
func DeleteTaskFunc(w http.ResponseWriter, r *http.Request) { func DeleteTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && sessions.IsLoggedIn(r) { if sessions.IsLoggedIn(r) {
if r.Method == "GET" {
id := r.URL.Path[len("/delete/"):] id := r.URL.Path[len("/delete/"):]
if id == "all" { if id == "all" {
err := db.DeleteAll() err := db.DeleteAll()
@ -89,15 +92,16 @@ func DeleteTaskFunc(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/deleted", http.StatusFound) http.Redirect(w, r, "/deleted", http.StatusFound)
} }
} }
}
} else { } else {
message = "Method not allowed" http.Redirect(w, r, "/login/", 302)
http.Redirect(w, r, "/", http.StatusFound)
} }
} }
//RestoreFromCompleteFunc restores the task from complete to pending //RestoreFromCompleteFunc restores the task from complete to pending
func RestoreFromCompleteFunc(w http.ResponseWriter, r *http.Request) { func RestoreFromCompleteFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && sessions.IsLoggedIn(r) { if sessions.IsLoggedIn(r) {
if r.Method == "GET" {
id, err := strconv.Atoi(r.URL.Path[len("/incomplete/"):]) id, err := strconv.Atoi(r.URL.Path[len("/incomplete/"):])
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -111,15 +115,16 @@ func RestoreFromCompleteFunc(w http.ResponseWriter, r *http.Request) {
} }
http.Redirect(w, r, "/completed", http.StatusFound) http.Redirect(w, r, "/completed", http.StatusFound)
} }
}
} else { } else {
message = "Method not allowed" http.Redirect(w, r, "/login/", 302)
http.Redirect(w, r, "/completed", http.StatusFound)
} }
} }
//DeleteCategoryFunc will delete any category //DeleteCategoryFunc will delete any category
func DeleteCategoryFunc(w http.ResponseWriter, r *http.Request) { func DeleteCategoryFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && sessions.IsLoggedIn(r) { if sessions.IsLoggedIn(r) {
if r.Method == "GET" {
categoryName := r.URL.Path[len("/del-category/"):] categoryName := r.URL.Path[len("/del-category/"):]
err := db.DeleteCategoryByName(categoryName) err := db.DeleteCategoryByName(categoryName)
if err != nil { if err != nil {
@ -130,11 +135,15 @@ func DeleteCategoryFunc(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
} }
} else {
http.Redirect(w, r, "/login/", 302)
}
} }
//DeleteCommentFunc will delete any category //DeleteCommentFunc will delete any category
func DeleteCommentFunc(w http.ResponseWriter, r *http.Request) { func DeleteCommentFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && sessions.IsLoggedIn(r) { if sessions.IsLoggedIn(r) {
if r.Method == "GET" {
id := r.URL.Path[len("/del-comment/"):] id := r.URL.Path[len("/del-comment/"):]
commentID, err := strconv.Atoi(id) commentID, err := strconv.Atoi(id)
if err != nil { if err != nil {
@ -152,4 +161,7 @@ func DeleteCommentFunc(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
} }
} else {
http.Redirect(w, r, "/login/", 302)
}
} }

View File

@ -56,7 +56,8 @@ func PopulateTemplates() {
//CompleteTaskFunc is used to show the complete tasks, handles "/completed/" url //CompleteTaskFunc is used to show the complete tasks, handles "/completed/" url
func CompleteTaskFunc(w http.ResponseWriter, r *http.Request) { func CompleteTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && sessions.IsLoggedIn(r) { if sessions.IsLoggedIn(r) {
if r.Method == "GET" {
redirectURL := utils.GetRedirectUrl(r.Referer()) redirectURL := utils.GetRedirectUrl(r.Referer())
id, err := strconv.Atoi(r.URL.Path[len("/complete/"):]) id, err := strconv.Atoi(r.URL.Path[len("/complete/"):])
if err != nil { if err != nil {
@ -70,15 +71,16 @@ func CompleteTaskFunc(w http.ResponseWriter, r *http.Request) {
} }
http.Redirect(w, r, redirectURL, http.StatusFound) http.Redirect(w, r, redirectURL, http.StatusFound)
} }
}
} else { } else {
message = "Method not allowed" http.Redirect(w, r, "/login/", 302)
http.Redirect(w, r, "/", http.StatusFound)
} }
} }
//SearchTaskFunc is used to handle the /search/ url, handles the search function //SearchTaskFunc is used to handle the /search/ url, handles the search function
func SearchTaskFunc(w http.ResponseWriter, r *http.Request) { func SearchTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && sessions.IsLoggedIn(r) { if sessions.IsLoggedIn(r) {
if r.Method == "POST" {
r.ParseForm() r.ParseForm()
query := r.Form.Get("query") query := r.Form.Get("query")
@ -88,15 +90,16 @@ func SearchTaskFunc(w http.ResponseWriter, r *http.Request) {
context.Categories = categories context.Categories = categories
searchTemplate.Execute(w, context) searchTemplate.Execute(w, context)
}
} else { } else {
message = "Method not allowed" http.Redirect(w, r, "/login/", 302)
http.Redirect(w, r, "/", http.StatusFound)
} }
} }
//UpdateTaskFunc is used to update a task, handes "/update/" URL //UpdateTaskFunc is used to update a task, handes "/update/" URL
func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) { func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && sessions.IsLoggedIn(r) { if sessions.IsLoggedIn(r) {
if r.Method == "POST" {
r.ParseForm() r.ParseForm()
id, err := strconv.Atoi(r.Form.Get("id")) id, err := strconv.Atoi(r.Form.Get("id"))
if err != nil { if err != nil {
@ -117,16 +120,16 @@ func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) {
log.Println(message) log.Println(message)
} }
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
}
} else { } else {
message = "Method not allowed" http.Redirect(w, r, "/login/", 302)
http.Redirect(w, r, "/", http.StatusFound)
} }
} }
//UpdateCategoryFunc is used to update a task, handes "/upd-category/" URL //UpdateCategoryFunc is used to update a task, handes "/upd-category/" URL
func UpdateCategoryFunc(w http.ResponseWriter, r *http.Request) { func UpdateCategoryFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && sessions.IsLoggedIn(r) { if sessions.IsLoggedIn(r) {
if r.Method == "POST" {
var redirectURL string var redirectURL string
r.ParseForm() r.ParseForm()
oldName := r.URL.Path[len("/upd-category/"):] oldName := r.URL.Path[len("/upd-category/"):]
@ -144,4 +147,7 @@ func UpdateCategoryFunc(w http.ResponseWriter, r *http.Request) {
log.Println("redirecting to " + redirectURL) log.Println("redirecting to " + redirectURL)
http.Redirect(w, r, redirectURL, http.StatusFound) http.Redirect(w, r, redirectURL, http.StatusFound)
} }
} else {
http.Redirect(w, r, "/login/", 302)
}
} }

View File

@ -25,12 +25,13 @@ var err error
//ShowAllTasksFunc is used to handle the "/" URL which is the default ons //ShowAllTasksFunc is used to handle the "/" URL which is the default ons
//TODO add http404 error //TODO add http404 error
func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) { func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && sessions.IsLoggedIn(r) { if sessions.IsLoggedIn(r) == true {
if r.Method == "GET" {
context, err := db.GetTasks("pending", "") context, err := db.GetTasks("pending", "")
categories := db.GetCategories() categories := db.GetCategories()
if err != nil { if err != nil {
http.Redirect(w, r, "/", http.StatusInternalServerError) http.Redirect(w, r, "/", http.StatusInternalServerError)
} } else {
if message != "" { if message != "" {
context.Message = message context.Message = message
} }
@ -41,15 +42,17 @@ func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) {
cookie := http.Cookie{Name: "csrftoken", Value: "abcd", Expires: expiration} cookie := http.Cookie{Name: "csrftoken", Value: "abcd", Expires: expiration}
http.SetCookie(w, &cookie) http.SetCookie(w, &cookie)
homeTemplate.Execute(w, context) homeTemplate.Execute(w, context)
}
}
} else { } else {
message = "Method not allowed" http.Redirect(w, r, "/login/", 302)
http.Redirect(w, r, "/", http.StatusFound)
} }
} }
//ShowTrashTaskFunc is used to handle the "/trash" URL which is used to show the deleted tasks //ShowTrashTaskFunc is used to handle the "/trash" URL which is used to show the deleted tasks
func ShowTrashTaskFunc(w http.ResponseWriter, r *http.Request) { func ShowTrashTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && sessions.IsLoggedIn(r) { if sessions.IsLoggedIn(r) {
if r.Method == "GET" {
context, err := db.GetTasks("deleted", "") context, err := db.GetTasks("deleted", "")
categories := db.GetCategories() categories := db.GetCategories()
context.Categories = categories context.Categories = categories
@ -61,15 +64,16 @@ func ShowTrashTaskFunc(w http.ResponseWriter, r *http.Request) {
message = "" message = ""
} }
deletedTemplate.Execute(w, context) deletedTemplate.Execute(w, context)
}
} else { } else {
message = "Method not allowed" http.Redirect(w, r, "/login/", 302)
http.Redirect(w, r, "/", http.StatusFound)
} }
} }
//ShowCompleteTasksFunc is used to populate the "/completed/" URL //ShowCompleteTasksFunc is used to populate the "/completed/" URL
func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) { func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && sessions.IsLoggedIn(r) { if sessions.IsLoggedIn(r) {
if r.Method == "GET" {
context, err := db.GetTasks("completed", "") context, err := db.GetTasks("completed", "")
categories := db.GetCategories() categories := db.GetCategories()
context.Categories = categories context.Categories = categories
@ -77,15 +81,16 @@ func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/completed", http.StatusInternalServerError) http.Redirect(w, r, "/completed", http.StatusInternalServerError)
} }
completedTemplate.Execute(w, context) completedTemplate.Execute(w, context)
}
} else { } else {
message = "Method not allowed" http.Redirect(w, r, "/login/", 302)
http.Redirect(w, r, "/", http.StatusFound)
} }
} }
//ShowCategoryFunc will populate the /category/<id> URL which shows all the tasks related //ShowCategoryFunc will populate the /category/<id> URL which shows all the tasks related
// to that particular category // to that particular category
func ShowCategoryFunc(w http.ResponseWriter, r *http.Request) { func ShowCategoryFunc(w http.ResponseWriter, r *http.Request) {
if sessions.IsLoggedIn(r) {
if r.Method == "GET" && sessions.IsLoggedIn(r) { if r.Method == "GET" && sessions.IsLoggedIn(r) {
category := r.URL.Path[len("/category/"):] category := r.URL.Path[len("/category/"):]
context, err := db.GetTasks("", category) context, err := db.GetTasks("", category)
@ -104,8 +109,8 @@ func ShowCategoryFunc(w http.ResponseWriter, r *http.Request) {
cookie := http.Cookie{Name: "csrftoken", Value: "abcd", Expires: expiration} cookie := http.Cookie{Name: "csrftoken", Value: "abcd", Expires: expiration}
http.SetCookie(w, &cookie) http.SetCookie(w, &cookie)
homeTemplate.Execute(w, context) homeTemplate.Execute(w, context)
}
} else { } else {
message = "Method not allowed" http.Redirect(w, r, "/login/", 302)
http.Redirect(w, r, "/", http.StatusFound)
} }
} }