use crate::serde::duration_from_secs;
use serde::{Deserialize, Serialize};
use std::time::Duration;
use uuid::Uuid;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TokenType {
Bearer,
}
impl std::fmt::Display for TokenType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Bearer => write!(f, "Bearer"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseTokenTypeError(String);
impl std::fmt::Display for ParseTokenTypeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Invalid token type: {}", self.0)
}
}
impl std::error::Error for ParseTokenTypeError {}
impl std::str::FromStr for TokenType {
type Err = ParseTokenTypeError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"Bearer" => Ok(Self::Bearer),
_ => Err(ParseTokenTypeError(s.to_string())),
}
}
}
impl TryFrom<&str> for TokenType {
type Error = ParseTokenTypeError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
value.parse()
}
}
impl TryFrom<String> for TokenType {
type Error = ParseTokenTypeError;
fn try_from(value: String) -> Result<Self, Self::Error> {
value.parse()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "strict", serde(deny_unknown_fields))]
pub struct AuthTokenResponse {
pub access_token: String,
pub refresh_token: String,
pub token_type: TokenType,
#[serde(with = "duration_from_secs")]
pub expires_in: Duration,
#[serde(with = "duration_from_secs")]
pub created_at: Duration,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "strict", serde(deny_unknown_fields))]
pub struct User {
pub id: Uuid,
pub email: String,
pub first_name: String,
pub last_name: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "strict", serde(deny_unknown_fields))]
pub struct AuthSignupResponse {
pub access_token: String,
pub refresh_token: String,
pub token_type: TokenType,
#[serde(with = "duration_from_secs")]
pub expires_in: Duration,
#[serde(with = "duration_from_secs")]
pub created_at: Duration,
pub user: User,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "strict", serde(deny_unknown_fields))]
pub struct AuthLoginResponse {
pub access_token: String,
pub refresh_token: String,
pub token_type: TokenType,
#[serde(with = "duration_from_secs")]
pub expires_in: Duration,
#[serde(with = "duration_from_secs")]
pub created_at: Duration,
pub user: User,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "strict", serde(deny_unknown_fields))]
pub struct DeviceInfo {
pub device_id: String,
pub device_name: String,
pub device_type: String,
pub os_version: String,
pub app_version: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "strict", serde(deny_unknown_fields))]
pub(crate) struct SignupRequest {
pub user: SignupUserData,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub invite_code: Option<String>,
pub device: DeviceInfo,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "strict", serde(deny_unknown_fields))]
pub struct SignupUserData {
pub email: String,
pub password: String,
pub first_name: String,
pub last_name: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "strict", serde(deny_unknown_fields))]
pub(crate) struct LoginRequest {
pub email: String,
pub password: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub otp_code: Option<String>,
pub device: DeviceInfo,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "strict", serde(deny_unknown_fields))]
pub(crate) struct RefreshTokenRequest {
pub refresh_token: String,
pub device: RefreshDeviceInfo,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "strict", serde(deny_unknown_fields))]
pub struct RefreshDeviceInfo {
pub device_id: String,
}