pub use crate::api::auth::{auth_step, next_step_request, AuthStep, Session};
use super::*;
use crate::{
api::auth::{next_step_request::form_fields::Field, *},
client_api,
};
#[derive(Debug, Clone)]
pub enum AuthStepResponse {
Choice(String),
Form(Vec<Field>),
Initial,
}
impl AuthStepResponse {
#[inline(always)]
pub fn choice(choice: impl ToString) -> Self {
Self::Choice(choice.to_string())
}
#[inline(always)]
pub fn form(fields: Vec<Field>) -> Self {
Self::Form(fields)
}
#[inline(always)]
pub fn login_choice() -> Self {
Self::choice("login")
}
#[inline(always)]
pub fn register_choice() -> Self {
Self::choice("register")
}
pub fn login_form(email: impl ToString, password: impl ToString) -> Self {
Self::form(vec![
Field::String(email.to_string()),
Field::Bytes(password.to_string().into_bytes()),
])
}
pub fn register_form(
email: impl ToString,
username: impl ToString,
password: impl ToString,
) -> Self {
Self::form(vec![
Field::String(email.to_string()),
Field::String(username.to_string()),
Field::Bytes(password.to_string().into_bytes()),
])
}
}
impl From<AuthStepResponse> for Option<next_step_request::Step> {
fn from(other: AuthStepResponse) -> Option<next_step_request::Step> {
match other {
AuthStepResponse::Choice(choice) => {
Some(next_step_request::Step::Choice(next_step_request::Choice {
choice,
}))
}
AuthStepResponse::Form(fields) => {
Some(next_step_request::Step::Form(next_step_request::Form {
fields: fields
.into_iter()
.map(|f| next_step_request::FormFields { field: Some(f) })
.collect(),
}))
}
AuthStepResponse::Initial => None,
}
}
}
client_api! {
response: BeginAuthResponse,
request: (),
api_fn: begin_auth,
service: auth,
}
#[into_request("NextStepRequest")]
#[derive(Debug, new)]
pub struct AuthResponse {
auth_id: String,
step: AuthStepResponse,
}
client_api! {
response: AuthStep,
request: NextStepRequest,
api_fn: next_step,
service: auth,
}
#[into_request("StepBackRequest", "StreamStepsRequest")]
#[derive(Debug, From, Into, Display, new)]
pub struct AuthId {
auth_id: String,
}
client_api! {
response: AuthStep,
request: StepBackRequest,
api_fn: step_back,
service: auth,
}
client_api! {
response: tonic::Streaming<AuthStep>,
request: StreamStepsRequest,
api_fn: stream_steps,
service: auth,
}