Added sessions to the backend, a basic form of it at least

This commit is contained in:
2024-05-20 17:35:16 +01:00
parent 2179040833
commit 8a625b5fa8
3 changed files with 11 additions and 8 deletions
+3 -3
View File
@@ -11,17 +11,17 @@ import (
"github.com/golang-jwt/jwt/v4"
)
func isAuthenticated(r *http.Request) bool {
func isAuthenticated(r *http.Request) (bool, string) {
cookies := r.Cookies()
for _, c := range cookies {
if c.Name == "token" {
// Check if the token exists and if its date is valid
expirationTime := time.Now()
return expirationTime.Unix() >= c.Expires.Unix()
return expirationTime.Unix() >= c.Expires.Unix(), c.Value
}
}
return false
return false, ""
}
func Authenticate(w http.ResponseWriter, r *http.Request) {
+2 -1
View File
@@ -6,7 +6,8 @@ import (
)
func EditorPage(w http.ResponseWriter, r *http.Request) {
if !isAuthenticated(r) {
authd, _ := isAuthenticated(r)
if !authd {
views.LoginPage(w, r)
return
}
+6 -4
View File
@@ -8,8 +8,7 @@ import (
"io"
"log"
"net/http"
"thermalFax/views"
"thermalFax/models"
)
type PrintServerResponse struct {
@@ -58,11 +57,14 @@ func sendRequestToPrinter(msg string) (PrintServerResponse, error) {
// sendFax is a function that receives the data from the fronted and
// sends it to the print server
func SendFax(w http.ResponseWriter, r *http.Request) {
if !isAuthenticated(r) {
views.LoginPage(w, r)
authd, token := isAuthenticated(r)
if !authd {
return
}
session, _ := models.GetSession(token)
log.Printf(" | User %s request a fax.", session.User)
data, _ := io.ReadAll(r.Body)
log.Printf("| Received Request: %s", data)