2023-05-21 18:53:59 +08:00
|
|
|
use std::collections::HashMap;
|
2022-07-04 14:53:35 +08:00
|
|
|
use std::convert::TryInto;
|
2023-05-17 09:49:39 +08:00
|
|
|
|
2023-05-21 18:53:59 +08:00
|
|
|
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
|
2023-07-29 09:46:24 +08:00
|
|
|
use flowy_user_deps::entities::*;
|
2023-05-17 09:49:39 +08:00
|
|
|
|
|
|
|
use crate::entities::parser::*;
|
|
|
|
use crate::errors::ErrorCode;
|
2021-11-07 21:45:18 +08:00
|
|
|
|
2021-08-31 17:25:08 +08:00
|
|
|
#[derive(ProtoBuf, Default)]
|
2022-07-19 14:40:56 +08:00
|
|
|
pub struct SignInPayloadPB {
|
2023-02-13 09:29:49 +08:00
|
|
|
#[pb(index = 1)]
|
|
|
|
pub email: String,
|
2021-08-31 17:25:08 +08:00
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
#[pb(index = 2)]
|
|
|
|
pub password: String,
|
2021-11-06 22:35:45 +08:00
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
#[pb(index = 3)]
|
|
|
|
pub name: String,
|
2023-05-21 18:53:59 +08:00
|
|
|
|
|
|
|
#[pb(index = 4)]
|
|
|
|
pub auth_type: AuthTypePB,
|
2023-07-14 13:37:13 +08:00
|
|
|
|
|
|
|
// Only used in local sign in.
|
|
|
|
#[pb(index = 5, one_of)]
|
|
|
|
pub uid: Option<i64>,
|
2021-08-31 17:25:08 +08:00
|
|
|
}
|
|
|
|
|
2022-07-19 14:40:56 +08:00
|
|
|
impl TryInto<SignInParams> for SignInPayloadPB {
|
2023-02-13 09:29:49 +08:00
|
|
|
type Error = ErrorCode;
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<SignInParams, Self::Error> {
|
|
|
|
let email = UserEmail::parse(self.email)?;
|
|
|
|
let password = UserPassword::parse(self.password)?;
|
|
|
|
|
|
|
|
Ok(SignInParams {
|
|
|
|
email: email.0,
|
|
|
|
password: password.0,
|
|
|
|
name: self.name,
|
2023-05-21 18:53:59 +08:00
|
|
|
auth_type: self.auth_type.into(),
|
2023-07-14 13:37:13 +08:00
|
|
|
uid: self.uid,
|
2023-02-13 09:29:49 +08:00
|
|
|
})
|
|
|
|
}
|
2021-08-31 17:25:08 +08:00
|
|
|
}
|
|
|
|
|
2021-07-05 21:23:13 +08:00
|
|
|
#[derive(ProtoBuf, Default)]
|
2022-07-19 14:40:56 +08:00
|
|
|
pub struct SignUpPayloadPB {
|
2023-02-13 09:29:49 +08:00
|
|
|
#[pb(index = 1)]
|
|
|
|
pub email: String,
|
2021-07-05 21:23:13 +08:00
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
#[pb(index = 2)]
|
|
|
|
pub name: String,
|
2021-07-05 21:23:13 +08:00
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
#[pb(index = 3)]
|
|
|
|
pub password: String,
|
2023-05-21 18:53:59 +08:00
|
|
|
|
|
|
|
#[pb(index = 4)]
|
|
|
|
pub auth_type: AuthTypePB,
|
2021-07-05 21:23:13 +08:00
|
|
|
}
|
2022-07-19 14:40:56 +08:00
|
|
|
impl TryInto<SignUpParams> for SignUpPayloadPB {
|
2023-02-13 09:29:49 +08:00
|
|
|
type Error = ErrorCode;
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<SignUpParams, Self::Error> {
|
|
|
|
let email = UserEmail::parse(self.email)?;
|
|
|
|
let password = UserPassword::parse(self.password)?;
|
|
|
|
let name = UserName::parse(self.name)?;
|
|
|
|
|
|
|
|
Ok(SignUpParams {
|
|
|
|
email: email.0,
|
|
|
|
name: name.0,
|
|
|
|
password: password.0,
|
2023-05-21 18:53:59 +08:00
|
|
|
auth_type: self.auth_type.into(),
|
2023-02-13 09:29:49 +08:00
|
|
|
})
|
|
|
|
}
|
2021-07-05 21:23:13 +08:00
|
|
|
}
|
2023-05-17 09:49:39 +08:00
|
|
|
|
2023-05-21 18:53:59 +08:00
|
|
|
#[derive(ProtoBuf, Default)]
|
|
|
|
pub struct ThirdPartyAuthPB {
|
|
|
|
/// Use this field to store the third party auth information.
|
|
|
|
/// Different auth type has different fields.
|
|
|
|
/// Supabase:
|
|
|
|
/// - map: { "uuid": "xxx" }
|
|
|
|
///
|
|
|
|
#[pb(index = 1)]
|
|
|
|
pub map: HashMap<String, String>,
|
|
|
|
|
|
|
|
#[pb(index = 2)]
|
|
|
|
pub auth_type: AuthTypePB,
|
|
|
|
}
|
|
|
|
|
2023-07-14 13:37:13 +08:00
|
|
|
#[derive(ProtoBuf_Enum, Eq, PartialEq, Debug, Clone)]
|
2023-05-21 18:53:59 +08:00
|
|
|
pub enum AuthTypePB {
|
|
|
|
Local = 0,
|
|
|
|
SelfHosted = 1,
|
|
|
|
Supabase = 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for AuthTypePB {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Local
|
|
|
|
}
|
2023-05-17 09:49:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-05 20:57:09 +08:00
|
|
|
#[derive(Debug, ProtoBuf, Default)]
|
|
|
|
pub struct UserCredentialsPB {
|
|
|
|
#[pb(index = 1, one_of)]
|
|
|
|
pub uid: Option<i64>,
|
|
|
|
|
|
|
|
#[pb(index = 2, one_of)]
|
|
|
|
pub uuid: Option<String>,
|
|
|
|
|
|
|
|
#[pb(index = 3, one_of)]
|
|
|
|
pub token: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UserCredentialsPB {
|
|
|
|
pub fn from_uid(uid: i64) -> Self {
|
|
|
|
Self {
|
|
|
|
uid: Some(uid),
|
|
|
|
uuid: None,
|
|
|
|
token: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_token(token: &str) -> Self {
|
|
|
|
Self {
|
|
|
|
uid: None,
|
|
|
|
uuid: None,
|
|
|
|
token: Some(token.to_owned()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_uuid(uuid: &str) -> Self {
|
|
|
|
Self {
|
|
|
|
uid: None,
|
|
|
|
uuid: Some(uuid.to_owned()),
|
|
|
|
token: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<UserCredentialsPB> for UserCredentials {
|
|
|
|
fn from(value: UserCredentialsPB) -> Self {
|
|
|
|
Self::new(value.token, value.uid, value.uuid)
|
|
|
|
}
|
|
|
|
}
|
2023-07-14 13:37:13 +08:00
|
|
|
|
|
|
|
#[derive(Default, ProtoBuf)]
|
|
|
|
pub struct UserStatePB {
|
|
|
|
#[pb(index = 1)]
|
|
|
|
pub auth_type: AuthTypePB,
|
|
|
|
}
|