card-jong-be/controller/utils.go

29 lines
571 B
Go
Raw Permalink Normal View History

2024-05-16 21:17:41 +00:00
package controller
import (
2024-05-16 22:15:52 +00:00
"fmt"
"net/http"
2024-05-16 21:17:41 +00:00
"strings"
)
2024-05-16 22:15:52 +00:00
func WriteError(err error, message string, writer *http.ResponseWriter) {
fmt.Printf("Error: %s\n", err)
(*writer).WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(*writer, "{\"error\": \"%s\"}", message)
}
func AuthHeaderIsValid(authHeader string) bool {
2024-05-16 21:17:41 +00:00
// (try to) get the Bearer token
reqToken := authHeader
if !strings.HasPrefix(reqToken, "Bearer ") {
return false
}
bearerToken := reqToken[7:]
// Check that the token is in the global map
2024-05-25 21:17:48 +00:00
_, ok := Users[bearerToken]
2024-05-16 21:17:41 +00:00
return ok
}