2021-07-11 13:54:55 +00:00
|
|
|
use crate::{entities::*, errors::UserError, services::user_session::UserSession};
|
2021-07-08 13:23:44 +00:00
|
|
|
use flowy_dispatch::prelude::*;
|
2021-07-09 15:31:44 +00:00
|
|
|
use std::{convert::TryInto, sync::Arc};
|
2021-06-29 15:21:25 +00:00
|
|
|
|
2021-06-30 07:33:49 +00:00
|
|
|
// tracing instrument 👉🏻 https://docs.rs/tracing/0.1.26/tracing/attr.instrument.html
|
|
|
|
#[tracing::instrument(
|
2021-07-05 13:23:13 +00:00
|
|
|
name = "user_sign_in",
|
2021-07-11 07:33:19 +00:00
|
|
|
skip(data, session),
|
2021-06-30 07:33:49 +00:00
|
|
|
fields(
|
|
|
|
email = %data.email,
|
|
|
|
)
|
|
|
|
)]
|
2021-07-11 07:33:19 +00:00
|
|
|
pub async fn user_sign_in(
|
|
|
|
data: Data<SignInRequest>,
|
|
|
|
session: ModuleData<Arc<UserSession>>,
|
2021-07-11 13:54:55 +00:00
|
|
|
) -> ResponseResult<UserDetail, UserError> {
|
2021-07-11 07:33:19 +00:00
|
|
|
let params: SignInParams = data.into_inner().try_into()?;
|
|
|
|
let user = session.sign_in(params).await?;
|
|
|
|
let user_detail = UserDetail::from(user);
|
|
|
|
response_ok(user_detail)
|
2021-07-05 13:23:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(
|
|
|
|
name = "user_sign_up",
|
2021-07-09 15:31:44 +00:00
|
|
|
skip(data, session),
|
2021-07-05 13:23:13 +00:00
|
|
|
fields(
|
2021-07-10 08:27:20 +00:00
|
|
|
email = %data.email,
|
|
|
|
name = %data.name,
|
2021-07-05 13:23:13 +00:00
|
|
|
)
|
|
|
|
)]
|
2021-07-09 15:31:44 +00:00
|
|
|
pub async fn user_sign_up(
|
|
|
|
data: Data<SignUpRequest>,
|
|
|
|
session: ModuleData<Arc<UserSession>>,
|
2021-07-11 13:54:55 +00:00
|
|
|
) -> ResponseResult<UserDetail, UserError> {
|
2021-07-10 08:27:20 +00:00
|
|
|
let params: SignUpParams = data.into_inner().try_into()?;
|
2021-07-11 07:33:19 +00:00
|
|
|
let user = session.sign_up(params).await?;
|
|
|
|
let user_detail = UserDetail::from(user);
|
|
|
|
response_ok(user_detail)
|
|
|
|
}
|
2021-07-10 08:27:20 +00:00
|
|
|
|
2021-07-11 07:33:19 +00:00
|
|
|
pub async fn user_get_status(
|
|
|
|
session: ModuleData<Arc<UserSession>>,
|
2021-07-11 13:54:55 +00:00
|
|
|
) -> ResponseResult<UserDetail, UserError> {
|
2021-07-11 09:38:03 +00:00
|
|
|
let user_detail = session.current_user_detail().await?;
|
2021-07-11 07:33:19 +00:00
|
|
|
response_ok(user_detail)
|
2021-06-29 15:21:25 +00:00
|
|
|
}
|
2021-07-11 09:38:03 +00:00
|
|
|
|
2021-07-11 13:54:55 +00:00
|
|
|
pub async fn user_sign_out(session: ModuleData<Arc<UserSession>>) -> Result<(), UserError> {
|
2021-07-11 09:38:03 +00:00
|
|
|
let _ = session.sign_out().await?;
|
|
|
|
Ok(())
|
|
|
|
}
|