Skip to content

Commit 9ff5126

Browse files
authored
Update user_controller.go
1 parent 076430b commit 9ff5126

File tree

1 file changed

+1
-10
lines changed

1 file changed

+1
-10
lines changed

user-service/controllers/user_controller.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// controllers/user_controller.go
21
package controllers
32

43
import (
@@ -11,31 +10,27 @@ import (
1110
)
1211

1312
func InitializeRoutes(router *gin.Engine, db *gorm.DB) {
14-
// Register route with raw SQL queries
1513
router.POST("/register", func(c *gin.Context) {
1614
var user models.User
1715
if err := c.ShouldBindJSON(&user); err != nil {
1816
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
1917
return
2018
}
2119

22-
// Check if the username already exists
2320
var count int64
2421
db.Raw("SELECT COUNT(*) FROM users WHERE username = ?", user.Username).Scan(&count)
2522
if count > 0 {
2623
c.JSON(http.StatusBadRequest, gin.H{"error": "Username already exists"})
2724
return
2825
}
2926

30-
// Hash the password
3127
hashedPassword, err := utils.HashPassword(user.Password)
3228
if err != nil {
3329
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to hash password"})
3430
return
3531
}
3632
user.Password = hashedPassword
3733

38-
// Insert the user using raw SQL query
3934
result := db.Exec("INSERT INTO users (username, password, role) VALUES (?, ?, ?)", user.Username, user.Password, user.Role)
4035
if result.Error != nil {
4136
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to register user", "details": result.Error.Error()})
@@ -45,7 +40,6 @@ func InitializeRoutes(router *gin.Engine, db *gorm.DB) {
4540
c.JSON(http.StatusOK, gin.H{"message": "User registered successfully", "user": user})
4641
})
4742

48-
// Login route with raw SQL queries
4943
router.POST("/login", func(c *gin.Context) {
5044
var req struct {
5145
Username string `json:"username" binding:"required"`
@@ -64,13 +58,11 @@ func InitializeRoutes(router *gin.Engine, db *gorm.DB) {
6458
return
6559
}
6660

67-
// Compare the hashed password
6861
if err := utils.CheckPasswordHash(req.Password, user.Password); err != nil {
6962
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid credentials"})
7063
return
7164
}
7265

73-
// Generate a JWT token
7466
token, err := utils.GenerateToken(user.Username, user.Role)
7567
if err != nil {
7668
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not generate token"})
@@ -81,10 +73,9 @@ func InitializeRoutes(router *gin.Engine, db *gorm.DB) {
8173
})
8274

8375
router.GET("/profile", utils.AuthMiddleware(), func(c *gin.Context) {
84-
username := c.MustGet("username").(string) // Retrieve the username from the token
76+
username := c.MustGet("username").(string)
8577
var user models.User
8678

87-
// Fetch the user details from the database using the username
8879
if err := db.Where("username = ?", username).First(&user).Error; err != nil {
8980
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
9081
return

0 commit comments

Comments
 (0)