From b681395287df198290f4f825837d5dce6d47bb96 Mon Sep 17 00:00:00 2001 From: Florian Beisel Date: Sun, 21 Jan 2024 16:17:35 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=8A=20Add=20logging=20to=20server.log?= =?UTF-8?q?=20when=20token=20generation=20fails?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- auth/handlers.go | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/auth/handlers.go b/auth/handlers.go index 5aaafb3..fcb4b0b 100644 --- a/auth/handlers.go +++ b/auth/handlers.go @@ -1,7 +1,22 @@ +// Copyright 2024 Florian Beisel +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package auth import ( "database/sql" + "log" "net/http" "git.beisel.it/florian/hostname-service/db" @@ -11,19 +26,19 @@ import ( "github.com/gin-gonic/gin" ) -// LoginHandler godoc +// LoginHandler godoc // -// @Summary User login -// @Description Authenticate user and return JWT token -// @Tags Authentication -// @Accept json -// @Produce json -// @Param loginCredentials body models.LoginCredentials true "Login Credentials" -// @Success 200 {object} models.TokenResponse "Successfully authenticated, JWT token returned" -// @Failure 400 {object} models.ErrorResponse "Invalid request body" -// @Failure 401 {object} models.ErrorResponse "Invalid login credentials" -// @Failure 500 {object} models.ErrorResponse "Internal server error" -// @Router /login [post] +// @Summary User login +// @Description Authenticate user and return JWT token +// @Tags Authentication +// @Accept json +// @Produce json +// @Param loginCredentials body models.LoginCredentials true "Login Credentials" +// @Success 200 {object} models.TokenResponse "Successfully authenticated, JWT token returned" +// @Failure 400 {object} models.ErrorResponse "Invalid request body" +// @Failure 401 {object} models.ErrorResponse "Invalid login credentials" +// @Failure 500 {object} models.ErrorResponse "Internal server error" +// @Router /login [post] func LoginHandler(c *gin.Context) { var creds models.LoginCredentials @@ -58,6 +73,7 @@ func LoginHandler(c *gin.Context) { token, err := GenerateToken(storedCreds.Username) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to generate token"}) + log.Printf("Error generating token %v", err) return }