package users import ( "time" "ttime/internal/types" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/log" "github.com/golang-jwt/jwt/v5" ) // @Summary LoginRenews // @Description Renews the users token. // @Tags Auth // @Produce json // @Security JWT // @Success 200 {object} types.Token "Successfully signed token for user" // @Failure 401 {string} string "Unauthorized" // @Failure 500 {string} string "Internal server error" // @Router /loginrenew [post] // // LoginRenew renews the users token func LoginRenew(c *fiber.Ctx) error { user := c.Locals("user").(*jwt.Token) log.Info("Renewing token for user:", user.Claims.(jwt.MapClaims)["name"]) // Renewing the token means we trust whatever is already in the token claims := user.Claims.(jwt.MapClaims) // 72 hour expiration time claims["exp"] = time.Now().Add(time.Hour * 72).Unix() // Create token with old claims, but new expiration time token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ "name": claims["name"], "admin": claims["admin"], "exp": claims["exp"], }) // Sign it with top secret key t, err := token.SignedString([]byte("secret")) if err != nil { log.Warn("Error signing token") return c.SendStatus(fiber.StatusInternalServerError) // 500 } log.Info("Successfully renewed token for user:", user.Claims.(jwt.MapClaims)["name"]) return c.JSON(types.Token{Token: t}) }