#17 add dockerfile for api

This commit is contained in:
Tim
2024-07-28 15:07:19 +02:00
parent 2d2c4008f2
commit de806cc657
12 changed files with 34 additions and 30 deletions

View File

@@ -0,0 +1,34 @@
package middleware
import (
"api/src/utils"
"context"
"net/http"
)
type ContextKey string
const TOKEN_KEY ContextKey = "token"
func EnsureAuth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tokenStr := r.Header.Get("Authorization")
if (tokenStr == "") || (len(tokenStr) < len("Bearer ")) {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
tokenStr = tokenStr[len("Bearer "):]
token, err := utils.VerifyToken(tokenStr)
if err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
var newContext = context.WithValue(r.Context(), TOKEN_KEY, token)
next.ServeHTTP(w, r.WithContext(newContext))
})
}