Moved getSession to frontend/sessions.go
This commit is contained in:
parent
a9bef63174
commit
afa1b65563
@ -1,11 +1,13 @@
|
|||||||
package frontend
|
package frontend
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/sessions"
|
||||||
b "streifling.com/jason/cpolis/cmd/backend"
|
b "streifling.com/jason/cpolis/cmd/backend"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -26,6 +28,25 @@ func saveSession(w http.ResponseWriter, r *http.Request, s *b.CookieStore, u *b.
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getSession is used for verifying that the user is logged in and returns their session and an error.
|
||||||
|
func getSession(w http.ResponseWriter, r *http.Request, c *b.Config, s *b.CookieStore) (*sessions.Session, error) {
|
||||||
|
msg := "Keine gültige Session. Bitte erneut anmelden."
|
||||||
|
tmpl, tmplErr := template.ParseFiles(c.WebDir+"/templates/index.html", c.WebDir+"/templates/login.html")
|
||||||
|
|
||||||
|
session, err := s.Get(r, "cookie")
|
||||||
|
if err != nil {
|
||||||
|
template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", msg)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if session.IsNew {
|
||||||
|
template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", msg)
|
||||||
|
return session, errors.New("error: no existing session")
|
||||||
|
}
|
||||||
|
|
||||||
|
return session, nil
|
||||||
|
}
|
||||||
|
|
||||||
func HomePage(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
func HomePage(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
numRows, err := db.CountEntries("users")
|
numRows, err := db.CountEntries("users")
|
||||||
|
@ -1,29 +0,0 @@
|
|||||||
package frontend
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"html/template"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/gorilla/sessions"
|
|
||||||
b "streifling.com/jason/cpolis/cmd/backend"
|
|
||||||
)
|
|
||||||
|
|
||||||
// getSession is used for verifying that the user is logged in and returns their session and an error.
|
|
||||||
func getSession(w http.ResponseWriter, r *http.Request, c *b.Config, s *b.CookieStore) (*sessions.Session, error) {
|
|
||||||
msg := "Keine gültige Session. Bitte erneut anmelden."
|
|
||||||
tmpl, tmplErr := template.ParseFiles(c.WebDir+"/templates/index.html", c.WebDir+"/templates/login.html")
|
|
||||||
|
|
||||||
session, err := s.Get(r, "cookie")
|
|
||||||
if err != nil {
|
|
||||||
template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if session.IsNew {
|
|
||||||
template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
return session, errors.New("error: no existing session")
|
|
||||||
}
|
|
||||||
|
|
||||||
return session, nil
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user