Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
BEGIN;

ALTER TABLE user_authentications DROP CONSTRAINT uq_user_authentications_provider_social_id;

ALTER TABLE user_authentications DROP CONSTRAINT uq_user_authentications_user_id_provider;

COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
BEGIN;

ALTER TABLE user_authentications ADD CONSTRAINT uq_user_authentications_provider_social_id UNIQUE (provider, social_id);

ALTER TABLE user_authentications ADD CONSTRAINT uq_user_authentications_user_id_provider UNIQUE (user_id, provider);

COMMIT;
2 changes: 2 additions & 0 deletions entity/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ var ErrUserNotFound = errors.New("user not found")
var ErrUserAlreadyExists = errors.New("user already exists")

var ErrAuthenticationAlreadyExists = errors.New("authentication already exists")

var ErrAuthenticationNotFound = errors.New("authentication not found")
4 changes: 4 additions & 0 deletions port/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ type IRepository interface {
// If the user does not exist, return ErrUserNotFound.
// If the authentication is already registered, return ErrAuthenticationAlreadyExists.
AddAuthentication(ctx context.Context, id string, a *entity.Authentication) error

// DeleteAuthentication deletes the authentication specified by the given userID and provider.
// If the corresponding authentication does not exist, return ErrAuthenticationNotFound.
DeleteAuthentication(ctx context.Context, userID string, provider entity.Provider) error
}
24 changes: 24 additions & 0 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,30 @@ func (r *Repository) AddAuthentication(ctx context.Context, id string, a *entity
return nil
}

func (r *Repository) DeleteAuthentication(ctx context.Context, userID string, provider entity.Provider) error {
failed := func(err error) error {
return fmt.Errorf("Repository.DeleteAuthentication %q %q -> %w", userID, provider, err)
}

var aid string
query := "SELECT id FROM user_authentications WHERE user_id = $1 AND provider = $2"
err := r.db.QueryRowContext(ctx, query, userID, provider).Scan(&aid)
if err != nil {
if err == sql.ErrNoRows {
return failed(entity.ErrAuthenticationNotFound)
}
return failed(err)
}

query = "DELETE FROM user_authentications WHERE id = $1"
_, err = r.db.ExecContext(ctx, query, aid)
if err != nil {
return failed(err)
}

return nil
}

// getUserIDByAuthentication returns the id of the user authorized by the given authentication.
// If the corresponding user does not exist, return ErrUserNotFound.
func (r *Repository) getUserIDByAuthentication(ctx context.Context, a *entity.Authentication) (id string, err error) {
Expand Down
2 changes: 2 additions & 0 deletions server/converter/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func ToGRPCError(err error, log func(err error)) error {
return status.Error(codes.NotFound, "指定されたユーザーが見つかりませんでした")
case errors.Is(err, entity.ErrAuthenticationAlreadyExists):
return status.Error(codes.AlreadyExists, "認証情報が既に登録されています")
case errors.Is(err, entity.ErrAuthenticationNotFound):
return status.Error(codes.NotFound, "指定された認証情報が見つかりませんでした")
default:
log(err)
return status.Error(codes.Internal, "サーバー内で問題が発生しました")
Expand Down
241 changes: 188 additions & 53 deletions server/pb/UserService.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions server/pb/UserService.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ service UserService {
rpc AddAuthentication(AddAuthenticationRequest) returns (AddAuthenticationResponse);
rpc GetUser(GetUserRequest) returns (GetUserResponse);
rpc DeleteUser(DeleteUserRequest) returns (DeleteUserResponse);

// 紐付けアカウントが1つだけの場合はエラーになる
// 紐付けアカウントが2つ以上の場合は指定された紐付けアカウントの紐付けを解除する(hard delete)
// 指定されたプロバイダがユーザーに紐づいていない場合はエラーになる
rpc DeleteAuthentication(DeleteAuthenticationRequest) returns (DeleteAuthenticationResponse);
}

enum Provider {
Expand All @@ -29,7 +34,6 @@ message GetOrCreateUserResponse {
}

message AddAuthenticationRequest {

string id = 1;
Provider provider = 2;
string socialId = 3;
Expand All @@ -50,4 +54,11 @@ message DeleteUserRequest {
string id = 1;
}

message DeleteUserResponse {}
message DeleteUserResponse {}

message DeleteAuthenticationRequest {
string id = 1;
Provider provider = 2;
}

message DeleteAuthenticationResponse {}
Loading