Skip to main content

Overview

The Users Service handles user registration, authentication, and profile management. Service Details:
  • Package: users
  • Port: 50051
  • Proto: proto/users/users.proto

Service Definition

service UsersService {
    rpc RegisterUser(RegisterUserRequest) returns (RegisterUserResponse) {}
    rpc LoginUser(LoginUserRequest) returns (LoginUserResponse) {}
    rpc GetUser(GetUserRequest) returns (GetUserResponse) {}
    rpc UpdateUser(UpdateUserRequest) returns (UpdateUserResponse) {}
}

RPC Methods

RegisterUser

Register a new user account. Request:
email
string
required
User’s email address
password
string
required
User’s password (will be hashed)
Response:
user_id
string
Unique identifier of the newly registered user
Example:
req := &users.RegisterUserRequest{
    Email:    "[email protected]",
    Password: "securepassword",
}

resp, err := client.RegisterUser(ctx, req)
if err != nil {
    log.Fatal(err)
}

fmt.Printf("User registered with ID: %s\n", resp.UserId)
# Using grpcurl
grpcurl -plaintext -d '{
  "email": "[email protected]",
  "password": "securepassword"
}' localhost:50051 users.UsersService/RegisterUser

LoginUser

Authenticate a user with email and password. Request:
email
string
required
User’s email address
password
string
required
User’s password
Response:
user_id
string
Unique identifier of the authenticated user
Example:
req := &users.LoginUserRequest{
    Email:    "[email protected]",
    Password: "securepassword",
}

resp, err := client.LoginUser(ctx, req)
if err != nil {
    log.Fatal(err)
}

fmt.Printf("User logged in with ID: %s\n", resp.UserId)
# Using grpcurl
grpcurl -plaintext -d '{
  "email": "[email protected]",
  "password": "securepassword"
}' localhost:50051 users.UsersService/LoginUser

GetUser

Retrieve user details by user ID. Request:
id
string
required
Unique identifier of the user to retrieve
Response:
email
string
User’s email address
notification_preference
string
User’s notification preference setting
created_at
string
Timestamp of when the user account was created
updated_at
string
Timestamp of when the user account was last updated
Example:
req := &users.GetUserRequest{
    Id: "user-123",
}

resp, err := client.GetUser(ctx, req)
if err != nil {
    log.Fatal(err)
}

fmt.Printf("Email: %s\n", resp.Email)
fmt.Printf("Notification Preference: %s\n", resp.NotificationPreference)
# Using grpcurl
grpcurl -plaintext -d '{
  "id": "user-123"
}' localhost:50051 users.UsersService/GetUser

UpdateUser

Update user profile information. Request:
id
string
required
Unique identifier of the user to update
notification_preference
string
required
User’s new notification preference (e.g., “email”, “push”, “none”)
Response: Empty response on success. Example:
req := &users.UpdateUserRequest{
    Id:                    "user-123",
    NotificationPreference: "email",
}

_, err := client.UpdateUser(ctx, req)
if err != nil {
    log.Fatal(err)
}

fmt.Println("User updated successfully")
# Using grpcurl
grpcurl -plaintext -d '{
  "id": "user-123",
  "notification_preference": "email"
}' localhost:50051 users.UsersService/UpdateUser

Connection Example

package main

import (
    "context"
    "log"
    "time"

    "google.golang.org/grpc"
    "google.golang.org/grpc/credentials/insecure"
    pb "github.com/hitesh22rana/chronoverse/pkg/proto/go/users"
)

func main() {
    // Connect to Users service
    conn, err := grpc.Dial(
        "localhost:50051",
        grpc.WithTransportCredentials(insecure.NewCredentials()),
    )
    if err != nil {
        log.Fatalf("Failed to connect: %v", err)
    }
    defer conn.Close()

    client := pb.NewUsersServiceClient(conn)
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()

    // Register a new user
    resp, err := client.RegisterUser(ctx, &pb.RegisterUserRequest{
        Email:    "[email protected]",
        Password: "securepassword",
    })
    if err != nil {
        log.Fatalf("RegisterUser failed: %v", err)
    }

    log.Printf("User registered: %s", resp.UserId)
}

Build docs developers (and LLMs) love