mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2025-12-25 14:14:40 +00:00
fix: set auth type
This commit is contained in:
parent
2f5b494885
commit
2c5f41b580
@ -2,6 +2,7 @@ import 'package:appflowy/user/application/user_listener.dart';
|
||||
import 'package:appflowy_backend/dispatch/dispatch.dart';
|
||||
import 'package:appflowy_backend/log.dart';
|
||||
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
|
||||
import 'package:appflowy_backend/protobuf/flowy-user/user_profile.pb.dart';
|
||||
import 'package:appflowy_backend/protobuf/flowy-user/workspace.pb.dart';
|
||||
import 'package:appflowy_result/appflowy_result.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
@ -389,7 +389,7 @@ class _CreateWorkspaceButton extends StatelessWidget {
|
||||
workspaceBloc.add(
|
||||
UserWorkspaceEvent.createWorkspace(
|
||||
name,
|
||||
AuthTypePB.Local,
|
||||
AuthTypePB.Server,
|
||||
),
|
||||
);
|
||||
},
|
||||
|
||||
@ -72,7 +72,7 @@ async fn migrate_anon_user_data_to_af_cloud_test() {
|
||||
let user = test.af_cloud_sign_up().await;
|
||||
let workspace = test.get_current_workspace().await;
|
||||
println!("user workspace: {:?}", workspace.id);
|
||||
assert_eq!(user.auth_type, AuthTypePB::AppFlowyCloud);
|
||||
assert_eq!(user.auth_type, AuthTypePB::Server);
|
||||
|
||||
let user_first_level_views = test.get_all_workspace_views().await;
|
||||
assert_eq!(user_first_level_views.len(), 3);
|
||||
|
||||
@ -82,12 +82,12 @@ impl ServerProvider {
|
||||
|
||||
pub fn set_auth_type(&self, new_auth_type: AuthType) {
|
||||
let old_type = self.get_auth_type();
|
||||
info!(
|
||||
"ServerProvider: set auth type from {:?} to {:?}",
|
||||
old_type, new_auth_type
|
||||
);
|
||||
|
||||
if old_type != new_auth_type {
|
||||
info!(
|
||||
"ServerProvider: auth type from {:?} to {:?}",
|
||||
old_type, new_auth_type
|
||||
);
|
||||
|
||||
self.auth_type.store(Arc::new(new_auth_type));
|
||||
if let Some((auth_type, _)) = self.providers.remove(&old_type) {
|
||||
info!("ServerProvider: remove old auth type: {:?}", auth_type);
|
||||
|
||||
@ -55,7 +55,6 @@ impl UserStatusCallback for UserStatusCallbackImpl {
|
||||
auth_type: &AuthType,
|
||||
) -> FlowyResult<()> {
|
||||
let workspace_id = user_workspace.workspace_id()?;
|
||||
self.server_provider.set_auth_type(*auth_type);
|
||||
|
||||
if let Some(cloud_config) = cloud_config {
|
||||
self
|
||||
@ -102,8 +101,6 @@ impl UserStatusCallback for UserStatusCallbackImpl {
|
||||
user_workspace,
|
||||
device_id
|
||||
);
|
||||
self.server_provider.set_auth_type(*auth_type);
|
||||
|
||||
self
|
||||
.folder_manager
|
||||
.initialize_after_sign_in(user_id)
|
||||
@ -130,8 +127,6 @@ impl UserStatusCallback for UserStatusCallbackImpl {
|
||||
device_id: &str,
|
||||
auth_type: &AuthType,
|
||||
) -> FlowyResult<()> {
|
||||
self.server_provider.set_auth_type(*auth_type);
|
||||
|
||||
event!(
|
||||
tracing::Level::TRACE,
|
||||
"Notify did sign up: is new: {} user_workspace: {:?}, device_id: {}",
|
||||
@ -212,7 +207,6 @@ impl UserStatusCallback for UserStatusCallbackImpl {
|
||||
user_workspace: &UserWorkspace,
|
||||
auth_type: &AuthType,
|
||||
) -> FlowyResult<()> {
|
||||
self.server_provider.set_auth_type(*auth_type);
|
||||
self
|
||||
.folder_manager
|
||||
.initialize_after_open_workspace(user_id)
|
||||
|
||||
@ -84,7 +84,7 @@ impl FolderCloudService for LocalServerFolderCloudServiceImpl {
|
||||
workspace_id: &Uuid,
|
||||
view_ids: Vec<Uuid>,
|
||||
) -> Result<(), FlowyError> {
|
||||
Err(FlowyError::local_version_not_support())
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_publish_info(&self, view_id: &Uuid) -> Result<PublishInfo, FlowyError> {
|
||||
|
||||
@ -48,7 +48,8 @@ impl UserCloudService for LocalServerUserServiceImpl {
|
||||
latest_workspace: user_workspace.clone(),
|
||||
user_workspaces: vec![user_workspace],
|
||||
is_new_user: true,
|
||||
email: Some(params.email),
|
||||
// Anon user doesn't have email
|
||||
email: None,
|
||||
token: None,
|
||||
encryption_type: EncryptionType::NoEncryption,
|
||||
updated_at: timestamp(),
|
||||
|
||||
@ -172,7 +172,6 @@ pub fn delete_all_then_insert_user_workspaces(
|
||||
) -> FlowyResult<()> {
|
||||
conn.immediate_transaction(|conn| {
|
||||
delete_user_all_workspace(uid, auth_type, conn)?;
|
||||
|
||||
info!(
|
||||
"Insert {} workspaces for user {} and auth type {:?}",
|
||||
user_workspaces.len(),
|
||||
|
||||
@ -255,9 +255,6 @@ impl From<i32> for AuthTypePB {
|
||||
match value {
|
||||
0 => AuthTypePB::Local,
|
||||
1 => AuthTypePB::Server,
|
||||
// For historical reasons, 2 also maps to Server.
|
||||
// Check the AuthenticatorType in flowy-server-pub
|
||||
2 => AuthTypePB::Server,
|
||||
_ => AuthTypePB::Server,
|
||||
}
|
||||
}
|
||||
|
||||
@ -44,18 +44,12 @@ pub async fn sign_in_with_email_password_handler(
|
||||
let manager = upgrade_manager(manager)?;
|
||||
let params: SignInParams = data.into_inner().try_into()?;
|
||||
|
||||
let old_authenticator = manager.cloud_service.get_server_auth_type();
|
||||
match manager
|
||||
.sign_in_with_password(¶ms.email, ¶ms.password)
|
||||
.await
|
||||
{
|
||||
Ok(token) => data_result_ok(token.into()),
|
||||
Err(err) => {
|
||||
manager
|
||||
.cloud_service
|
||||
.set_server_auth_type(&old_authenticator);
|
||||
return Err(err);
|
||||
},
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
@ -77,13 +71,9 @@ pub async fn sign_up(
|
||||
let params: SignUpParams = data.into_inner().try_into()?;
|
||||
let auth_type = params.auth_type;
|
||||
|
||||
let prev_auth_type = manager.cloud_service.get_server_auth_type();
|
||||
match manager.sign_up(auth_type, BoxAny::new(params)).await {
|
||||
Ok(profile) => data_result_ok(UserProfilePB::from(profile)),
|
||||
Err(err) => {
|
||||
manager.cloud_service.set_server_auth_type(&prev_auth_type);
|
||||
Err(err)
|
||||
},
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -132,18 +132,19 @@ impl UserManager {
|
||||
|
||||
if let Ok(session) = self.get_session() {
|
||||
let user = self.get_user_profile_from_disk(session.user_id).await?;
|
||||
self.cloud_service.set_server_auth_type(&user.auth_type);
|
||||
|
||||
// Get the current authenticator from the environment variable
|
||||
let current_authenticator = current_authenticator();
|
||||
let env_auth_type = current_authenticator();
|
||||
|
||||
// If the current authenticator is different from the authenticator in the session and it's
|
||||
// not a local authenticator, we need to sign out the user.
|
||||
if user.auth_type != AuthType::Local && user.auth_type != current_authenticator {
|
||||
if user.auth_type != AuthType::Local && user.auth_type != env_auth_type {
|
||||
event!(
|
||||
tracing::Level::INFO,
|
||||
"Authenticator changed from {:?} to {:?}",
|
||||
"Auth type changed from {:?} to {:?}",
|
||||
user.auth_type,
|
||||
current_authenticator
|
||||
env_auth_type
|
||||
);
|
||||
self.sign_out().await?;
|
||||
return Ok(());
|
||||
@ -151,7 +152,7 @@ impl UserManager {
|
||||
|
||||
event!(
|
||||
tracing::Level::INFO,
|
||||
"init user session: {}:{}, authenticator: {:?}",
|
||||
"init user session: {}:{}, auth type: {:?}",
|
||||
user.uid,
|
||||
user.email,
|
||||
user.auth_type,
|
||||
@ -389,9 +390,10 @@ impl UserManager {
|
||||
auth_type: AuthType,
|
||||
params: BoxAny,
|
||||
) -> Result<UserProfile, FlowyError> {
|
||||
self.cloud_service.set_server_auth_type(&auth_type);
|
||||
|
||||
// sign out the current user if there is one
|
||||
let migration_user = self.get_migration_user(&auth_type).await;
|
||||
self.cloud_service.set_server_auth_type(&auth_type);
|
||||
let auth_service = self.cloud_service.get_user_service()?;
|
||||
let response: AuthResponse = auth_service.sign_up(params).await?;
|
||||
let new_user_profile = UserProfile::from((&response, &auth_type));
|
||||
@ -401,28 +403,6 @@ impl UserManager {
|
||||
Ok(new_user_profile)
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "info", skip(self))]
|
||||
pub async fn resume_sign_up(&self) -> Result<(), FlowyError> {
|
||||
let UserAuthProcess {
|
||||
user_profile,
|
||||
migration_user,
|
||||
response,
|
||||
authenticator,
|
||||
} = self
|
||||
.auth_process
|
||||
.lock()
|
||||
.await
|
||||
.clone()
|
||||
.ok_or(FlowyError::new(
|
||||
ErrorCode::Internal,
|
||||
"No resumable sign up data",
|
||||
))?;
|
||||
self
|
||||
.continue_sign_up(&user_profile, migration_user, response, &authenticator)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "info", skip_all, err)]
|
||||
async fn continue_sign_up(
|
||||
&self,
|
||||
@ -436,9 +416,7 @@ impl UserManager {
|
||||
self
|
||||
.save_auth_data(&response, *auth_type, &new_session)
|
||||
.await?;
|
||||
let _ = self
|
||||
.initial_user_awareness(&new_session, &new_user_profile.auth_type)
|
||||
.await;
|
||||
let _ = self.initial_user_awareness(&new_session, auth_type).await;
|
||||
self
|
||||
.user_status_callback
|
||||
.read()
|
||||
@ -670,6 +648,7 @@ impl UserManager {
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(level = "info", skip_all)]
|
||||
pub(crate) async fn generate_sign_in_url_with_email(
|
||||
&self,
|
||||
authenticator: &AuthType,
|
||||
@ -682,6 +661,7 @@ impl UserManager {
|
||||
Ok(url)
|
||||
}
|
||||
|
||||
#[instrument(level = "info", skip_all)]
|
||||
pub(crate) async fn sign_in_with_password(
|
||||
&self,
|
||||
email: &str,
|
||||
@ -695,6 +675,7 @@ impl UserManager {
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
#[instrument(level = "info", skip_all)]
|
||||
pub(crate) async fn sign_in_with_magic_link(
|
||||
&self,
|
||||
email: &str,
|
||||
@ -710,6 +691,7 @@ impl UserManager {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[instrument(level = "info", skip_all)]
|
||||
pub(crate) async fn sign_in_with_passcode(
|
||||
&self,
|
||||
email: &str,
|
||||
@ -723,6 +705,7 @@ impl UserManager {
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
#[instrument(level = "info", skip_all)]
|
||||
pub(crate) async fn generate_oauth_url(
|
||||
&self,
|
||||
oauth_provider: &str,
|
||||
|
||||
@ -154,6 +154,8 @@ impl UserManager {
|
||||
#[instrument(skip(self), err)]
|
||||
pub async fn open_workspace(&self, workspace_id: &Uuid, auth_type: AuthType) -> FlowyResult<()> {
|
||||
info!("open workspace: {}, auth_type:{}", workspace_id, auth_type);
|
||||
self.cloud_service.set_server_auth_type(&auth_type);
|
||||
|
||||
let uid = self.user_id()?;
|
||||
let conn = self.db_connection(self.user_id()?)?;
|
||||
let user_workspace = match select_user_workspace(&workspace_id.to_string(), conn) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user