Files
Datarush/internal/gw/grpc_client/auth_client.go
T

70 lines
1.4 KiB
Go

package grpc_client
import (
"context"
"fmt"
pb "datarush/pkg/api/auth"
"google.golang.org/grpc"
)
type AuthClient struct {
client pb.AuthServiceClient
conn *grpc.ClientConn
}
func NewAuthClient(ctx context.Context, address string, factory *ClientFactory) (*AuthClient, error) {
conn, err := factory.GetConnectionWithRetry(ctx, address)
if err != nil {
return nil, fmt.Errorf("failed to create auth client: %w", err)
}
return &AuthClient{
client: pb.NewAuthServiceClient(conn),
conn: conn,
}, nil
}
func (c *AuthClient) SignUp(ctx context.Context, email, username, password string) (string, error) {
req := &pb.SignUpRequest{
Email: email,
Username: username,
Password: password,
}
resp, err := c.client.SignUp(ctx, req)
if err != nil {
return "", fmt.Errorf("sign up failed: %w", err)
}
return resp.Token, nil
}
func (c *AuthClient) SignIn(ctx context.Context, email, password string) (string, error) {
req := &pb.SignInRequest{
Email: email,
Password: password,
}
resp, err := c.client.SignIn(ctx, req)
if err != nil {
return "", fmt.Errorf("sign in failed: %w", err)
}
return resp.Token, nil
}
func (c *AuthClient) ValidateToken(ctx context.Context, token string) (string, error) {
req := &pb.ValidateTokenRequest{
Token: token,
}
resp, err := c.client.ValidateToken(ctx, req)
if err != nil {
return "", fmt.Errorf("token validation failed: %w", err)
}
return resp.UserId, nil
}