Added sessions to the backend, a basic form of it at least
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user