50 lines
1.5 KiB
Rust
50 lines
1.5 KiB
Rust
// src/services/auth.rs
|
|
use tonic::transport::Channel;
|
|
use common::proto::KompAC::auth::{
|
|
auth_service_client::AuthServiceClient,
|
|
LoginRequest, LoginResponse,
|
|
RegisterRequest, AuthResponse,
|
|
};
|
|
use anyhow::{Context, Result};
|
|
|
|
#[derive(Clone)]
|
|
pub struct AuthClient {
|
|
client: AuthServiceClient<Channel>,
|
|
}
|
|
|
|
impl AuthClient {
|
|
pub async fn new() -> Result<Self> {
|
|
let client = AuthServiceClient::connect("http://[::1]:50051")
|
|
.await
|
|
.context("Failed to connect to auth service")?;
|
|
Ok(Self { client })
|
|
}
|
|
|
|
/// Login user via gRPC.
|
|
pub async fn login(&mut self, identifier: String, password: String) -> Result<LoginResponse> {
|
|
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>,
|
|
password_confirmation: Option<String>,
|
|
role: Option<String>,
|
|
) -> Result<AuthResponse> {
|
|
let request = tonic::Request::new(RegisterRequest {
|
|
username,
|
|
email,
|
|
password: password.unwrap_or_default(),
|
|
password_confirmation: password_confirmation.unwrap_or_default(),
|
|
role: role.unwrap_or_default(),
|
|
});
|
|
let response = self.client.register(request).await?.into_inner();
|
|
Ok(response)
|
|
}
|
|
}
|