#1 add authorization with firebase
Co-authored-by: Tim <timwundenberg@outlook.de> Reviewed-on: tim/me-fit#2
This commit was merged in pull request #2.
This commit is contained in:
28
api/middleware/auth.go
Normal file
28
api/middleware/auth.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"api/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")
|
||||
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))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user