82 lines
2.8 KiB
Go
82 lines
2.8 KiB
Go
package auth
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
type User struct {
|
|
ID string `gorm:"type:uuid;primaryKey" json:"id"`
|
|
Username string `gorm:"type:text;not null" json:"username"`
|
|
Email string `gorm:"type:text;not null;uniqueIndex" json:"email"`
|
|
PasswordHash string `gorm:"column:password_hash;type:text;not null" json:"-"`
|
|
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
|
}
|
|
|
|
type RegisterRequest struct {
|
|
Username string `json:"username" binding:"required,min=3,max=30" example:"john"`
|
|
Email string `json:"email" binding:"required,email" example:"john@example.com"`
|
|
Password string `json:"password" binding:"required,min=8" example:"Secret123!"`
|
|
}
|
|
|
|
type LoginRequest struct {
|
|
Email string `json:"email" binding:"required,email" example:"john@example.com"`
|
|
Password string `json:"password" binding:"required" example:"secret123"`
|
|
}
|
|
|
|
type AuthResponse struct {
|
|
Token string `json:"token" example:"eyJhbGciOiJIUzI1NiIs..."`
|
|
RefreshToken string `json:"refresh_token" example:"dGhpcyBpcyBhIHJlZnJlc2ggdG9rZW4="`
|
|
User UserPublic `json:"user"`
|
|
}
|
|
|
|
type RefreshRequest struct {
|
|
RefreshToken string `json:"refresh_token" binding:"required" example:"dGhpcyBpcyBhIHJlZnJlc2ggdG9rZW4="`
|
|
}
|
|
|
|
type LogoutRequest struct {
|
|
RefreshToken string `json:"refresh_token" binding:"required" example:"dGhpcyBpcyBhIHJlZnJlc2ggdG9rZW4="`
|
|
}
|
|
|
|
type RefreshTokenDoc struct {
|
|
ID string `gorm:"type:uuid;primaryKey" json:"id"`
|
|
UserID string `gorm:"column:user_id;type:uuid;not null;index" json:"user_id"`
|
|
TokenHash string `gorm:"column:token_hash;type:text;not null;uniqueIndex" json:"token_hash"`
|
|
ExpiresAt time.Time `gorm:"column:expires_at;type:timestamptz;not null" json:"expires_at"`
|
|
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
|
}
|
|
|
|
func (RefreshTokenDoc) TableName() string { return "refresh_tokens" }
|
|
|
|
type UserPublic struct {
|
|
ID string `json:"id"`
|
|
Username string `json:"username"`
|
|
Email string `json:"email"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
func NewUserPublic(u *User) UserPublic {
|
|
return UserPublic{
|
|
ID: u.ID,
|
|
Username: u.Username,
|
|
Email: u.Email,
|
|
CreatedAt: u.CreatedAt,
|
|
}
|
|
}
|
|
|
|
type UserResponse struct {
|
|
User UserPublic `json:"user"`
|
|
}
|
|
|
|
type PasswordChangeRequest struct {
|
|
OldPassword string `json:"old_password" binding:"required" example:"Secret123!"`
|
|
NewPassword string `json:"new_password" binding:"required,min=8" example:"NewSecret456!"`
|
|
}
|
|
|
|
type UpdateProfileRequest struct {
|
|
Username string `json:"username" binding:"required,min=3,max=30" example:"john_updated"`
|
|
}
|
|
|
|
type ErrorResponse struct {
|
|
Error string `json:"error" example:"invalid email or password"`
|
|
}
|