44 lines
1.5 KiB
Rust
44 lines
1.5 KiB
Rust
// src/services/auth.rs
|
|
use tonic::transport::Channel;
|
|
use common::proto::multieko2::auth::{
|
|
auth_service_client::AuthServiceClient,
|
|
LoginRequest, LoginResponse,
|
|
RegisterRequest, AuthResponse,
|
|
};
|
|
|
|
pub struct AuthClient {
|
|
client: AuthServiceClient<Channel>,
|
|
}
|
|
|
|
impl AuthClient {
|
|
pub async fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
|
let client = AuthServiceClient::connect("http://[::1]:50051").await?;
|
|
Ok(Self { client })
|
|
}
|
|
|
|
/// Login user via gRPC.
|
|
pub async fn login(&mut self, identifier: String, password: String) -> Result<LoginResponse, Box<dyn std::error::Error>> {
|
|
let request = tonic::Request::new(LoginRequest { identifier, password });
|
|
let response = self.client.login(request).await?.into_inner();
|
|
Ok(response)
|
|
}
|
|
|
|
/// Registers a new user via gRPC.
|
|
pub async fn register(
|
|
&mut self,
|
|
username: String,
|
|
email: String,
|
|
password: Option<String>, // Use Option for optional fields
|
|
password_confirmation: Option<String>, // Use Option for optional fields
|
|
) -> Result<AuthResponse, Box<dyn std::error::Error>> {
|
|
let request = tonic::Request::new(RegisterRequest {
|
|
username,
|
|
email,
|
|
password: password.unwrap_or_default(), // Send empty string if None
|
|
password_confirmation: password_confirmation.unwrap_or_default(), // Send empty string if None
|
|
});
|
|
let response = self.client.register(request).await?.into_inner();
|
|
Ok(response)
|
|
}
|
|
}
|