Skip to content

Commit

Permalink
Update routes and token handling in user controller; add refresh toke…
Browse files Browse the repository at this point in the history
…n functionality and modify request structure
  • Loading branch information
L4B0MB4 committed Nov 28, 2024
1 parent 63ce137 commit 7bf59af
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require github.com/mattn/go-sqlite3 v1.14.24

require (
github.com/L4B0MB4/EVTSRC v0.4.5 // indirect
github.com/PRYVT/utils v0.1.0 // indirect
github.com/PRYVT/utils v0.1.1 // indirect
github.com/bytedance/sonic v1.12.2 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/L4B0MB4/EVTSRC v0.4.5 h1:HA4tp4fa/oCPTCl3gTD2FkRjo+nFKWm4rLmpudxcxXg=
github.com/L4B0MB4/EVTSRC v0.4.5/go.mod h1:hpyNdNWqikZ6dcm8dhZAXgnAXZQNGAfXgRw902zjby0=
github.com/PRYVT/utils v0.1.0 h1:Oq/sKHTKBVoAX+pLJMniYaZfv0ReDlQ+5E8qpKGiVjI=
github.com/PRYVT/utils v0.1.0/go.mod h1:RDgZFSTf1GPnpjVqnw0PSY68bzyp+WTK/wgAklOBFxY=
github.com/PRYVT/utils v0.1.1 h1:Y/WHkTHID0T40O2XfzlLM1QsDyQWE8FtH8ncaU4ags8=
github.com/PRYVT/utils v0.1.1/go.mod h1:b7zk2FAGwJ8BPJx2JQ8qd+bA59g5EY7Y1vZQPWZHK3s=
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic v1.12.2 h1:oaMFuRTpMHYLpCntGca65YWt5ny+wAceDERTkT2L9lg=
Expand Down
4 changes: 2 additions & 2 deletions pkg/command/httphandler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func NewHttpHandler(c *controller.UserController) *HttpHandler {
}

func (h *HttpHandler) RegisterRoutes() {
h.router.POST("/:userId/changeName", h.userController.ChangeDisplayName)
h.router.POST("/00000000-0000-0000-0000-000000000000/create", h.userController.CreateUser)
h.router.POST("users/:userId/changeName", h.userController.ChangeDisplayName)
h.router.POST("users/00000000-0000-0000-0000-000000000000/create", h.userController.CreateUser)
}

func (h *HttpHandler) Start() error {
Expand Down
1 change: 1 addition & 0 deletions pkg/models/query/token_request.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package query

type TokenRequest struct {
UserName string `json:"username"`
Password string `json:"password"`
}
27 changes: 21 additions & 6 deletions pkg/query/httphandler/controller/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@ func NewUserController(userRepo *repository.UserRepository, tokenManager *auth.T
}

func (ctrl *UserController) GetToken(c *gin.Context) {
userUuid, err := utils.GetUserIdParam(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

tokenReq := &models.TokenRequest{}
err = c.BindJSON(tokenReq)
err := c.BindJSON(tokenReq)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
userUuid := hash.GenerateGUID(tokenReq.UserName)

user, err := ctrl.userRepo.GetUserById(userUuid)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "User not found"})
Expand All @@ -50,6 +48,23 @@ func (ctrl *UserController) GetToken(c *gin.Context) {
return
}
c.JSON(http.StatusOK, models.TokenResponse{Token: token})
}
func (ctrl *UserController) RefreshToken(c *gin.Context) {

tokenStr := auth.GetTokenFromHeader(c)
userUuid, err := ctrl.tokenManager.GetUserUuidFromToken(tokenStr)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
token, err := ctrl.tokenManager.CreateToken(userUuid)

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, models.TokenResponse{Token: token})

}

Expand Down
7 changes: 4 additions & 3 deletions pkg/query/httphandler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ func NewHttpHandler(c *controller.UserController, am *auth.AuthMiddleware) *Http
}

func (h *HttpHandler) RegisterRoutes() {
h.router.POST("/:userId/token", h.userController.GetToken)
h.router.POST("/authentication/token", h.userController.GetToken)
h.router.Use(h.authMiddleware.AuthenticateMiddleware)
{
h.router.GET("/:userId", h.userController.GetUser)
h.router.GET("/", h.userController.GetUsers)
h.router.POST("/authentication/refresh", h.userController.RefreshToken)
h.router.GET("users/:userId", h.userController.GetUser)
h.router.GET("users/", h.userController.GetUsers)
}
}

Expand Down

0 comments on commit 7bf59af

Please sign in to comment.