changing returned values

This commit is contained in:
2025-12-09 17:27:41 +00:00
parent fb47883e59
commit 2f27d07274
3 changed files with 49 additions and 41 deletions

View File

@@ -6,18 +6,19 @@ import (
"adam-french.co.uk/backend/models"
"github.com/gin-gonic/gin"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)
func (store *Store) AuthMiddlewear(ctx *gin.Context) {
access_token, err := ctx.Cookie("access_token")
if err != nil {
ctx.AbortWithStatusJSON(401, gin.H{"error": "unauthorized"})
ctx.AbortWithStatusJSON(401, err.Error())
return
}
claims, err := store.Auth.VerifyJWT(access_token)
if err != nil {
ctx.AbortWithStatusJSON(401, gin.H{"error": err.Error()})
ctx.AbortWithStatusJSON(401, err.Error())
return
}
@@ -35,45 +36,54 @@ func (store *Store) CheckToken(ctx *gin.Context) {
claims, err := store.Auth.VerifyJWT(access_token)
if err != nil {
ctx.JSON(401, gin.H{"error": err.Error()})
ctx.JSON(401, err.Error())
return
}
ctx.JSON(http.StatusOK, gin.H{"data": gin.H{
"id": (*claims)["id"],
"username": (*claims)["username"],
"admin": (*claims)["admin"],
}})
userID, ok := (*claims)["id"].(uint)
if !ok {
ctx.JSON(401, gin.H{"error": "claims does not contain id"})
return
}
user := models.User{Model: gorm.Model{ID: userID}}
tx := store.DB.First(&user)
if tx.Error != nil {
ctx.JSON(http.StatusNotFound, tx.Error.Error())
return
}
ctx.JSON(http.StatusOK, user)
}
func (store *Store) RefreshToken(ctx *gin.Context) {
refreshToken, err := ctx.Cookie("refresh_token")
if err != nil {
ctx.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
ctx.JSON(http.StatusUnauthorized, err.Error())
return
}
claims, err := store.Auth.VerifyJWT(refreshToken)
if err != nil {
ctx.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
ctx.JSON(http.StatusUnauthorized, err.Error())
}
userId, ok := (*claims)["id"].(uint)
userID, ok := (*claims)["id"].(uint)
if !ok {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "invalid token claims"})
return
}
user := models.User{}
tx := store.DB.First(&user, userId)
tx := store.DB.First(&user, userID)
if tx.Error != nil {
ctx.JSON(http.StatusNotFound, gin.H{"error": tx.Error.Error()})
ctx.JSON(http.StatusNotFound, tx.Error.Error())
return
}
tokens, err := store.Auth.GenerateJWT(&user)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
ctx.JSON(http.StatusInternalServerError, err.Error())
return
}
@@ -94,31 +104,30 @@ func (store *Store) RefreshToken(ctx *gin.Context) {
true, true,
)
ctx.JSON(http.StatusAccepted, gin.H{"data": user})
ctx.JSON(http.StatusAccepted, user)
}
func (store *Store) Login(ctx *gin.Context) {
var input UserCredentials
if err := ctx.ShouldBindBodyWithJSON(&input); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
ctx.JSON(http.StatusBadRequest, err.Error())
return
}
user := models.User{Username: input.Username}
user := models.User{}
if err := store.DB.Where("username = ?", input.Username).First(&user).Error; err != nil {
ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
ctx.JSON(http.StatusNotFound, err.Error())
return
}
if err := bcrypt.CompareHashAndPassword(user.Password, []byte(input.Password)); err != nil {
ctx.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
ctx.JSON(http.StatusUnauthorized, err.Error())
return
}
// Generate JWT token
tokens, err := store.Auth.GenerateJWT(&user)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
ctx.JSON(http.StatusInternalServerError, err.Error())
return
}
@@ -139,5 +148,5 @@ func (store *Store) Login(ctx *gin.Context) {
true, true,
)
ctx.JSON(http.StatusAccepted, gin.H{"data": user})
ctx.JSON(http.StatusAccepted, user)
}