add: register and login with DB query AND some struct type correction

This commit is contained in:
mario
2025-05-15 09:46:32 +07:00
parent dd4451c2a8
commit c13f834b92
16 changed files with 465 additions and 25 deletions

View File

@@ -7,6 +7,7 @@ import (
"devone.aplikasi.web.id/gitea/mario/go-ohif-proxy/internal/api/models"
"devone.aplikasi.web.id/gitea/mario/go-ohif-proxy/internal/database"
"golang.org/x/crypto/bcrypt"
)
// DBUser represents a user from the database
@@ -138,3 +139,30 @@ func (r *UserRepository) RevokeRefreshToken(token string) error {
return nil
}
// CreateUser creates a new user
func (r *UserRepository) CreateUser(user *models.User) error {
// Hash the password before storing
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
if err != nil {
return fmt.Errorf("failed to hash password: %w", err)
}
query := `INSERT INTO user (UserEmail, UserPassword, UserRole, UserName, UserCreatedAt, UserUpdatedAt)
VALUES (?, ?, ?, ?, NOW(), NOW())`
result, err := database.DB.Exec(query, user.Email, string(hashedPassword), user.Role, user.Name)
if err != nil {
return fmt.Errorf("database error creating user: %w", err)
}
// Get the last inserted ID
id, err := result.LastInsertId()
if err != nil {
return fmt.Errorf("failed to get last insert ID: %w", err)
}
// Update the user ID
user.ID = fmt.Sprintf("%d", id)
return nil
}