Files
honeyDueAPI/internal/dto/responses/auth.go
Trey t 780e699463 Add Google OAuth authentication support
- Add Google OAuth token verification and user lookup/creation
- Add GoogleAuthRequest and GoogleAuthResponse DTOs
- Add GoogleLogin handler in auth_handler.go
- Add google_auth.go service for token verification
- Add FindByGoogleID repository method for user lookup
- Add GoogleID field to User model
- Add Google OAuth configuration (client ID, enabled flag)
- Add i18n translations for Google auth error messages
- Add Google verification email template support

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-13 00:51:44 -06:00

190 lines
5.6 KiB
Go

package responses
import (
"time"
"github.com/treytartt/casera-api/internal/models"
)
// UserResponse represents a user in API responses
type UserResponse struct {
ID uint `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
IsActive bool `json:"is_active"`
Verified bool `json:"verified"`
DateJoined time.Time `json:"date_joined"`
LastLogin *time.Time `json:"last_login,omitempty"`
}
// UserProfileResponse represents a user profile in API responses
type UserProfileResponse struct {
ID uint `json:"id"`
UserID uint `json:"user_id"`
Verified bool `json:"verified"`
Bio string `json:"bio"`
PhoneNumber string `json:"phone_number"`
DateOfBirth *time.Time `json:"date_of_birth,omitempty"`
ProfilePicture string `json:"profile_picture"`
}
// LoginResponse represents the login response
type LoginResponse struct {
Token string `json:"token"`
User UserResponse `json:"user"`
}
// RegisterResponse represents the registration response
type RegisterResponse struct {
Token string `json:"token"`
User UserResponse `json:"user"`
Message string `json:"message"`
}
// CurrentUserResponse represents the /auth/me/ response
type CurrentUserResponse struct {
ID uint `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
IsActive bool `json:"is_active"`
DateJoined time.Time `json:"date_joined"`
LastLogin *time.Time `json:"last_login,omitempty"`
Profile *UserProfileResponse `json:"profile,omitempty"`
}
// VerifyEmailResponse represents the email verification response
type VerifyEmailResponse struct {
Message string `json:"message"`
Verified bool `json:"verified"`
}
// ForgotPasswordResponse represents the forgot password response
type ForgotPasswordResponse struct {
Message string `json:"message"`
}
// VerifyResetCodeResponse represents the verify reset code response
type VerifyResetCodeResponse struct {
Message string `json:"message"`
ResetToken string `json:"reset_token"`
}
// ResetPasswordResponse represents the reset password response
type ResetPasswordResponse struct {
Message string `json:"message"`
}
// MessageResponse represents a simple message response
type MessageResponse struct {
Message string `json:"message"`
}
// ErrorResponse represents an error response
type ErrorResponse struct {
Error string `json:"error"`
Details map[string]string `json:"details,omitempty"`
}
// NewUserResponse creates a UserResponse from a User model
func NewUserResponse(user *models.User) UserResponse {
verified := false
if user.Profile != nil {
verified = user.Profile.Verified
}
return UserResponse{
ID: user.ID,
Username: user.Username,
Email: user.Email,
FirstName: user.FirstName,
LastName: user.LastName,
IsActive: user.IsActive,
Verified: verified,
DateJoined: user.DateJoined,
LastLogin: user.LastLogin,
}
}
// NewUserProfileResponse creates a UserProfileResponse from a UserProfile model
func NewUserProfileResponse(profile *models.UserProfile) *UserProfileResponse {
if profile == nil {
return nil
}
return &UserProfileResponse{
ID: profile.ID,
UserID: profile.UserID,
Verified: profile.Verified,
Bio: profile.Bio,
PhoneNumber: profile.PhoneNumber,
DateOfBirth: profile.DateOfBirth,
ProfilePicture: profile.ProfilePicture,
}
}
// NewCurrentUserResponse creates a CurrentUserResponse from a User model
func NewCurrentUserResponse(user *models.User) CurrentUserResponse {
return CurrentUserResponse{
ID: user.ID,
Username: user.Username,
Email: user.Email,
FirstName: user.FirstName,
LastName: user.LastName,
IsActive: user.IsActive,
DateJoined: user.DateJoined,
LastLogin: user.LastLogin,
Profile: NewUserProfileResponse(user.Profile),
}
}
// NewLoginResponse creates a LoginResponse
func NewLoginResponse(token string, user *models.User) LoginResponse {
return LoginResponse{
Token: token,
User: NewUserResponse(user),
}
}
// NewRegisterResponse creates a RegisterResponse
func NewRegisterResponse(token string, user *models.User) RegisterResponse {
return RegisterResponse{
Token: token,
User: NewUserResponse(user),
Message: "Registration successful. Please check your email to verify your account.",
}
}
// AppleSignInResponse represents the Apple Sign In response
type AppleSignInResponse struct {
Token string `json:"token"`
User UserResponse `json:"user"`
IsNewUser bool `json:"is_new_user"`
}
// NewAppleSignInResponse creates an AppleSignInResponse
func NewAppleSignInResponse(token string, user *models.User, isNewUser bool) AppleSignInResponse {
return AppleSignInResponse{
Token: token,
User: NewUserResponse(user),
IsNewUser: isNewUser,
}
}
// GoogleSignInResponse represents the Google Sign In response
type GoogleSignInResponse struct {
Token string `json:"token"`
User UserResponse `json:"user"`
IsNewUser bool `json:"is_new_user"`
}
// NewGoogleSignInResponse creates a GoogleSignInResponse
func NewGoogleSignInResponse(token string, user *models.User, isNewUser bool) GoogleSignInResponse {
return GoogleSignInResponse{
Token: token,
User: NewUserResponse(user),
IsNewUser: isNewUser,
}
}