user.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package models
  2. import (
  3. "time"
  4. )
  5. // User represents the user account model
  6. type User struct {
  7. ID string `gorm:"type:uuid;primaryKey;default:gen_random_uuid()" json:"id"`
  8. Username string `gorm:"uniqueIndex;not null;size:50" json:"username"`
  9. Email *string `gorm:"uniqueIndex;size:100" json:"email,omitempty"`
  10. PhoneNumber *string `gorm:"uniqueIndex;size:20" json:"phoneNumber,omitempty"`
  11. PasswordHash string `gorm:"column:password_hash;not null" json:"-"`
  12. AvatarURL *string `gorm:"size:500" json:"avatarURL,omitempty"`
  13. RegisteredAt time.Time `gorm:"autoCreateTime" json:"registeredAt"`
  14. UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updatedAt"`
  15. }
  16. // AuthSession stores a revocable, hashed refresh token. Access tokens are
  17. // short-lived JWTs and reference this row through their sid claim.
  18. type AuthSession struct {
  19. ID string `gorm:"type:uuid;primaryKey;default:gen_random_uuid()" json:"id"`
  20. UserID string `gorm:"type:uuid;not null;index" json:"userId"`
  21. RefreshHash string `gorm:"uniqueIndex;not null;size:64" json:"-"`
  22. ExpiresAt time.Time `gorm:"not null;index" json:"expiresAt"`
  23. RevokedAt *time.Time `gorm:"index" json:"-"`
  24. LastUsedAt *time.Time `json:"lastUsedAt,omitempty"`
  25. CreatedAt time.Time `gorm:"autoCreateTime" json:"createdAt"`
  26. }
  27. // RegisterRequest DTO
  28. type RegisterRequest struct {
  29. Username string `json:"username" binding:"required,min=2"`
  30. Identifier string `json:"identifier" binding:"required"` // email or phone
  31. Password string `json:"password" binding:"required,min=6"`
  32. }
  33. // LoginRequest DTO
  34. type LoginRequest struct {
  35. Identifier string `json:"identifier" binding:"required"`
  36. Password string `json:"password" binding:"required"`
  37. }
  38. // UpdateProfileRequest DTO
  39. type UpdateProfileRequest struct {
  40. Username *string `json:"username,omitempty"`
  41. Email *string `json:"email,omitempty"`
  42. PhoneNumber *string `json:"phoneNumber,omitempty"`
  43. AvatarURL *string `json:"avatarURL,omitempty"`
  44. }
  45. // ChangePasswordRequest DTO
  46. type ChangePasswordRequest struct {
  47. OldPassword string `json:"oldPassword" binding:"required"`
  48. NewPassword string `json:"newPassword" binding:"required,min=6"`
  49. }
  50. // AuthResponse DTO
  51. type AuthResponse struct {
  52. User User `json:"user"`
  53. Token string `json:"token"`
  54. RefreshToken string `json:"refreshToken"`
  55. ExpiresAt time.Time `json:"expiresAt"`
  56. }
  57. type RefreshRequest struct {
  58. RefreshToken string `json:"refreshToken" binding:"required"`
  59. }
  60. // ErrorResponse DTO
  61. type ErrorResponse struct {
  62. Code int `json:"code"`
  63. Message string `json:"message"`
  64. }