+
diff --git a/logout/index.html b/logout/index.html
new file mode 100644
index 0000000..690d0bc
--- /dev/null
+++ b/logout/index.html
@@ -0,0 +1,11 @@
+{{define "body"}}
+
+{{end}}
diff --git a/main.go b/main.go
index 8a7cb17..16e9a55 100644
--- a/main.go
+++ b/main.go
@@ -18,13 +18,7 @@ var store = sessions.NewCookieStore(key)
var emailrx = regexp.MustCompile("/^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/");
-type LoginData struct {
- username string
- password string
-}
-
-
-func secureheaders(next http.Handler) http.Handler {
+func secure_headers(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-XSS-Protection", "1; mode=block")
w.Header().Set("X-Frame-Options", "deny")
@@ -35,6 +29,20 @@ func secureheaders(next http.Handler) http.Handler {
return http.HandlerFunc(fn)
}
+func require_authenticated_user(next http.HandlerFunc) http.HandlerFunc {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ // If the user is not authenticated, redirect them to the login page and
+ // return from the middleware chain so that no subsequent handlers in
+ // the chain are executed.
+ if authenticated_user(r) == 0 {
+ http.Redirect(w, r, "/login", http.StatusSeeOther)
+ return
+ }
+ // Otherwise call the next handler in the chain.
+ next.ServeHTTP(w, r)
+ })
+}
+
func main() {
addr := flag.String("addr", ":8080", "HTTP network address")
flag.Parse()
@@ -82,12 +90,13 @@ func main() {
mux.HandleFunc("/", home)
mux.HandleFunc("/login", login)
+ mux.HandleFunc("/logout", logout)
mux.HandleFunc("/register", register)
- mux.HandleFunc("/account", account)
+ mux.HandleFunc("/account", require_authenticated_user(account))
- log.Fatal(http.ListenAndServe(*addr, secureheaders(mux)))
+ log.Fatal(http.ListenAndServe(*addr, secure_headers(mux)))
}
//cookie := http.Cookie{
diff --git a/models/models.go b/models/models.go
index 42c026b..8cc9f14 100644
--- a/models/models.go
+++ b/models/models.go
@@ -5,7 +5,7 @@ import "time"
import "golang.org/x/crypto/bcrypt"
import "database/sql"
import _ "github.com/lib/pq"
-import "fmt"
+import _ "fmt"
var Errnorecord = errors.New("no matching record found")
var Errinvalidcredentials = errors.New("invalid credentials")
@@ -44,7 +44,6 @@ func (m *Usermodel) Get_account(id int32) (Account, error) {
row := m.DB.QueryRow(stmt, id)
var account Account
err := row.Scan(&account.Id, &account.Username, &account.Password, &account.Color, &account.Firstname, &account.Lastname, &account.Email, &account.Created)
- fmt.Println(err)
if err == sql.ErrNoRows {
return Account{}, sql.ErrNoRows
} else if err != nil {