package auth

import (
	"github.com/gin-gonic/gin"
	"bwallet/pkg/e"
	"bwallet/models"
	"fmt"
	"net/http"
)

func AUTH() gin.HandlerFunc {
	return func(c *gin.Context) {
		var code int
		var data interface{}

		code = e.SUCCESS
		token := c.Request.Header.Get("Token")
		if token == "" {
			code = e.INVALID_PARAMS
		} else {
			_, err := models.CheckToken(token)
			fmt.Println(err)
			if err != nil {
				code = e.ERROR_AUTH_CHECK_TOKEN_FAIL
			}
		}

		if code != e.SUCCESS {
			c.JSON(http.StatusUnauthorized, gin.H{
				"code": code,
				"msg":  e.GetMsg(code),
				"data": data,
			})

			c.Abort()
			return
		}

		c.Next()
	}
}
