//Register creates the new user and returns a token with created user info
Register creates the new user and returns a token with created user info
// Register creates the new user and returns a token with created user info
func (service *UserService) Register(ctx context.Context, rr *pb.RegistrationRequest) (*pb.AuthenticationResponse, error) {
	err := rr.Validate()
	if err != nil {
		return nil, err
	}
	var user UserDBModel
	result := service.DB.Take(&user, "email = ?", rr.Email)
	if errors.Is(result.Error, gorm.ErrRecordNotFound) {
		user.Email = rr.Email
		hashedPassword, err := server.Hash(rr.Password)
		if err != nil {
			return nil, err
		}
		user.Password = hashedPassword
		fmt.Printf("Creating new user with email %s and password %s\n", rr.Email, hashedPassword)
		result := service.DB.Create(&user)
		if result.Error != nil {
			return nil, result.Error
		}
	}
	authenticationResponse, err := service.Login(ctx, &pb.LoginRequest{
		Email:    rr.Email,
		Password: rr.Password,
	})
	if err != nil {
		return nil, err
	}
	return authenticationResponse, nil
}
func ( service * UserService ) Register ( ctx context . Context , rr * pb . RegistrationRequest ) ( * pb . AuthenticationResponse , error ) { err := rr . Validate ( ) if err != nil { return nil , err } var user UserDBModel result := service . DB . Take ( & user , " " , rr . Email ) if errors . Is ( result . Error , gorm . ErrRecordNotFound ) { user . Email = rr . Email hashedPassword , err := server . Hash ( rr . Password ) if err != nil { return nil , err } user . Password = hashedPassword fmt . Printf ( " \n " , rr . Email , hashedPassword ) result := service . DB . Create ( & user ) if result . Error != nil { return nil , result . Error } } authenticationResponse , err := service . Login ( ctx , & pb . LoginRequest { Email : rr . Email , Password : rr . Password , } ) if err != nil { return nil , err } return authenticationResponse , nil }
Register creates the new user and returns a token with created user info
Register creates the new user and returns a token with created user info