Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2PC or Saga for registration route #6

Open
cl1ckname opened this issue Dec 3, 2023 · 0 comments
Open

2PC or Saga for registration route #6

cl1ckname opened this issue Dec 3, 2023 · 0 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@cl1ckname
Copy link
Owner

There is a danger moment about user registration

func (s *service) Register(ctx context.Context, req *pb.RegisterRequest) (*pb.UserResponse, error) {
	u, err := findByEmail(s.db, req.Email)
	if err != nil {
		if !errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, err
		}
	}
	u.Email = req.Email
	u.PasswordHash = hashString(req.Password, s.salt)
	savedU, err := saveUser(s.db, u)
	if err != nil {
		return nil, err
	}
	if _, err := s.profile.Create(ctx, &profile.CreateRequest{
		UserId:   savedU.UserId.String(),
		Username: req.Username,
	}); err != nil {
		return nil, err
	}
	return &pb.UserResponse{
		UserId:   savedU.UserId.String(),
		Username: req.Username,
	}, nil
}

If registered successes but profile creating failed user won't be able to register again and also won't be able to visit his profile. I suppose there should be an algorithm for distributed transactions (actually idk). Advice me smthng?

@cl1ckname cl1ckname added enhancement New feature or request help wanted Extra attention is needed labels Dec 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant