add: register and login with DB query AND some struct type correction
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user