changing returned values
This commit is contained in:
@@ -6,18 +6,19 @@ import (
|
|||||||
"adam-french.co.uk/backend/models"
|
"adam-french.co.uk/backend/models"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (store *Store) AuthMiddlewear(ctx *gin.Context) {
|
func (store *Store) AuthMiddlewear(ctx *gin.Context) {
|
||||||
access_token, err := ctx.Cookie("access_token")
|
access_token, err := ctx.Cookie("access_token")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.AbortWithStatusJSON(401, gin.H{"error": "unauthorized"})
|
ctx.AbortWithStatusJSON(401, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
claims, err := store.Auth.VerifyJWT(access_token)
|
claims, err := store.Auth.VerifyJWT(access_token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.AbortWithStatusJSON(401, gin.H{"error": err.Error()})
|
ctx.AbortWithStatusJSON(401, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,45 +36,54 @@ func (store *Store) CheckToken(ctx *gin.Context) {
|
|||||||
|
|
||||||
claims, err := store.Auth.VerifyJWT(access_token)
|
claims, err := store.Auth.VerifyJWT(access_token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.JSON(401, gin.H{"error": err.Error()})
|
ctx.JSON(401, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.JSON(http.StatusOK, gin.H{"data": gin.H{
|
userID, ok := (*claims)["id"].(uint)
|
||||||
"id": (*claims)["id"],
|
if !ok {
|
||||||
"username": (*claims)["username"],
|
ctx.JSON(401, gin.H{"error": "claims does not contain id"})
|
||||||
"admin": (*claims)["admin"],
|
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) {
|
func (store *Store) RefreshToken(ctx *gin.Context) {
|
||||||
refreshToken, err := ctx.Cookie("refresh_token")
|
refreshToken, err := ctx.Cookie("refresh_token")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
|
ctx.JSON(http.StatusUnauthorized, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
claims, err := store.Auth.VerifyJWT(refreshToken)
|
claims, err := store.Auth.VerifyJWT(refreshToken)
|
||||||
if err != nil {
|
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 {
|
if !ok {
|
||||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "invalid token claims"})
|
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "invalid token claims"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user := models.User{}
|
user := models.User{}
|
||||||
tx := store.DB.First(&user, userId)
|
tx := store.DB.First(&user, userID)
|
||||||
if tx.Error != nil {
|
if tx.Error != nil {
|
||||||
ctx.JSON(http.StatusNotFound, gin.H{"error": tx.Error.Error()})
|
ctx.JSON(http.StatusNotFound, tx.Error.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tokens, err := store.Auth.GenerateJWT(&user)
|
tokens, err := store.Auth.GenerateJWT(&user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
ctx.JSON(http.StatusInternalServerError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,31 +104,30 @@ func (store *Store) RefreshToken(ctx *gin.Context) {
|
|||||||
true, true,
|
true, true,
|
||||||
)
|
)
|
||||||
|
|
||||||
ctx.JSON(http.StatusAccepted, gin.H{"data": user})
|
ctx.JSON(http.StatusAccepted, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *Store) Login(ctx *gin.Context) {
|
func (store *Store) Login(ctx *gin.Context) {
|
||||||
var input UserCredentials
|
var input UserCredentials
|
||||||
if err := ctx.ShouldBindBodyWithJSON(&input); err != nil {
|
if err := ctx.ShouldBindBodyWithJSON(&input); err != nil {
|
||||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
ctx.JSON(http.StatusBadRequest, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user := models.User{Username: input.Username}
|
user := models.User{}
|
||||||
if err := store.DB.Where("username = ?", input.Username).First(&user).Error; err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := bcrypt.CompareHashAndPassword(user.Password, []byte(input.Password)); err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate JWT token
|
|
||||||
tokens, err := store.Auth.GenerateJWT(&user)
|
tokens, err := store.Auth.GenerateJWT(&user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
ctx.JSON(http.StatusInternalServerError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,5 +148,5 @@ func (store *Store) Login(ctx *gin.Context) {
|
|||||||
true, true,
|
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()})
|
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.JSON(http.StatusOK, gin.H{"data": posts})
|
ctx.JSON(http.StatusOK, posts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *Store) GetPost(ctx *gin.Context) {
|
func (store *Store) GetPost(ctx *gin.Context) {
|
||||||
@@ -30,7 +30,7 @@ func (store *Store) GetPost(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.JSON(http.StatusOK, gin.H{"data": post})
|
ctx.JSON(http.StatusOK, post)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *Store) CreatePost(ctx *gin.Context) {
|
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}
|
post := models.Post{Title: input.Title, Content: input.Content, AuthorID: userID}
|
||||||
store.DB.Create(&post)
|
store.DB.Create(&post)
|
||||||
|
|
||||||
ctx.JSON(http.StatusCreated, gin.H{"data": post})
|
ctx.JSON(http.StatusCreated, post)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *Store) UpdatePost(ctx *gin.Context) {
|
func (store *Store) UpdatePost(ctx *gin.Context) {
|
||||||
@@ -97,7 +97,7 @@ func (store *Store) UpdatePost(ctx *gin.Context) {
|
|||||||
|
|
||||||
var input CreatePostInput
|
var input CreatePostInput
|
||||||
if err := ctx.ShouldBindBodyWithJSON(&input); err != nil {
|
if err := ctx.ShouldBindBodyWithJSON(&input); err != nil {
|
||||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
ctx.JSON(http.StatusBadRequest, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,14 +105,14 @@ func (store *Store) UpdatePost(ctx *gin.Context) {
|
|||||||
post.Content = input.Content
|
post.Content = input.Content
|
||||||
store.DB.Save(&post)
|
store.DB.Save(&post)
|
||||||
|
|
||||||
ctx.JSON(http.StatusOK, gin.H{"data": post})
|
ctx.JSON(http.StatusOK, post)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *Store) DeletePost(ctx *gin.Context) {
|
func (store *Store) DeletePost(ctx *gin.Context) {
|
||||||
postID := ctx.Param("id")
|
postID := ctx.Param("id")
|
||||||
var post models.Post
|
var post models.Post
|
||||||
if err := store.DB.First(&post, postID).Error; err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,5 +139,5 @@ func (store *Store) DeletePost(ctx *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
store.DB.Delete(&post)
|
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) {
|
func (store *Store) CreateUser(ctx *gin.Context) {
|
||||||
var input UserCredentials
|
var input UserCredentials
|
||||||
if err := ctx.ShouldBindBodyWithJSON(&input); err != nil {
|
if err := ctx.ShouldBindBodyWithJSON(&input); err != nil {
|
||||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
ctx.JSON(http.StatusBadRequest, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(input.Password), bcrypt.DefaultCost)
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(input.Password), bcrypt.DefaultCost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
ctx.JSON(http.StatusInternalServerError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ func (store *Store) CreateUser(ctx *gin.Context) {
|
|||||||
// Generate JWT token
|
// Generate JWT token
|
||||||
tokens, err := store.Auth.GenerateJWT(&user)
|
tokens, err := store.Auth.GenerateJWT(&user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
ctx.JSON(http.StatusInternalServerError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,27 +54,27 @@ func (store *Store) CreateUser(ctx *gin.Context) {
|
|||||||
true, true,
|
true, true,
|
||||||
)
|
)
|
||||||
|
|
||||||
ctx.JSON(http.StatusOK, gin.H{"data": user})
|
ctx.JSON(http.StatusOK, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *Store) GetUser(ctx *gin.Context) {
|
func (store *Store) GetUser(ctx *gin.Context) {
|
||||||
userID := ctx.Param("id")
|
userID := ctx.Param("id")
|
||||||
var user models.User
|
var user models.User
|
||||||
if err := store.DB.First(&user, userID).Error; err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.JSON(http.StatusOK, gin.H{"data": user})
|
ctx.JSON(http.StatusOK, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *Store) GetUsers(ctx *gin.Context) {
|
func (store *Store) GetUsers(ctx *gin.Context) {
|
||||||
var users []models.User
|
var users []models.User
|
||||||
if err := store.DB.Find(&users).Error; err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
ctx.JSON(http.StatusOK, gin.H{"data": users})
|
ctx.JSON(http.StatusOK, users)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *Store) UpdateUser(ctx *gin.Context) {
|
func (store *Store) UpdateUser(ctx *gin.Context) {
|
||||||
@@ -98,7 +98,7 @@ func (store *Store) UpdateUser(ctx *gin.Context) {
|
|||||||
|
|
||||||
var user models.User
|
var user models.User
|
||||||
if err := store.DB.First(&user, userID).Error; err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,11 +126,10 @@ func (store *Store) DeleteUser(ctx *gin.Context) {
|
|||||||
|
|
||||||
var user models.User
|
var user models.User
|
||||||
if err := store.DB.First(&user, userID).Error; err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
store.DB.Delete(&user)
|
store.DB.Delete(&user)
|
||||||
ctx.JSON(http.StatusOK, gin.H{"data": user})
|
ctx.JSON(http.StatusOK, user)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user