implement JWT tokens
This commit is contained in:
12
api/user.go
12
api/user.go
@@ -52,17 +52,17 @@ func Whoami(w http.ResponseWriter, r *http.Request) {
|
||||
func LoginCtx(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
slog.Debug("user: entering LoginCtx middleware")
|
||||
username, ok := r.Context().Value(usernameKey).(string)
|
||||
if !ok || username == "" {
|
||||
slog.Debug("user: no username provided, assuming anonymous user")
|
||||
userID, ok := r.Context().Value(userIDKey).(uuid.UUID)
|
||||
if !ok || userID == uuid.Nil {
|
||||
slog.Debug("user: no user ID provided, assuming anonymous user")
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
slog.Debug("user: fetching user by username", "username", username)
|
||||
user, err := dbGetUserByName(username)
|
||||
slog.Debug("user: fetching user by user ID", "userID", userID)
|
||||
user, err := dbGetUser(userID.String())
|
||||
if err != nil {
|
||||
slog.Error("user: failed to fetch user by username", "username", username, "error", err)
|
||||
slog.Error("user: failed to fetch user by user ID", "userID", userID, "error", err)
|
||||
render.Render(w, r, ErrNotFound)
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user