auth_controller.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package controllers
  2. import (
  3. "ems-backend/models"
  4. "ems-backend/utils"
  5. "net/http"
  6. "github.com/gin-gonic/gin"
  7. )
  8. type LoginRequest struct {
  9. Username string `json:"username" binding:"required"`
  10. Password string `json:"password" binding:"required"`
  11. }
  12. type LoginResponse struct {
  13. Token string `json:"token"`
  14. User models.User `json:"user"`
  15. }
  16. func Login(c *gin.Context) {
  17. var req LoginRequest
  18. if err := c.ShouldBindJSON(&req); err != nil {
  19. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
  20. return
  21. }
  22. // 1. Database User Check
  23. var user models.User
  24. // 使用 MD5 或其他加密方式比较密码(这里为了演示先用明文,实际项目请使用 bcrypt)
  25. if err := models.DB.Where("username = ? AND password = ?", req.Username, req.Password).First(&user).Error; err != nil {
  26. c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid credentials"})
  27. return
  28. }
  29. token, err := utils.GenerateToken(user)
  30. if err != nil {
  31. c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to generate token"})
  32. return
  33. }
  34. // Avoid returning sensitive data
  35. user.Password = ""
  36. c.JSON(http.StatusOK, LoginResponse{
  37. Token: token,
  38. User: user,
  39. })
  40. }
  41. func GetProfile(c *gin.Context) {
  42. userId, exists := c.Get("userId")
  43. if !exists {
  44. c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
  45. return
  46. }
  47. var user models.User
  48. if err := models.DB.Where("id = ?", userId).First(&user).Error; err != nil {
  49. c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
  50. return
  51. }
  52. user.Password = ""
  53. c.JSON(http.StatusOK, gin.H{"user": user})
  54. }
  55. type UpdatePwdRequest struct {
  56. OldPassword string `json:"oldPassword" binding:"required"`
  57. NewPassword string `json:"newPassword" binding:"required"`
  58. }
  59. func UpdateProfilePwd(c *gin.Context) {
  60. userId, exists := c.Get("userId")
  61. if !exists {
  62. c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
  63. return
  64. }
  65. var req UpdatePwdRequest
  66. if err := c.ShouldBindJSON(&req); err != nil {
  67. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  68. return
  69. }
  70. var user models.User
  71. if err := models.DB.Where("id = ?", userId).First(&user).Error; err != nil {
  72. c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
  73. return
  74. }
  75. // Verify old password (plaintext for now)
  76. if user.Password != req.OldPassword {
  77. c.JSON(http.StatusBadRequest, gin.H{"error": "旧密码错误"})
  78. return
  79. }
  80. user.Password = req.NewPassword
  81. if err := models.DB.Save(&user).Error; err != nil {
  82. c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update password"})
  83. return
  84. }
  85. c.JSON(http.StatusOK, gin.H{"message": "Password updated successfully"})
  86. }