changing returned values
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ func (store *Store) GetPosts(ctx *gin.Context) {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, gin.H{"data": posts})
|
||||
ctx.JSON(http.StatusOK, posts)
|
||||
}
|
||||
|
||||
func (store *Store) GetPost(ctx *gin.Context) {
|
||||
@@ -30,7 +30,7 @@ func (store *Store) GetPost(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{"data": post})
|
||||
ctx.JSON(http.StatusOK, post)
|
||||
}
|
||||
|
||||
func (store *Store) CreatePost(ctx *gin.Context) {
|
||||
@@ -62,7 +62,7 @@ func (store *Store) CreatePost(ctx *gin.Context) {
|
||||
post := models.Post{Title: input.Title, Content: input.Content, AuthorID: userID}
|
||||
store.DB.Create(&post)
|
||||
|
||||
ctx.JSON(http.StatusCreated, gin.H{"data": post})
|
||||
ctx.JSON(http.StatusCreated, post)
|
||||
}
|
||||
|
||||
func (store *Store) UpdatePost(ctx *gin.Context) {
|
||||
@@ -97,7 +97,7 @@ func (store *Store) UpdatePost(ctx *gin.Context) {
|
||||
|
||||
var input CreatePostInput
|
||||
if err := ctx.ShouldBindBodyWithJSON(&input); err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
ctx.JSON(http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -105,14 +105,14 @@ func (store *Store) UpdatePost(ctx *gin.Context) {
|
||||
post.Content = input.Content
|
||||
store.DB.Save(&post)
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{"data": post})
|
||||
ctx.JSON(http.StatusOK, post)
|
||||
}
|
||||
|
||||
func (store *Store) DeletePost(ctx *gin.Context) {
|
||||
postID := ctx.Param("id")
|
||||
var post models.Post
|
||||
if err := store.DB.First(&post, postID).Error; err != nil {
|
||||
ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||
ctx.JSON(http.StatusNotFound, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -139,5 +139,5 @@ func (store *Store) DeletePost(ctx *gin.Context) {
|
||||
}
|
||||
|
||||
store.DB.Delete(&post)
|
||||
ctx.JSON(http.StatusOK, gin.H{"data": post})
|
||||
ctx.JSON(http.StatusOK, post)
|
||||
}
|
||||
|
||||
@@ -17,13 +17,13 @@ type UserCredentials struct {
|
||||
func (store *Store) CreateUser(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
|
||||
}
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(input.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
ctx.JSON(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func (store *Store) CreateUser(ctx *gin.Context) {
|
||||
// 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
|
||||
}
|
||||
|
||||
@@ -54,27 +54,27 @@ func (store *Store) CreateUser(ctx *gin.Context) {
|
||||
true, true,
|
||||
)
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{"data": user})
|
||||
ctx.JSON(http.StatusOK, user)
|
||||
}
|
||||
|
||||
func (store *Store) GetUser(ctx *gin.Context) {
|
||||
userID := ctx.Param("id")
|
||||
var user models.User
|
||||
if err := store.DB.First(&user, userID).Error; err != nil {
|
||||
ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||
ctx.JSON(http.StatusNotFound, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{"data": user})
|
||||
ctx.JSON(http.StatusOK, user)
|
||||
}
|
||||
|
||||
func (store *Store) GetUsers(ctx *gin.Context) {
|
||||
var users []models.User
|
||||
if err := store.DB.Find(&users).Error; err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
ctx.JSON(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, gin.H{"data": users})
|
||||
ctx.JSON(http.StatusOK, users)
|
||||
}
|
||||
|
||||
func (store *Store) UpdateUser(ctx *gin.Context) {
|
||||
@@ -98,7 +98,7 @@ func (store *Store) UpdateUser(ctx *gin.Context) {
|
||||
|
||||
var user models.User
|
||||
if err := store.DB.First(&user, userID).Error; err != nil {
|
||||
ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||
ctx.JSON(http.StatusNotFound, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -126,11 +126,10 @@ func (store *Store) DeleteUser(ctx *gin.Context) {
|
||||
|
||||
var user models.User
|
||||
if err := store.DB.First(&user, userID).Error; err != nil {
|
||||
ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||
ctx.JSON(http.StatusNotFound, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
store.DB.Delete(&user)
|
||||
ctx.JSON(http.StatusOK, gin.H{"data": user})
|
||||
|
||||
ctx.JSON(http.StatusOK, user)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user